断言一个元素是重点 [英] Asserting an element is focused

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

问题描述

根据 我如何断言一个元素被聚焦? 线程,您可以通过切换到 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 以解决承诺,因此诸如 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');
})

你可以说,为什么不解决一次承诺呢?但是还有嵌套的承诺.

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 是否具有相同的底层 webelements:

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天全站免登陆