Search code examples
javascripttestingphantomjscasperjsfile-import

How to import other js files in casperjs


Basically,I have extracted the re-usable functions into a file 'utility.js'.I want to import this file into every CasperJS test script.I'm following the documentation here

When i try to get it to work,I'm getting the following error.

'undefined' is not a function

Folder structure is like this

utility.js
test1.js
test2.js

Test file has following code as suggested by the above documentation

var require = patchRequire(require);
var helper = require('utility');

and I access the function in utility.js as follows helper.fn_do_something()

I tried to use other methods provided by casperJS,like

casper.options.clientscript = [relativepath];

But that didn't help.I'm getting the same error.

I'm using PhantomJS 1.9.8.


Solution

  • You can also use phantom objects within your casperjs script.

    Try specifying utility as a library path:

    phantom.libraryPath=[relativePath]
    

    See phantom js docs for further options: http://phantomjs.org/api/phantom/property/library-path.html

    According to the author of casperjs

    The phantom object is available anywhere within a casperjs env, mostly because a casperjs environment is at first a phantomjs environment. https://github.com/n1k0/casperjs/issues/355