Basic Casper.js test

By Alexander van Noord on

Casper.js is an add-on for Phantom.js, which is a headless Webkit which you can inspect with Javascript. Casper.js adds functionality for particularly navigating and testing web pages.

This is a simple example test for a web page. It checks if the correct URL was loaded (without redirects), if the status code is '200 OK' and it echoes all errors or console.log()'s.

var casper = require('casper').create({
    verbose: true,

var url = '';

casper.on('page.error', function(msg, trace) {
    this.echo("Error: " + msg, "ERROR");

casper.on('remote.message', function(msg) {

casper.start(url, function() {
    this.test.assert(this.getCurrentUrl() === url, 'Url is: ' + this.getCurrentUrl());
    this.test.assertHttpStatus(200, 'HTTP status code 200 OK');

The output of running this might look like this:

PASS Url is:
PASS HTTP status code 200 OK