CasperJS is an open source navigation scripting & testing
headless browser and SlimerJS (Gecko).
It eases the process of defining a full navigation scenario
and provides useful high-level functions, methods &
Doing common tasks
defining & ordering browsing navigation steps
filling & submitting forms
clicking & following links
capturing screenshots of a page (or part of it)
testing remote DOM
downloading resources, including binary ones
writing functional test suites, saving results as JUnit XML
scraping Web contents
It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and
A minimal scraping script
What did we just do?
we created a new Casper instance
we started it and opened http://casperjs.org/
once the page has been loaded, we asked to print
the title of that webpage (the content of its <title>
then we opened another url, http://phantomjs.org/
once the new page has been loaded, we asked to
print its title too
we executed the whole process
start(), then(), run(), exit()
Signature: start(String url [, Function then])
Configures and starts Casper, then open the provided url and optionally adds the step provided by the then argument
Signature: then(Function then)
Signature: run(fn onComplete[, int time])
Runs the whole suite of steps and optionally executes a callback when they’ve all been done. Obviously, calling this method is
mandatory in order to run the Casper navigation suite.
Signature: exit([int status])
Exits PhantomJS with an optional exit status code.
Note that the current Casper instance automatically binds the this keyword for you within step functions
casper module – fill
Signature: fill(String selector, Object values[, Boolean submit])
Fills the fields of a form with given values and optionally submits it.
Fields are referenced by their name attribute.
click(Number x, Number y)
Performs a click on the first element found matching the provided selector expression or
at given coordinates if two numbers are passed
CSS3 selector strings
Signature: evaluate(Function fn[, arg1[, arg2[, …]]])
Evaluates an expression in the current page DOM context
Think of the evaluate() method as a gate between the CasperJS environment and the
one of the page you have opened;
Every time you pass a closure to evaluate(), you’re entering the page and execute code
as if you were using the browser console.
Signature: waitForSelector(String selector[, Function then, Function onTimeout, Number
Waits until an element matching the provided selector expression exists in remote DOM to
process any next step