How do I assert an element is focused

0 votes

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.

Aug 8, 2018 in Selenium by GandalfDwhite
• 1,320 points
3,175 views

1 answer to this question.

0 votes

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'));
});
answered Aug 8, 2018 by Meci Matt
• 9,460 points

Related Questions In Selenium

0 votes
1 answer

How do I find an element that contains specific text in Selenium WebDriver (Python)?

Try the following: driver.find_elements_by_xpath("//*[contains(text(), 'My Button')]") I hope this ...READ MORE

answered Nov 27, 2020 in Selenium by Gitika
• 65,850 points
6,435 views
0 votes
1 answer

How can I get cssSelector of an element?

There are two ways as much as ...READ MORE

answered Dec 27, 2018 in Selenium by Trisha
699 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
13,630 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,119 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
7,207 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,859 views
0 votes
1 answer

How do I get current URL in Selenium Webdriver using Python?

Use current_url element. Example: print browser.current_url READ MORE

answered Aug 8, 2018 in Selenium by Meci Matt
• 9,460 points
25,987 views
0 votes
1 answer

How can I Install Selenium IDE in Firefox

Selenium IDE works with all Firefox versions, ...READ MORE

answered Apr 13, 2018 in Selenium by Meci Matt
• 9,460 points
3,732 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP