主张的元件被聚焦 [英] Asserting an element is focused

查看:194
本文介绍了主张的元件被聚焦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照怎样断言元素集中<? / A>线程,你可以检查元素是否被转换到侧重 activeElement(),并断言这就是你预期有焦点的相同元素:

<$p$p><$c$c>expect(page.element.getAttribute('id')).toEqual(browser.driver.switchTo().activeElement().getAttribute('id'));

在我的情况下,目前主要集中的元素没有一个 ID 属性即可。

我应该做的,而不是检查一个的id是什么

奖金的问题:此外,你可以看到从我试图解决它,它看起来像我不能指望/断言元素(或网页元素)作为一个完整的对象。为什么呢?


我试过:

 预期(page.element).toEqual(browser.driver.switchTo()activeElement());

但与一个错误我甚至无法理解失败 - 有一个巨大的回溯(这是约10分钟在控制台滚动),但里面没有用户友好的错误

我也试过用 getWebElement()

 预期(page.element.getWebElement())toEqual(browser.driver.switchTo()activeElement()。)。

但是,这将导致以下错误:


  

错误:期待调用WebElement的说法,预期承诺。难道
  你的意思是使用.getText()?


使用最新的量角器开发版本。


解决方案

在我的答案,我要承担 activeElem pageElem 都是量角器元素发现者,并且都指向同一个网页元素。

首先回答您的问题关于为什么

 预期(activeElem).toEqual(pageElem);

获取进入一个无限循环,这是因为量角器修补茉莉花的期望断言之前解决的承诺,让喜欢东西期望(activeElem.getText ())toEqual(文本); 工作而不必做

  activeElem.getText()。然后(函数(文本){
  期待(文本).toEqual(文);
})

您可能会说,为什么不解决这个承诺一次?但后来有嵌套的承诺。

所以,现在你可能会想这是一个问题,但它确实是不是因为你永远不会在一个真正的用例比较两个elementFinders。茉莉的toEqual做一个参考检查,并没有深刻的比较,所以期待(activeElem).toEqual(pageElem),仅仅是同一个简单的参考对比:(activeElem === pageElem).toToTruthy(),和真的没有一点这样做。 (注元素(by.css('HTML'))===元素(by.css('HTML'))是假的,因为它是不一样的参考。)

因此​​,要回答的真正问题这个主题:如何查看两个elementFinders有相同的底层webelements:

 预期(activeElem.getId())toEqual(pageElem.getId());

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'));

In my case, the currently focused element does not have an id attribute.

What should I do instead of checking an id?

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?


I've tried:

expect(page.element).toEqual(browser.driver.switchTo().activeElement());

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.

I've also tried to use getWebElement():

expect(page.element.getWebElement()).toEqual(browser.driver.switchTo().activeElement());

But this resulted into the following error:

Error: expect called with WebElement argument, expected a Promise. Did you mean to use .getText()?

Using the latest protractor development version.

解决方案

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());

这篇关于主张的元件被聚焦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆