casperjs passing params to evaluate fails

narek picture narek · Nov 6, 2012 · Viewed 9.9k times · Source
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?

Answer

NiKo picture NiKo · Nov 10, 2012

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