Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
742 views
in Technique[技术] by (71.8m points)

angularjs - How do I assert an element is focused?

I am attempting to verify that the focused element is set on page load as one of my tests.

This seems to be working, and I can verify with the element explorer, but the Jasmine matchers don't seem to pick up on this.

Here's my code:

var LoginPage = function () {
    this.basePath = browser.params.baseUrl;
    this.loginPart = "/#/login";
    this.usernameInput = element(by.model('username'));

    this.get = function () { ... }
}

it('should focus on the username field on load', function () {
     loginPage.get();
     expect(loginPage.usernameInput).toBe(browser.driver.switchTo().activeElement());
});

The field itself is correctly getting focus when the page loads (and element explorer correctly allows me to query this via browser.driver.switchTo().activeElement(), so I think this test should be passing, but it isn't.

Instead I get an enormous stacktrace which doesn't offer any useful information.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think I found a workaround:

Since expect expects to be called with a promise, you can compare some attribute of the two webElements (your input and the currently activeElement) :

it('should focus on the username field on load', function () {
     loginPage.get();
     expect(loginPage.usernameInput.getAttribute('id')).toEqual(browser.driver.switchTo().activeElement().getAttribute('id'));
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...