javascript 如何通过命令行将变量作为参数传递给 CasperJS 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21765178/
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 21:38:35  来源:igfitidea点击:

How to pass a variable as an argument to a CasperJS script through the command line?

javascriptjquerycmdphantomjscasperjs

提问by kapa

I'm using PhantomJs, CasperJs, and Js in a js file ran through the cmd.

我在通过 cmd 运行的 js 文件中使用 PhantomJs、CasperJs 和 Js。

Imagine we had two files(test1.js, and test2.js). Both files have a url/site variable that directs the test to a particular address. Everytime an environment changed or the target location changed, we would need to update this variable.

假设我们有两个文件(test1.js 和 test2.js)。这两个文件都有一个 url/site 变量,用于将测试定向到特定地址。每次环境改变或目标位置改变时,我们都需要更新这个变量。

To avoid having to update the files, I'd like to pass the values through the command line, as to where to test this.

为了避免更新文件,我想通过命令行传递值,以了解在哪里测试。

Is there a way to declare the string variable through the cmd as you run the file?

有没有办法在运行文件时通过 cmd 声明字符串变量?

E.g.:

例如:

casperjs test.js "var site='http://google.com';"

回答by kapa

The documentationsays you can pass command-line parameters.

文件说,你可以通过命令行参数。

CasperJS ships with a built-in command line parser on top of PhantomJS' one, located in the climodule; it exposes passed arguments as positional ones and named options

But no worries for manipulating the cli module parsing API, a Casper instance always contains a ready to use cliproperty, allowing easy access of all these parameters.

CasperJS 在 PhantomJS 的顶部带有一个内置的命令行解析器,位于cli模块中;它将传递的参数公开为位置参数和命名选项

但是不用担心操纵 cli 模块解析 API,一个 Casper 实例总是包含一个随时可用的cli属性,允许轻松访问所有这些参数。

Example code:

示例代码:

var casper = require("casper").create();

casper.echo("Casper CLI passed args:");
require("utils").dump(casper.cli.args);

casper.echo("Casper CLI passed options:");
require("utils").dump(casper.cli.options);

casper.exit();

Execution results:

执行结果:

$ casperjs test.js arg1 arg2 arg3 --foo=bar --plop anotherarg Casper

$ casperjs test.js arg1 arg2 arg3 --foo=bar --plop anotherarg Casper

CLI passed args: [
    "arg1",
    "arg2",
    "arg3",
    "anotherarg" ]
Casper CLI passed options: {
    "casper-path": "/Users/niko/Sites/casperjs",
    "cli": true,
    "foo": "bar",
    "plop": true }

回答by SharpCoder

on the command prompt say:

在命令提示符下说:

casperjs test file_name.js --port='123' --username='batman'

in the test script say:

在测试脚本中说:

casper.cli.get('port');
casper.cli.get('username');

回答by Grant Miller

Complete Intuitive Solution:

完整直观的解决方案:

Consider the following command:

考虑以下命令:

casperjs example.js true --foo=false

The string trueis a command line argument, while the name foois a command line option.

字符串true是命令行参数,而名称foo是命令行选项

The valueassigned to the name (variable) foois the string false.

分配给所述名称(变量)foo是字符串false

Command line argumentsare positional and must be accessed via the index of the argument.

命令行参数是位置参数,必须通过参数的索引来访问。

Command line optionsare named and must be accessed via the name of the option.

命令行选项已命名,必须通过选项名称访问。

In other words, you could look at arguments as similar to values in a numeric array, while options are similar to key/value pairs in an associative array.

换句话说,您可以将参数视为类似于数值数组中的值,而选项类似于关联数组中的键/值对。



Command Line Arguments

命令行参数

You can access the arguments using one of the following methods:

您可以使用以下方法之一访问参数:

casper.cli.get(0)             // Returns Boolean true   ; Slowest / Most Readable  (Opinion)
casper.cli.args[0]            // Returns Boolean true
casper.cli.raw.get(0)         // Returns String "true"
casper.cli.raw.args[0]        // Returns String "true"  ; Fastest / Least Readable (Opinion)

Command Line Options

命令行选项

You can access the options using one of the following methods:

您可以使用以下方法之一访问选项:

casper.cli.get('foo')         // Returns Boolean false  ; Slowest / Most Readable  (Opinion)
casper.cli.options['foo']     // Returns Boolean false
casper.cli.raw.get('foo')     // Returns String "false"
casper.cli.raw.options['foo'] // Returns String "false" ; Fastest / Least Readable (Opinion)


For other inquiries about CasperJS command line arguments or options, see the documentation: http://docs.casperjs.org/en/latest/cli.html

有关 CasperJS 命令行参数或选项的其他查询,请参阅文档:http://docs.casperjs.org/en/latest/cli.html

回答by Proximo

Found the answers too hard to understand at a glance. You can pass arg or option parameters.

发现答案太难一目了然。您可以传递 arg 或选项参数。

Example: Passing Options *(Using =is required)

示例:传递选项 *(=需要使用)

$ casperjs myscript.js --username=user --password=123
var casper = require('casper').create();
var username = casper.cli.options.username;
var password = casper.cli.options.password;
console.log(username + ':' + password); // user:123
casper.exit();

Example: Passing Args

示例:传递参数

$ casperjs myscript.js user 123
var casper = require('casper').create();
var username = casper.cli.args[0];
var password = casper.cli.args[1];
console.log(username + ':' + password); // user:123
casper.exit();

回答by Fanch

You could have one file with your global variables, then call them in the other files. Like that when you want to modify one variable, you have only one file to modify. Use phantom.injectJs(path/to/file) to call other files in your main files. It Works with phantomJS and slimerJS.

您可以拥有一个包含全局变量的文件,然后在其他文件中调用它们。就像当你想修改一个变量时,你只有一个文件可以修改。使用 phantom.injectJs(path/to/file) 调用主文件中的其他文件。它适用于 phantomJS 和 slimerJS。

Example :

例子 :

js_file

js_file

--variable.js--
var site='http://google.com';

js_file

js_file

--file1.js--
phantom.injectJs( 'variable.js');
casper.start(site, function(){
    ...
});

js_file

js_file

--file2.js--
phantom.injectJs( 'variable.js');
casper.thenOpen(site, function(){
    ...
});