Writing CasperJS modules — CasperJS 1.1.0-DEV documentation

# 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 use 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`:

    require = patchRequire global.require

    utils = require 'utils'
    magic = 42
    exports.answer = ->
      utils.format "it's ${magic}"

From your root casper script:

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

New in version 1.1..

Hint

CasperJS allows using nodejs modules installed through npm. Note that since CasperJS uses it’s own JavaScript environment, npm modules that use node-specific features will not work under CasperJS.

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