writing_modules.rst 1.97 KB

Writing CasperJS modules

As of 1.1, CasperJS relies on PhantomJS' native require() function internally though it had to be patched in order to allow requiring casper modules using their full name, eg. require('casper').

So if you plan to write your own modules and uses casperjs' ones from them, be sure to call the patchRequire() function:

// my module, stored in universe.js
// patching phantomjs' require()
var require = patchRequire(require);

// now you're ready to go
var utils = require('utils');
var magic = 42;
exports.answer = function() {
    return utils.format("it's %d", magic);
};

Warning

When using CoffeeScript global.require must be passed to patchRequire() instead of just require.

From your root casper script:

var universe = require('./universe');
console.log(universe.answer()); // prints "It's 42"

Hint

Like PhantomJS, CasperJS allows using nodejs modules installed through npm.

As an example, let's install the underscore library:

$ npm install underscore

Then, require it like you would with any other nodejs compliant module:

//npm-underscore-test.js
var _ = require('underscore');
var casper = require('casper').create();
var urls = _.uniq([
  'http://google.com/',
  'http://docs.casperjs.org/',
  'http://google.com/'
]);

casper.start().eachThen(urls, function(response) {
  this.thenOpen(response.data, function(response) {
    this.echo(this.getTitle());
  });
});

casper.run();

Finally, you’ll probably get something like this:

$ casperjs npm-underscore-test.js
Google
CasperJS documentation | CasperJS 1.1.0-DEV documentation