1

With casperjs, how can i add variable to fill form function and the send the variable externally via terminal.

I list the code i have for form fill below. I want to be able to add variables to it then pass these via command line. Any help would be appreciated.

casper.thenOpen(url2, function(){
    this.fill('form[name="LoginForm"]', {
        'username': 'var1here',
        'password': 'var2here' },
        true);
});

Once i have the variable setup, how do i pass these via command line?

Thanks in advance

Brice Favre
  • 1,511
  • 1
  • 15
  • 34
Cyber-C0ding
  • 67
  • 2
  • 13
  • I'm not sure to understand. You want to pass arguments via the command line to your Casper script, right? [Go read the doc](http://docs.casperjs.org/en/latest/cli.html) – moins52 Jan 27 '14 at 23:26

2 Answers2

1

Another way is to use named parameters :

var userinput = casper.cli.get("userinput");
var passinput = casper.cli.get("passinput");

And call your script like this :

casperjs test.js --userinput=test --passinput=test

In your script you will be able to test if the params is set like this

if (casper.cli.has("userinput")) {
}

if (casper.cli.has("passinput")) {
}

More information about casper.cli there => http://docs.casperjs.org/en/latest/cli.html

Brice Favre
  • 1,511
  • 1
  • 15
  • 34
0

I achieved this and got it working by adding the following snippets of code into the right place in my casperjs script :

var userinput = casper.cli.get(0)
var passinput = casper.cli.get(1)

Then i just set my fill form section like this :

casper.thenOpen(url, function(){
this.fill('form[name="LoginForm"]', {
    'username': userinput ,   <-- this being (0) if you use this function remove this-->
    'password': passinput ,  <-- this being (1) if you use this function remove this-->
true);
});
Cyber-C0ding
  • 67
  • 2
  • 13
  • @BriceFavre can you help me with this >>> http://stackoverflow.com/questions/21563640/php-execution-phantom-js-works-but-casperjs-does-not-work-permission-denied – Cyber-C0ding Feb 04 '14 at 22:35