如何在casperjs中导入其他js文件

How to import other js files in casperjs

基本上,我已将可重复使用的函数提取到一个文件 'utility.js' 中。我想将此文件导入到每个 CasperJS 测试中 script.I' 遵循文档 here

当我尝试让它工作时,出现以下错误。

'undefined' is not a function

文件夹结构是这样的

utility.js
test1.js
test2.js

测试文件包含上述文档建议的以下代码

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

我访问 utility.js 中的函数如下 helper.fn_do_something()

我尝试使用casperJS提供的其他方法,比如

casper.options.clientscript = [relativepath];

但这并没有help.I得到同样的错误。

我正在使用 PhantomJS 1.9.8。

您还可以在 casperjs 脚本中使用虚拟对象。

尝试将实用程序指定为库路径:

phantom.libraryPath=[relativePath]

有关更多选项,请参阅 phantom js 文档:http://phantomjs.org/api/phantom/property/library-path.html

根据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