How do I assert an element is focused?

前端 未结 3 1927
囚心锁ツ
囚心锁ツ 2020-12-09 10:18

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

相关标签:
3条回答
  • 2020-12-09 11:03

    I managed to find a way to compare an element with the focused one without relaying to any attributes:

    element(by.css('div.my-class')).equals(browser.driver.switchTo().activeElement())
        .then(equals => expect(equals).toBe(true));
    
    0 讨论(0)
  • 2020-12-09 11:07

    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'));
    });
    
    0 讨论(0)
  • 2020-12-09 11:09

    I ended up using this nice simple function to assert focus.

    var expectToBeFocused = function(element){
      return expect(element.getInnerHtml()).
        toBe(browser.driver.switchTo().activeElement().getInnerHtml());
    };
    
    0 讨论(0)
提交回复
热议问题