問題描述
根據我如何斷言一個元素是有焦點的? 線程,您可以通過切換到 activeElement()
來檢查元素是否獲得焦點,并斷言這是您期望獲得焦點的同一元素:
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 same element you've expected to have the focus:
expect(page.element.getAttribute('id')).toEqual(browser.driver.switchTo().activeElement().getAttribute('id'));
在我的例子中,當前聚焦的元素沒有id
屬性.
In my case, the currently focused element does not have an id
attribute.
我應該怎么做而不是檢查 id
?
What should I do instead of checking an id
?
獎勵問題:此外,正如您從我嘗試解決它的嘗試中看到的那樣,看起來我不能期望/斷言一個元素(或 Web 元素)作為一個完整的對象.為什么?
Bonus question: Also, as you can see from my tries to solve it, it looks like I cannot expect/assert an element (or web element) as a complete object. Why?
我試過了:
expect(page.element).toEqual(browser.driver.switchTo().activeElement());
但是由于我什至無法理解的錯誤而失敗 - 有一個巨大的回溯(在控制臺中滾動大約需要 10 分鐘),但內部沒有用戶友好的錯誤.
But is is failing with an error I cannot even understand - there is a huge traceback (it is about 10 minutes to scroll in the console), but no user-friendly error inside.
我也嘗試過使用 getWebElement()
:
expect(page.element.getWebElement()).toEqual(browser.driver.switchTo().activeElement());
但這導致了以下錯誤:
錯誤:期望使用 WebElement 參數調用,期望一個 Promise.做過你的意思是使用 .getText()?
Error: expect called with WebElement argument, expected a Promise. Did you mean to use .getText()?
使用最新的量角器開發版本.
Using the latest protractor development version.
推薦答案
在我的回答中,我將假設 activeElem
和 pageElem
都是量角器元素查找器,并且指向同一個網絡元素.
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);
進入一個無限循環,這是因為量角器在斷言之前修補了jasmine的 expect
以解決promise,因此 expect(activeElem.getText()).toEqual('text');
無需執行即可工作
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');
})
你可以說,為什么不只解決一次承諾呢?但是還有嵌套的 Promise.
You could say, why not just resolve the promise once? But then there are nested promises.
所以現在您可能會認為這是一個問題,但實際上并不是因為您永遠不會在實際用例中比較兩個 elementFinder.Jasmine 的 toEqual 進行引用檢查,而不是深度比較,因此 expect(activeElem).toEqual(pageElem)
與簡單的引用比較相同:(activeElem === pageElem).toToTruthy()
,這樣做真的沒有意義.(注意 element(by.css('html')) === element(by.css('html'))
是假的,因為它不是同一個引用.)
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.)
所以,回答這個線程的真正問題:如何查看兩個 elementFinder 是否具有相同的底層 web 元素:
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());
這篇關于斷言元素被聚焦的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!