2

I'm running CasperJS using Windows' command line and I would like to know how to get user's input using the command line.

I would like to ask the user for information during the script's execution or pass an argument before running the script, for example:

casperjs test test.js my_argument_here
Huangism
  • 16,278
  • 7
  • 48
  • 74
lucasfcosta
  • 285
  • 2
  • 10

3 Answers3

3

I've done more research about this subject and discovered that I can't create my own casper object inside the tester module.

Here's what I've done (just In case there's anyone with the same problem):

I included this on my test.js file:

var url = casper.cli.get("url")

And now I run it through the command line like this:

casperjs test test.js --url="http://google.com"

Thank you all for your responses.

lucasfcosta
  • 285
  • 2
  • 10
2

If you are using the test suite you probably want to just use an include file.

In file include.me.js

casper.my_params = {
      "arg1" : "val1",
      "arg2" : "val2"  // etc
};

Then on the command line run like this:

casperjs test file.js --includes=include.me.js 

casper.my_params.arg1 and casper.my_params.arg2 will be available


Clearly documented here using the cli object: http://docs.casperjs.org/en/latest/cli.html

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();
Hogan
  • 69,564
  • 10
  • 76
  • 117
  • I'm using CasperJS test module. I cannot instantiate a casper object this way. I get this error: Test file: portal.js Fatal: you can't override the preconfigured casper instance in a test environment. Docs: http://docs.casperjs.org/en/latest/testing.html#test-command-args-and-options – lucasfcosta Jul 28 '14 at 17:43
  • just take out the var line... the casper object already exists. – Hogan Jul 28 '14 at 17:45
  • I keep getting an ÔÜá┬á┬áCasperError: Invalid test path: arg1. I just added this ( require("utils").dump(casper.cli.args); ) to my casper start function (which was working properly before) and it isn't working yet. I'm trying to run it like this: casperjs test file.js arg1 arg2 – lucasfcosta Jul 28 '14 at 17:54
  • @lucasfcosta see use of `--includes` above – Hogan Jul 28 '14 at 18:08
0

casper.cli.options will provide that.

http://docs.casperjs.org/en/latest/cli.html

Paul
  • 35,689
  • 11
  • 93
  • 122