- 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 page
casper.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 Google
casper.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 files
casper.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 files
casper.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/gs
casper.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/gs
casper.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 loaded
casper.waitForPopup(/popup\.html$/, function() {
this.test.assertEquals(this.popups.length, 1);
});
// this will wait for the first popup to be opened and loaded
casper.waitForPopup(0, function() {
this.test.assertEquals(this.popups.length, 1);
});
// this will wait for the popup's named to be opened and loaded
casper.waitForPopup({windowName: "mainPopup"}, function() {
this.test.assertEquals(this.popups.length, 1);
});
// this will wait for the popup's title to be opened and loaded
casper.waitForPopup({title: "Popup Title"}, function() {
this.test.assertEquals(this.popups.length, 1);
});
// this will wait for the popup's url to be opened and loaded
casper.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 executed
casper.withPopup(/popup\.html$/, function() {
this.test.assertTitle('Popup title');
});
// next step will automatically revert the current page to the initial one
casper.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 loaded
casper.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 executed
casper.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 executed
casper.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 executed
casper.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 one
casper.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();