javascript - Filling a form in an Angular.js app using PhantomJS -


i want test angularjs application using phantomjs. first step fill login form username field (let's assume there no other fields).
(in real browser) field controlled angular (it has ng-model attribute) , whole form has ng-submit attribute.

so in phantomjs script do

  1. input username

    page.evaluate(function () {     document.getelementsbyname('username')[0].value = 'tester'; }); 
  2. take screenshot , see 'tester' present in input

    page.render('myfile.jpg'); 
  3. click on submit button:

    page.evaluate(function () {     document.getelementbyid('go').click(); }); 
  4. take screenshot.

what see happens button gets clicked, message displayed says value of username empty. guess model not updated.
tried set these values using jquery - same result.

how correctly input values fields controlled angular?

phantomjs provides sendevent function can used implement rudimentary sendkeys, need focus on element first:

function sendkeys(page, selector, keys){     page.evaluate(function(selector){         // focus on text element before typing         var element = document.queryselector(selector);         element.click();         element.focus();     }, selector);     page.sendevent("keypress", keys); } sendkeys(page, "*[name=username]", "tester"); 

you might want checkout either protractor seems tailored angular.js testing or casperjs builds on top of phantomjs nicer api.


Comments

Popular posts from this blog

android - Automated my builds -

how to proxy from https to http with lighttpd -

python - Flask migration error -