Asserting an element is focused

后端 未结 7 864
傲寒
傲寒 2020-12-16 18:50

According to the How do I assert an element is focused? thread, you can check if an element is focused by switching to an activeElement() and assert this is the

相关标签:
7条回答
  • 2020-12-16 19:56

    In my answer I'm going to assume activeElem and pageElem are both protractor element finders, and are pointing to the same web element.

    First to answer your question about why

    expect(activeElem).toEqual(pageElem);
    

    Gets into an infinite loop, it's because protractor patched jasmine's expect to resolve the promise before asserting, so that things like expect(activeElem.getText()).toEqual('text'); works without having to do

    activeElem.getText().then(function(text) {
      expect(text).toEqual('text');
    })
    

    You could say, why not just resolve the promise once? But then there are nested promises.

    So now you might be thinking this is an issue, but it really isn't because you would never compare two elementFinders in a real use case. Jasmine's toEqual does a reference check, and not a deep compare, so expect(activeElem).toEqual(pageElem), is just the same as a simple reference comparison: (activeElem === pageElem).toToTruthy(), and there's really no point doing that. (Note element(by.css('html')) === element(by.css('html')) is false because it's not the same reference.)

    So, to answer the real question for this thread: how to see if two elementFinders have the same underlying webelements:

    expect(activeElem.getId()).toEqual(pageElem.getId());
    
    0 讨论(0)
提交回复
热议问题