1.1-beta1

@n1k0 n1k0 released this Jul 13, 2013 · 826 commits to master since this release

2013-07-13, v1.1-beta1

1.0 users, an upgrading guide is available here.

Important Changes & Caveats

Minimum PhantomJS version

PhantomJS 1.8.1 or later is required for 1.1.

Experimental support of Gecko, with SlimerJS

CasperJS can now be launched with SlimerJS instead of PhantomJS.
It allows you to execute tests with the rendering engine of Firefox. Just launch CasperJS
with the flag --engine=slimerjs.

SlimerJS 0.8 or later is required.

require() in custom modules

CasperJS 1.1 now internally uses PhantomJS' native require() function, but it has side effect if you write your own casperjs modules; in any casperjs module, you now have to use the new global patchRequire() function first:

// casperjs module code
var require = patchRequire(require);
// now you can require casperjs builtins
var utils = require('utils');
exports = {
    // ...
};

Note: you don't have to use patchRequire() in a standard casperjs script.

Testing framework refactoring

A new Tester.begin() method has been introduced to help organizing tests better:

function Cow() {
    this.mowed = false;
    this.moo = function moo() {
        this.mowed = true; // mootable state: don't do that
        return 'moo!';
    };
}

// unit style synchronous test case
casper.test.begin('Cow can moo', 2, function suite(test) {
    var cow = new Cow();
    test.assertEquals(cow.moo(), 'moo!');
    test.assert(cow.mowed);
    test.done();
});

// asynchronous test case
casper.test.begin('Casperjs.org is navigable', 2, function suite(test) {
    casper.start('http://casperjs.org/', function() {
        test.assertTitleMatches(/casperjs/i);
        this.clickLabel('Testing');
    });

    casper.then(function() {
        test.assertUrlMatches(/testing\.html$/);
    });

    casper.run(function() {
        test.done();
    });
});

Tester#setUp() and Tester#tearDown() methods have been also added in order to ease the definition of operations to be performed before and after each test defined using Tester#begin():

casper.test.setUp(function() {
    console.log('executed before each test');
});

casper.test.tearDown(function() {
    console.log('executed after each test');
});

Both can work asynchronously as well of you define the done argument:

casper.test.setUp(function(done) {
    setTimeout(function() {
        console.log('asynchronously executed before each test');
        done();
    }, 1000);
});

casper.test.tearDown(function(done) {
    setTimeout(function() {
        console.log('asynchronously executed after each test');
        done();
    }, 1000);
});

Tester#begin() itself has also local setUp() and tearDown() capabilities if you pass it a configuration object instead of a function:

casper.test.begin('range tests', 1, {
    setUp: function(test) {
        this.range.push(1);
        this.range.push(2);
        this.range.push(3);
    },

    tearDown: function(test) {
        this.range = [];
    },

    test: function(test) {
        test.assertEquals(this.range.length, 3);
        test.done();
    }
});

Scraping and testing are now better separated in CasperJS. That involves breaking up BC on some points though:

  • The Casper object won't be created with a test reference if not invoked using the casperjs test command, therefore the ability to run any test without calling it has been dropped. I know, get over it.
  • Passing the planned number of tests to casper.done() has been dropped as well, because done() may be never called at all when big troubles happen; rather use the new begin() method and provide the expected number of tests using the second argument:
casper.test.begin("Planning 4 tests", 4, function(test) {
    [1, 2, 3, 4].forEach(function() {
        test.assert(true);
    });
    test.done();
});

Last, all the casper test suites have been upgraded to use the new testing features, you may want to have a look at the changes.

Request abortion

When using PhantomJS >=1.9.0, you can now abort outgoing requests:

casper.on('page.resource.requested', function(requestData, request) {
    if (requestData.url.indexOf('http://adserver.com') === 0) {
        request.abort();
    }
});

Bugfixes & enhancements

Downloads