断言元素被聚焦 [英] Asserting an element is focused

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

问题描述

根据我如何断言一个元素是有焦点的? 线程,您可以通过切换到 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.

推荐答案

在我的回答中,我将假设 activeElempageElem 都是量角器元素查找器,并且指向同一个网络元素.

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

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

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