Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

casperjs passing params to evaluate fails

Tags:

casperjs

casper.then(function(){
 phone_number   = '7wqeqwe6';
 phone_password = 'Teqweqweqw34';

});



casper.thenEvaluate(function(phone,password) {

document.querySelector('input#myTMobile-phone').setAttribute('value',phone);
document.querySelector('input#myTMobile-password').setAttribute('value',password);

//  document.querySelector('form').submit();
}, { 

phone    : phone_number,
password : phone_password

});

this throws me

string(307) "[37;41;1mFAIL[0m ReferenceError: Can't find variable: phone_number

Is there a way to pass params to evaluate method?

like image 712
narek Avatar asked Nov 06 '12 15:11

narek


2 Answers

The other answers are pre 1.0. The preferred way is to pass along the arguments in line

Example

casper.evaluate(function(username, password) {
    document.querySelector('#username').value = username;
    document.querySelector('#password').value = password;
    document.querySelector('#submit').click();
}, 'sheldon.cooper', 'b4z1ng4');

http://docs.casperjs.org/en/latest/modules/casper.html#evaluate

like image 162
fracmak Avatar answered Nov 06 '22 18:11

fracmak


Try something like this:

var phone_number = '7wqeqwe6',
    phone_password = 'Teqweqweqw34';

casper.start('http://…');

casper.thenEvaluate(function(phone, password) {
    document.querySelector('input#myTMobile-phone').setAttribute('value', phone);
    document.querySelector('input#myTMobile-password').setAttribute('value', password);
    //  document.querySelector('form').submit();
}, {
    phone: phone_number,
    password: phone_password
});

Notes:

  1. a cool link on javascript scoping
  2. filling forms? there's an API for that
like image 23
NiKo Avatar answered Nov 06 '22 20:11

NiKo