I want to test AngularJS application using PhantomJS. First step is to fill login form with username
field (let's assume there are no other fields).
Normally (in real browser) this field is controlled by Angular (it has ng-model
attribute) and the whole form has ng-submit
attribute.
So in PhantomJS script I do
input username
page.evaluate(function () { document.getElementsByName('username')[0].value = 'tester'; });
take screenshot and see that 'tester' is present in input
page.render('myfile.jpg');
click on submit button:
page.evaluate(function () { document.getElementById('go').click(); });
take another screenshot.
What I see happens is that button gets clicked, but the message is displayed which says that the value of username
is empty. So I guess the model was not properly updated.
I also tried to set these values using jQuery - same result.
How do I correctly input values into fields controlled by angular?
I want to test AngularJS application using PhantomJS. First step is to fill login form with username
field (let's assume there are no other fields).
Normally (in real browser) this field is controlled by Angular (it has ng-model
attribute) and the whole form has ng-submit
attribute.
So in PhantomJS script I do
input username
page.evaluate(function () { document.getElementsByName('username')[0].value = 'tester'; });
take screenshot and see that 'tester' is present in input
page.render('myfile.jpg');
click on submit button:
page.evaluate(function () { document.getElementById('go').click(); });
take another screenshot.
What I see happens is that button gets clicked, but the message is displayed which says that the value of username
is empty. So I guess the model was not properly updated.
I also tried to set these values using jQuery - same result.
How do I correctly input values into fields controlled by angular?
Share Improve this question edited Dec 20, 2015 at 18:12 Artjom B. 62k26 gold badges135 silver badges230 bronze badges asked May 16, 2014 at 14:19 xaxaxaxa 1,1592 gold badges28 silver badges55 bronze badges1 Answer
Reset to default 6PhantomJS provides the sendEvent
function which can be used to implement a rudimentary sendKeys
, but you need to focus on the element first:
function sendKeys(page, selector, keys){
page.evaluate(function(selector){
// focus on the 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 to checkout either protractor which seems to be tailored for Angular.js testing or CasperJS which builds on top of PhantomJS with a much nicer API.