如何在 PhantomJS 或 CasperJS 中导入其他 javascript 模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17673850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:09:25  来源:igfitidea点击:

How to import other javascript module in PhantomJS or CasperJS

javascriptcoffeescriptphantomjscasperjs

提问by Robert Zaremba

I'm trying to build a functional test using CasperJS. caseperjsis run by a backend test suite using the following command:

我正在尝试使用 CasperJS 构建功能测试。 caseperjs由后端测试套件使用以下命令运行:

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

In init.coffeeI want to import/include other module (file) which seats just next to it. How to do it?

init.coffee 中,我想导入/包含位于它旁边的其他模块(文件)。怎么做?

The following doesn't works:

以下不起作用:

require("user")

All I want is to get a content from other file into init.coffee

我想要的只是将其他文件中的内容放入 init.coffee

回答by Mark Simon

After trying a number of the other suggestions (each expected to work in the context of their corresponding environments), hit on this solution:

在尝试了许多其他建议(每个都希望在其相应环境的上下文中工作)之后,找到了这个解决方案:

phantom.page.injectJs( 'script.js');

回答by Robert Zaremba

As of 1.1, CasperJS relies on PhantomJS' native require():
https://groups.google.com/forum/#!topic/phantomjs/0-DYnNn_6Bs

从 1.1 开始,CasperJS 依赖 PhantomJS 的原生require()https: //groups.google.com/forum/#!topic/phantomjs/0-DYnNn_6Bs

Injecting dependencies

注入依赖

While injecting additional modules, CasperJS looks for path relative to cur directory (the place where we run a casperjscommand) We can inject dependency using clientScriptsoption. However the injected dependencies can't use require"globally". They are injected immediately to every page loaded.

在注入额外的模块时,CasperJS 寻找相对于 cur 目录(我们运行casperjs命令的地方)的路径我们可以使用clientScripts选项注入依赖项。但是注入的依赖项不能require“全局”使用。它们会立即注入到加载的每个页面中。

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

Also we can inject modules using commandline args:

我们也可以使用命令行参数注入模块:

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

Using require

使用要求

To import user modules use:

要导入用户模块,请使用:

require "./user-module.coffee"

Then in user modules you can also use require. Using require paths are resolved relative to the current file (where require is called).
If in user module you want to import casper libs, then you need to patch require, check: https://casperjs.readthedocs.org/en/latest/writing_modules.html

然后在用户模块中,您也可以使用 require。使用 require 路径是相对于当前文件(在 require 被调用的地方)解析的。
如果在用户模块中要导入 casper 库,则需要修补 require,请检查:https: //casperjs.readthedocs.org/en/latest/writing_modules.html

回答by Alberto Zaccagni

There's a section about thatin the docs

文档中有一节是关于这个

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

In your case you should use global.requiresince you're using CoffeeScript.

在您的情况下,您应该使用,global.require因为您使用的是 CoffeeScript。