CasperJS provides an event handler very similar to the nodejs' one; actually it borrows most of its codebase. CasperJS also adds filters, which are basically ways to alter values asynchronously.
.. index:: ! events
Using events is pretty much straightforward if you're a node developer, or if you worked with any evented system before:
var casper = require('casper').create();
casper.on('resource.received', function(resource) {
casper.echo(resource.url);
});
Of course you can emit your own events, using the Casper.emit() method:
var casper = require('casper').create();
// listening to a custom event
casper.on('google.loaded', function() {
this.echo('Google page title is ' + this.getTitle());
});
casper.start('http://google.com/', function() {
// emitting a custom event
this.emit('google.loaded');
});
casper.run();
Arguments: None
Emitted when the embedded browser is asked to go back a step in its history.
Arguments: targetFile
Emitted when a :index:`screenshot` image has been captured.
.. index:: click
Arguments: selector
Emitted when the Casper.click() method has been called.
Arguments: message, status
Emitted when the Casper.die() method has been called.
.. index:: download
Arguments: targetPath
Emitted when a file has been downloaded by :ref:`Casper.download() <casper_download>`; target will contain the path to the downloaded file.
.. index:: error
Arguments: msg, backtrace
.. versionadded:: 0.6.9
Emitted when an error hasn't been caught. Do basically what PhantomJS' onError() native handler does.
.. index:: exit
Arguments: status
Emitted when the Casper.exit() method has been called.
.. index:: fill
Arguments: selector, vals, submit
Emitted when a form is filled using the Casper.fill() method.
Arguments: None
Emitted when the embedded browser is asked to go forward a step in its history.
.. index:: auth
Arguments: username, password
Emitted when http authentication parameters are set.
.. index:: HTTP
Arguments: resource
Emitted when any given HTTP reponse is received with the status code specified by [code], eg.:
casper.on('http.status.404', function(resource) {
casper.echo(resource.url + ' is 404');
})
Arguments: None
Emitted when PhantomJS' WebPage.onLoadStarted event callback is called.
Arguments: Object
Emitted when PhantomJS' WebPage.onLoadFinished event callback has been called and failed.
Arguments: status
Emitted when PhantomJS' WebPage.onLoadFinished event callback is called.
.. index:: log
Arguments: entry
Emitted when the Casper.log() method has been called. The entry parameter is an Object like this:
{
level: "debug",
space: "phantom",
message: "A message",
date: "a javascript Date instance"
}
..index:: click
Arguments: args
Emitted when the mouse left-click something or somewhere.
Arguments: args
Emitted when the mouse presses on something or somewhere with the left button.
Arguments: args
Emitted when the mouse moves onto something or somewhere.
Arguments: args
Emitted when the mouse releases the left button over something or somewhere.
Arguments: url, navigationType, navigationLocked, isMainFrame
.. versionadded:: 1.0
Emitted each time a navigation operation has been requested. Available navigation types are: LinkClicked, FormSubmitted, BackOrForward, Reload, FormResubmitted and Other.
.. index:: HTTP
location, settings
Emitted when an HTTP request is sent. First callback arg is the location, second one is a request settings Object of the form:
{
method: "post",
data: "foo=42&chuck=norris"
}
Arguments: page
Emitted when PhantomJS' WebPage object used by CasperJS has been created.
Arguments: message, trace
Emitted when retrieved page leaved a Javascript error uncaught:
casper.on("page.error", function(msg, trace) {
this.echo("Error: " + msg, "ERROR");
});
Arguments: WebPage
Emitted when PhantomJS' WebPage object used by CasperJS has been initialized.
.. index:: HTTP
Arguments: response
Emitted when the HTTP response corresponding to current required url has been received.
.. index:: HTTP
Arguments: request
Emitted when a new HTTP request is performed to open the required url.
Arguments: WebPage
Emitted when a new window has been opened.
Arguments: WebPage
Emitted when a new window has been loaded.
Arguments: WebPage
Emitted when a new opened window has been closed.
Arguments: message
Emitted when a remote alert() call has been performed.
Arguments: msg
Emitted when any remote console logging call has been performed.
Arguments: resource
Emitted when any resource has been received.
Arguments: request
Emitted when any resource has been requested.
Arguments: None
Emitted when the whole series of steps in the stack have been executed.
Arguments: None
Emitted when Casper.run() is called.
Arguments: None
Emitted when Casper.start() is called.
Arguments: None
Emitted when Casper has been started using Casper.start().
Arguments: step
Emitted when a new navigation step has been added to the stack.
Arguments: stepResult
Emitted when a navigation step has been executed.
Arguments: fn
Emitted when a new navigation step has been created.
Arguments: step
Emitted when a navigation step has been started.
Arguments: None
Emitted when a navigation step has been executed.
Arguments: None
Emitted when the execution time of the script has reached the Casper.options.timeout value.
Arguments: url
Added in 1.0 Emitted each time the current page url changes.
.. index:: viewport
Arguments: [width, height]
Emitted when the viewport has been changed.
Arguments: None
Emitted when a Casper.wait()operation ends.
Arguments: None
Emitted when a Casper.wait() operation starts.
Arguments: None
Emitted when the execution time of a Casper.wait*() operation has exceeded the value of Casper.options.stepTimeout.
.. index:: filters
Filters allow you to alter some values asynchronously. Sounds obscure? Let's take a simple example and imagine you would like to alter every single url opened by CasperJS to append a foo=42 query string parameter:
var casper = require('casper').create();
casper.setFilter('open.location', function(location) {
return /\?+/.test(location) ? location += "&foo=42" : location += "?foo=42";
});
There you have it, every single requested url will have this appended. Let me bet you'll find far more interesting use cases than my silly one ;)
Here'a the list of all available filters with their expected return value:
.. index:: screenshot
Arguments: args
Return type: String
Allows to alter the value of the filename where a screen capture should be stored.
Arguments: message
Return type: String
Allows to alter every message written onto stdout.
Arguments: message
Return type: String
Allows to alter every log message.
Arguments: args
Return type: String
Allows to alter every url before it being opened.
Arguments: message
Return type: Boolean
.. versionadded:: 1.0
Allows to react on a javascript confirm() call:
casper.setFilter("page.confirm", function(msg) {
return msg === "Do you like vbscript?" ? false : true;
});
Arguments: message, value
Return type: String
.. versionadded:: 1.0
Allows to react on a javascript prompt() call:
casper.setFilter("page.prompt", function(msg, value) {
if (msg === "What's your name?") {
return "Chuck";
}
});