CasperJS API 第三篇
来源:互联网 发布:网络高级工程师认证 编辑:程序博客网 时间:2024/05/19 11:47
- then()
casper.start('http://google.fr/');casper.then(function() { this.echo("I'm in your google.");});casper.then(function() { this.echo('Now, let me write something');});casper.then(function() { this.echo('Oh well.');});casper.run();
- thenBypass()
casper.start('http://foo.bar/');casper.thenBypass(2);casper.then(function() { // This test won't be executed});casper.then(function() { // Nor this one});casper.then(function() { // While this one will});casper.run();
- thenBypassIf()
var universe = { answer: 42};casper.start('http://foo.bar/');casper.thenBypassIf(function() { return universe && universe.answer === 42;}, 2);casper.then(function() { // This step won't be executed as universe.answer is 42});casper.then(function() { // Nor this one});casper.then(function() { // While this one will});casper.run();
thenBypassUnless()
…thenClick()
// Click the first link in the casperJS pagecasper.start('http://casperjs.org/').thenClick('a', function() { this.echo("I clicked on first link found, the page is now loaded.");});casper.run();
- thenEvaluate()
// Querying for "Chuck Norris" on Googlecasper.start('http://google.fr/').thenEvaluate(function(term) { document.querySelector('input[name="q"]').setAttribute('value', term); document.querySelector('form[name="f"]').submit();}, 'Chuck Norris');casper.run();
- thenOpen()
casper.start('http://google.fr/').then(function() { this.echo("I'm in your google.");});casper.thenOpen('http://yahoo.fr/', function() { this.echo("Now I'm in your yahoo.")});casper.run();
casper.start().thenOpen('http://url.to/some/uri', { method: "post", data: { username: 'chuck', password: 'n0rr15' }}, function() { this.echo("POST request has been sent.")});casper.run();
- thenOpenAndEvaluate()
casper.start('http://google.fr/').then(function() { this.echo("I'm in your google.");});casper.thenOpenAndEvaluate('http://yahoo.fr/', function() { var f = document.querySelector('form'); f.querySelector('input[name=q]').value = 'chuck norris'; f.submit();});casper.run(function() { this.debugPage(); this.exit();});
- toString()
casper.start('http://google.fr/', function() { this.echo(this); // [object Casper], currently at http://google.fr/});casper.run();
unwait()
Abort all current waiting processes, if any.userAgent()
casper.start();casper.userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X)');casper.thenOpen('http://google.com/', function() { this.echo("I'm a Mac."); this.userAgent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)');});casper.thenOpen('http://google.com/', function() { this.echo("I'm a PC.");});casper.run();
- visible()
casper.start('http://google.com/', function() { if (this.visible('#hplogo')) { this.echo("I can see the logo"); } else { this.echo("I can't see the logo"); }});
- wait()
casper.start('http://yoursite.tld/', function() { this.wait(1000, function() { this.echo("I've waited for a second."); });});casper.run();
casper.start('http://yoursite.tld/');casper.wait(1000, function() { this.echo("I've waited for a second.");});casper.run();
- waitFor()
casper.start('http://yoursite.tld/');casper.waitFor(function check() { return this.evaluate(function() { return document.querySelectorAll('ul.your-list li').length > 2; });}, function then() { this.captureSelector('yoursitelist.png', 'ul.your-list');});casper.run();
casper.start('http://yoursite.tld/');casper.waitFor(function check() { return this.evaluate(function() { return document.querySelectorAll('ul.your-list li').length > 2; });}, function then() { // step to execute when check() is ok this.captureSelector('yoursitelist.png', 'ul.your-list');}, function timeout() { // step to execute if check has failed this.echo("I can't haz my screenshot.").exit();});casper.run();
- waitForAlert()
casper.waitForAlert(function(response) { this.echo("Alert received: " + response.data);});
- waitForExec()
// merge captured PDFs with default system shell (bash on Linux) calling /usr/bin/gs, and runs a small script to remove filescasper.waitForExec(null, ['-c','{ /usr/bin/gs -dPDFSETTINGS=/ebook -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=/my_merged_captures.pdf /my_captures*.pdf && /bin/rm /my_captures*.pdf; } || { /bin/rm /my_merged_captures.pdf && exit 1; }'], function(response) { this.echo("Program finished by itself:" + JSON.stringify(response.data)); }, function(timeout, response) { this.echo("Program finished by casper:" + JSON.stringify(response.data));}); // merge captured PDFs with bash calling /usr/bin/gs, and runs a small script to remove filescasper.waitForExec('/bin/bash -c', ['{ /usr/bin/gs -dPDFSETTINGS=/ebook -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=/my_merged_captures.pdf /my_captures*.pdf && /bin/rm /my_captures*.pdf; } || { /bin/rm /my_merged_captures.pdf && exit 1; }'], function(response) { this.echo("Program finished by itself:" + JSON.stringify(response.data)); }, function(timeout, response) { this.echo("Program finished by casper:" + JSON.stringify(response.data));});// merge captured PDFs calling /usr/bin/gscasper.waitForExec('/usr/bin/gs',['-dPDFSETTINGS=/ebook','-dBATCH','-dNOPAUSE','-q','-sDEVICE=pdfwrite','-sOutputFile=/my_merged_captures_pdfs.pdf','/my_captures_1.pdf','/my_captures_2.pdf','/my_captures_3.pdf'], function(response) { this.echo("Program finished by itself:" + JSON.stringify(response.data)); }, function(timeout, response) { this.echo("Program finished by casper:" + JSON.stringify(response.data));});// merge captured PDFs calling /usr/bin/gscasper.waitForExec('/usr/bin/gs -dPDFSETTINGS=/ebook -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=/my_merged_captures_pdfs.pdf /my_captures_1.pdf /my_captures_2.pdf /my_captures_3.pdf', null, function(response) { this.echo("Program finished by itself:" + JSON.stringify(response.data)); }, function(timeout, response) { this.echo("Program finished by casper:" + JSON.stringify(response.data));});
- waitForPopup()
casper.start('http://foo.bar/').then(function() { this.test.assertTitle('Main page title'); this.clickLabel('Open me a popup');});// this will wait for the popup to be opened and loadedcasper.waitForPopup(/popup\.html$/, function() { this.test.assertEquals(this.popups.length, 1);});// this will wait for the first popup to be opened and loadedcasper.waitForPopup(0, function() { this.test.assertEquals(this.popups.length, 1);});// this will wait for the popup's named to be opened and loadedcasper.waitForPopup({windowName: "mainPopup"}, function() { this.test.assertEquals(this.popups.length, 1);});// this will wait for the popup's title to be opened and loadedcasper.waitForPopup({title: "Popup Title"}, function() { this.test.assertEquals(this.popups.length, 1);});// this will wait for the popup's url to be opened and loadedcasper.waitForPopup({url: 'http://foo.bar/'}, function() { this.test.assertEquals(this.popups.length, 1);});// this will set the popup DOM as the main active one only for time the// step closure being executedcasper.withPopup(/popup\.html$/, function() { this.test.assertTitle('Popup title');});// next step will automatically revert the current page to the initial onecasper.then(function() { this.test.assertTitle('Main page title');});
- waitForResource()
casper.waitForResource("foobar.png", function() { this.echo('foobar.png has been loaded.');});
casper.waitForResource(/foo(bar|baz)\.png$/, function() { this.echo('foobar.png or foobaz.png has been loaded.');});
casper.waitForResource(function testResource(resource) { return resource.url.indexOf("https") === 0;}, function onReceived() { this.echo('a secure resource has been loaded.');});
- waitForUrl()
casper.start('http://foo/').waitForUrl(/login\.html$/, function() { this.echo('redirected to login.html');});casper.run();
- waitForSelector()
casper.start('https://twitter.com/#!/n1k0');casper.waitForSelector('.tweet-row', function() { this.captureSelector('twitter.png', 'html');});casper.run();
- waitWhileSelector()
casper.start('http://foo.bar/');casper.waitWhileSelector('.selector', function() { this.echo('.selector is no more!');});casper.run();
- waitForSelectorTextChange()
casper.start('http://foo.bar/');casper.waitForSelectorTextChange('.selector', function() { this.echo('The text on .selector has been changed.');});casper.run();
- waitForText()
casper.start('http://why.univer.se/').waitForText("42", function() { this.echo('Found the answer.');});casper.run();
waitUntilVisible()
Waits until an element matching the provided selector expression is visible in the remote DOM to process a next step. Uses waitFor().waitWhileVisible()
var casper = require('casper').create();casper.start('https://www.example.com/').thenClick('html body div p a', function () { this.waitWhileVisible('body > div:nth-child(1) > p:nth-child(2)', function () { this.echo("The selected element existed in previous page but doesn't exist in this page."); })}).run();
- warn()
casper.warn("I'm a warning message.");
- withFrame()
casper.start('tests/site/frames.html', function() { this.test.assertTitle('FRAMESET TITLE');});casper.withFrame('frame1', function() { this.test.assertTitle('FRAME TITLE');});casper.withFrame(0, function() { this.test.assertTitle('FRAME TITLE');});casper.then(function() { this.test.assertTitle('FRAMESET TITLE');});
- withPopup()
casper.start('http://foo.bar/').then(function() { this.test.assertTitle('Main page title'); this.clickLabel('Open me a popup');});// this will wait for the popup to be opened and loadedcasper.waitForPopup(/popup\.html$/, function() { this.test.assertEquals(this.popups.length, 1);});// this will set the popup DOM as the main active one only for time the// step closure being executedcasper.withPopup(/popup\.html$/, function() { this.test.assertTitle('Popup title');});// this will set the popup DOM as the main active one only for time the// step closure being executedcasper.withPopup(0, function() { this.test.assertTitle('Popup title');});// this will set the popup DOM as the main active one only for time the// step closure being executedcasper.withPopup({windowName: "mainPopup", title:'Popup title', url:'http://foo.bar/'}, function() { this.test.assertTitle('Popup title');});// next step will automatically revert the current page to the initial onecasper.then(function() { this.test.assertTitle('Main page title');});
withSelectorScope()
Switches the main DOM scope to a specific scope the information passed as argument, and processes a step. The scope context switch only lasts until the step execution is finished:zoom()
var casper = require('casper').create();casper.start().zoom(2).thenOpen('http://google.com', function() { this.capture('big-google.png');});casper.run();
阅读全文
0 0
- CasperJS API 第三篇
- CasperJS API 第二篇
- CasperJS API 第一篇
- CasperJS API介绍(1)-- CasperJS Class
- CasperJS API介绍(2)-- CasperJS最基本API介绍
- CasperJS API介绍(3)-- CasperJS utils module API介绍
- casperjs 的API-casper模块
- casperjs
- Casperjs
- casperjs
- CasperJS
- [CasperJS] API--The casper module(译)
- NumPy详细API第三篇
- Jquery第三篇【AJAX 相关的API】
- casperjs小结
- casperjs小结
- [CasperJS] 安装
- plantomjs&casperjs
- 笔记本不显示“选择关闭盖子的功能”
- 小技巧
- Eclipse中Web项目出现感叹或红叉该怎么解决?
- java 运算符
- Flum+hdfs
- CasperJS API 第三篇
- ThreadLocal使用
- docker问题汇总
- 使用Mockito进行单元测试【1】——mock and verify
- 数十种TensorFlow实现案例汇集:代码+笔记
- esriControlsMousePointer
- python生成器小结
- 657
- 爬虫入门五(Phantomjs和selenium)