CasperJS: a navigation scripting & testing utility for PhantomJS

CasperJS is an open source navigation scripting & testing utility written in Javascript and based on PhantomJS — the scriptable headless WebKit engine. It eases the process of defining a full navigation scenario and provides useful high-level functions, methods & syntactic sugar for doing common tasks

Written on top of the previously mentioned PhantomJS

var links = [];
var casper = require('casper').create();

function getLinks() {
    var links = document.querySelectorAll('h3.r a');
    return, function(e) {
        return e.getAttribute('href')

casper.start('', function() { // search for 'casperjs' from google form
    this.fill('form[action="/search"]', { q: 'casperjs' }, true);

casper.then(function() { // aggregate results for the 'casperjs' search
    links = this.evaluate(getLinks);
}); { // echo results in some pretty fashion
    this.echo(links.length + ' links found:');
    this.echo(' - ' + links.join('\n - ')).exit();

Also ships with a handful of tools to perform functional testing:

var casper = require('casper').create();

casper.start('', function() {
    this.test.assertTitle('Google', 'google homepage title is the one expected');
    this.test.assertExists('form[action="/search"]', 'main form is found');
    this.fill('form[action="/search"]', {
        q: 'foo'
    }, true);

casper.then(function() {
    this.test.assertTitle('foo - Recherche Google', 'google title is ok');
    this.test.assertUrlMatch(/q=foo/, 'search term has been submitted');
    this.test.assertEval(function() {
        return __utils__.findAll('h3.r').length >= 10;
    }, 'google search for "foo" retrieves 10 or more results');
}); {
    this.test.done(5); // checks that 5 assertions have been executed

CasperJS →

If you want to use CasperJS from within Node, check out SpookyJS

Published by Bramus!

Bramus is a Freelance Web Developer from Belgium. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.