How to import another javascript module into PhantomJS or CasperJS

I am trying to build a functional test using CasperJS. caseperjs is launched by the backend software package using the following command:

PHANTOMJS_EXECUTABLE=../client/node_modules/phantomjs/bin/phantomjs ../client/ext_modules/casperjs/bin/casperjs test ../client/test/functional/init.coffee 

In init.coffee, I want to import / enable another module (file) that is next to it. How to do it?

The following does not work:

 require("user") 

All I want is to get the contents from another file in init.coffee

+6
source share
3 answers

Starting with version 1.1, CasperJS relies on its own phantomJS require () :
https://groups.google.com/forum/#!topic/phantomjs/0-DYnNn_6Bs

Injection Dependence

When introducing additional modules, CasperJS looks for a path relative to the cur directory (the place where we run the casperjs command). We can introduce a dependency using the clientScripts option. However, nested dependencies cannot use require globally. They are immediately entered on each loaded page.

 casper.options.clientScripts = ["path/relative/to/cur/dir"] 

We can also enter modules using the args command line:

 casperjs test --includes=foo.js,bar.js path/to/the/test/file 

Using require

To import custom modules, use:

 require "./user-module.coffee" 

Then in custom modules you can also use require. Use of the required paths is permitted relative to the current file (where the call is required).
If in the user module you want to import casper libraries, you need to pay , check: https://casperjs.readthedocs.org/en/latest/writing_modules.html

+8
source

After trying a number of other suggestions (each of which should work in the context of their respective environments), click on this solution:

 phantom.page.injectJs( 'script.js'); 
+12
source

There's a section on this in the docs

 var require = patchRequire(global.require); require('./user'); 

In your case, you should use global.require since you are using CoffeeScript.

+2
source

Source: https://habr.com/ru/post/949548/


All Articles