How to import other js files/library in casperjs

907 views
Skip to first unread message

Devi Sridharan

unread,
Feb 13, 2015, 1:50:12 AM2/13/15
to casp...@googlegroups.com
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.

Eloi Tay

unread,
Mar 13, 2015, 12:02:07 AM3/13/15
to casp...@googlegroups.com
var require = patchRequire(require); should be placed at where your module is in this case utility.js and that is assuming you use casper function in that utility.js file or else it is not required.
In the test1.js, you will need  var helper = require('utility'); which I think is what you are doing now.

The key missing part is think is the export function 
exports.answer = function() { return utils.format("it's %d", magic); };
This should be placed in utility so that in test1.js you can call it via console.log(helper.answer());
Please do take note that casperJS is not compatible with NPM modules so if you do use it you have to reference it with the full path to the actual js instead of just the name.

Hasan Naqvi

unread,
Mar 13, 2015, 2:20:08 AM3/13/15
to casp...@googlegroups.com
Hi!

Use --include="utility.js, test1.js, test2.js"

Regards,
Hasan
Reply all
Reply to author
Forward
0 new messages