varcasper=require('casper').create({clientScripts:['includes/jquery.js',// These two scripts will be injected in remote'includes/underscore.js'// DOM on every request],pageSettings:{loadImages:false,// The WebPage instance used by Casper willloadPlugins:false// use these settings},logLevel:"info",// Only "info" level messages will be loggedverbose:true// log messages will be printed out to the console});

javascriptEnabled defines whether to execute the script in the page or not (default to true)

loadImages defines whether to load the inlined images or not

localToRemoteUrlAccessEnabled defines whether local resource (e.g. from file) can access remote URLs or not (default to false)

userAgent defines the user agent sent to server when the web page requests resources

userName sets the user name used for HTTP authentication

password sets the password used for HTTP authentication

resourceTimeout (in milli-secs) defines the timeout after which any resource requested will stop trying and proceed with other parts of the page. onResourceTimeout callback will be called on timeout. undefined (default value) means default gecko parameters.

When this option is set to true — which is the default, any password information entered in <input type=”password”> will be obfuscated in log messages. Set safeLogs to false to disclose passwords in plain text (not recommended).

Max step timeout in milliseconds; when set, every defined step function will have to execute before this timeout value has been reached. You can define the onStepTimeout() callback to catch such a case. By default, the script will die() with an error message.

casper.start();casper.then(function(){// This step will be executed});casper.then(function(){this.bypass(2);});casper.then(function(){// This test won't be executed});casper.then(function(){// Nor this one});casper.run();

Performs a click on the element matching the provided selector expression. The method tries two strategies sequentially:

trying to trigger a MouseEvent in Javascript

using native QtWebKit event if the previous attempt failed

Example:

casper.start('http://google.fr/');casper.thenEvaluate(function(term){document.querySelector('input[name="q"]').setAttribute('value',term);document.querySelector('form[name="f"]').submit();},'CasperJS');casper.then(function(){// Click on 1st result linkthis.click('h3.r a');});casper.then(function(){// Click on 1st result linkthis.click('h3.r a',10,10);});casper.then(function(){// Click on 1st result linkthis.click('h3.r a',"50%","50%");});casper.then(function(){console.log('clicked ok, new location is '+this.getCurrentUrl());});casper.run();

The pre-1.0 way of passing arguments using an object has been kept for BC purpose, though it may not work in some case; so you’re encouraged to use the method described above.

Understanding evaluate()

The concept behind this method is probably the most difficult to understand when discovering CasperJS. As a reminder, think of the evaluate() method as a gate between the CasperJS environment and the one of the page you have opened; everytime you pass a closure to evaluate(), you’re entering the page and execute code as if you were using the browser console.

Here’s a quickly drafted diagram trying to basically explain the separation of concerns:

Note: You can not rely on the fact that your script will be turned off immediately, because this method works asynchronously. It means that your script may continue to be executed after the call of this method. More info here.

Logs a message with an optional level in an optional space. Available levels are debug, info, warning and error. A space is a kind of namespace you can set for filtering your logs. By default, Casper logs messages in two distinct spaces: phantom and remote, to distinguish what happens in the PhantomJS environment from the remote one:

Casper.run() also accepts an onComplete callback, which you can consider as a custom final step to perform when all the other steps have been executed. Just don’t forget to exit() Casper if you define one!:

When set to true, this option will first empty the current field value. By default, it’s set to false and sendKeys() will just append string to the current field value.

(Boolean)keepFocus:

sendKeys() by default will remove the focus on text input fields, which will typically close autocomplete widgets. If you want to maintain focus, use the keepFocus option. For example, if using jQuery-UI, you can click on the first autocomplete suggestion using:

Sets the maximum number of listeners that can be added for each type of listener:

casper.setMaxListeners(12);

Note

Incorrect registering of listeners in your casper scripts can result in a warning
message indicating that a possible EventEmitter leak has been detected. Ensure you
are adding listeners in the required way.

If you need a listener that will be processed by all of your scripts then ensure it
is only registered once. If you need to add a listener per test suite, for example,
ensure the listener is added during setUp and removed during tearDown. See
Tester#begin() for setUp and tearDown structure.

Warning

Changing the maximum listeners is not recommended. You should only increase it if
you require more than the default amount of listeners in your case. Increasing this
limit will increase it for all listener types and could result in possible
EventEmitter leaks going undetected. If you must increase this limit, increase it
in small increments.

when the previous main HTTP request has been executed and the page loaded;

Note that there’s no single definition of page loaded; is it when the DOMReady event has been triggered? Is it “all requests being finished”? Is it *all application logic being performed”? Or “all elements being rendered”? The answer always depends on the context. Hence why you’re encouraged to always use the waitFor() family methods to keep explicit control on what you actually expect.

Adds a navigation step which will bypass a given number of following steps:

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();

Bypass a given number of navigation steps if the provided condition is truthy or is a function that returns a truthy value:

varuniverse={answer:42};casper.start('http://foo.bar/');casper.thenBypassIf(function(){returnuniverse&&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();

casper.start('http://yoursite.tld/');casper.waitFor(functioncheck(){returnthis.evaluate(function(){returndocument.querySelectorAll('ul.your-list li').length>2;});},functionthen(){// step to execute when check() is okthis.captureSelector('yoursitelist.png','ul.your-list');},functiontimeout(){// step to execute if check has failedthis.echo("I can't haz my screenshot.").exit();});casper.run();

details is a property bag of various information that will be passed to the waitFor.timeout event, if it is emitted.
This can be used for better error messages or to conditionally ignore some timeout events.

Note

All waitFor methods are not chainable. Consider wrapping each of them in a casper.then in order to acheive this functionality.

New in version 1.1.5.

As of 1.1.5 does a last run of check function after timeout if check function is still false.

Waits until command runs with parameters and exits. The command, parameters, pid, stdout, stderr, elapsedTime and exitCode will be in the response.data property.
command must be a string or parameters must be an array.
command can be an string of a executable or an string of a executable and its arguments separated by spaces. If command is falsy or is not a string, system shell (environment variable SHELL or ComSpec) is used. The arguments separated by spaces are concatenated with the parameters array to be send to executable. If parameters is falsy or is not an array, an empty array is used.
timeout can be a number or an array of two numbers, the first is the timeout of wait* family functions and the second is the timeout between TERM and KILL signals on timeout. If not declared, it assumes the same value of the first element or the default timeout of wait* family functions.:

Waits for a popup having its url matching the provided pattern to be opened and loaded.

The currently loaded popups are available in the Casper.popups array-like property:

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');});

Switches the main page to a popup matching the information passed as argument, and processes a step. The page context switch only lasts until the step execution is finished:

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');});

Note

The currently loaded popups are available in the Casper.popups array-like property.