最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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

  1. input username

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

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

    page.evaluate(function () {
        document.getElementById('go').click();
    });
    
  4. 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

  1. input username

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

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

    page.evaluate(function () {
        document.getElementById('go').click();
    });
    
  4. 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 badges
Add a ment  | 

1 Answer 1

Reset to default 6

PhantomJS 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.

发布评论

评论列表(0)

  1. 暂无评论