18

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

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.

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?

E.g.:

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

5 Answers5

24

The documentation says you can pass command-line parameters.

CasperJS ships with a built-in command line parser on top of PhantomJS’ one, located in the cli module; 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 cli property, allowing easy access of all these parameters.

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

CLI passed args: [
    "arg1",
    "arg2",
    "arg3",
    "anotherarg" ]
Casper CLI passed options: {
    "casper-path": "/Users/niko/Sites/casperjs",
    "cli": true,
    "foo": "bar",
    "plop": true }
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
kapa
  • 77,694
  • 21
  • 158
  • 175
  • Thank you. This is perfect. Great explanation. I guess I just didnt understand their explanation. Thank you for the in-depth info. –  Feb 13 '14 at 21:00
  • 1
    @AaronLoften Well, I must tell you my whole answer is copied from the documentation page I linked ;). – kapa Feb 13 '14 at 21:02
  • 1
    Why is require("utils") being called again and again? I'm asking this because I'm so used to the pattern where we load dependencies only once in a program. – rineez Aug 23 '16 at 04:36
  • @rineey I agree with you. I simply copied the example from the documentation. – kapa Aug 23 '16 at 08:36
12

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');
SharpCoder
  • 18,279
  • 43
  • 153
  • 249
3

Complete Intuitive Solution:

Consider the following command:

casperjs example.js true --foo=false

The string true is a command line argument, while the name foo is a command line option.

The value assigned to the name (variable) foo is the string false.

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

Command line options are 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

Community
  • 1
  • 1
Grant Miller
  • 27,532
  • 16
  • 147
  • 165
1

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.

Example :

js_file

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

js_file

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

js_file

--file2.js--
phantom.injectJs( 'variable.js');
casper.thenOpen(site, function(){
    ...
});
Fanch
  • 3,274
  • 3
  • 20
  • 51
1

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

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();
Proximo
  • 6,235
  • 11
  • 49
  • 67