protractor: test for focus of field

房东的猫 提交于 2021-02-08 05:25:16

问题


How can I test if an input field is infocus with protractor? I'm doing this:

it('should focus email field', function(){
    expect(element(by.model('login.email')).getAttribute('id')).toEqual(browser.driver.switchTo().activeElement().getAttribute('id'));
});

This seems to work with chrome, but this test fails with firefox. Any ideas?

This is the failure message:

[firefox #1]   2) Login page should focus email field
[firefox #1]    Message:
[firefox #1]      Expected 'login' to equal ''.
[firefox #1]    Stacktrace:
[firefox #1]      Error: Failed expectation
[firefox #1]     at [object Object].<anonymous> (/Users/Jason/Desktop/app/test/e2e/login-spec.js:38:69)
[firefox #1] 

Here's my test script:

describe('Login page', function() {
    var loginURL = 'http://localhost:8090/app/#/login';
    var loginEmailField = 'login-email';

    beforeEach(function() {
        browser.get(loginURL);
    });

    it('should focus email field', function(){     
        expect(element(by.id(loginEmailField)).getAttribute('id')).toEqual(browser.driver.switchTo().activeElement().getAttribute('id'));
    });
});

回答1:


You need to wait for angular before making any assertions:

beforeEach(function() {
    browser.get(loginURL);
    browser.waitForAngular();
});

Aside from that, this might be connected to the autofocus attribute, it has/had issues in Firefox, see:

  • Autofocus Attribute of HTML5 does not work only in FireFox when <Form><input> are loaded via Ajax. WHY?
  • Why doesn't autofocus=“autofocus” work in Mozilla Firefox?


来源:https://stackoverflow.com/questions/27767553/protractor-test-for-focus-of-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!