断言元素被聚焦

2023-06-15前端开发问题
8

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

问题描述

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

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

The End

相关推荐

Fatal error: Call to a member function fetch_assoc() on a no
业务场景:使用update语句去更新数据库字段。 原因:update接收值不正确。原来代码: $query = "UPDATE student SET date = now() WHERE id = $id";$result = $mysqli-query($query2) or die($mysqli-error); // 问题出现了在这句 $data = $result-fetch_ass...
2024-12-13 前端开发问题
136

layui实现laydate日历控件控制之前日期不可选择
具体实现代码如下: laydate.render({ elem: '#start_time', min:0, //,type: 'date' //默认,可不填}); 只要加一个min参数,就可以控制了。0表示之前的日期不可...
2024-11-29 前端开发问题
133

如何使用百度地图API获取地理位置信息
首先,我们需要在百度地图开放平台上申请一个开发者账号,并创建一个应用。在创建应用的过程中,我们会得到一个密钥(ak),这是调用API的凭证。 接下来,我们需要准备一个PHP文件,以便可以在网页中调用。首先,我们需要引入百度地图API的JS文件,代码如下...
2024-11-22 前端开发问题
244

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156