如何等待从 DOM 中删除元素? [英] How to wait for when an element is removed from DOM?

查看:22
本文介绍了如何等待从 DOM 中删除元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我尝试等到一个 DOM 元素从我的量角器测试正在测试的网页上的当前 DOM 树中删除时,我都会遇到这个问题.当我尝试等到一个 DOM 元素被另一个线程中的 user2912739 提供的这种好技术隐藏时,我已经掌握了它.

I run into this issue whenever I try to wait until an DOM element is removed from the current DOM tree on the web page that my protractor test is testing. I already got a hang of it when I try to wait until a DOM element becomes hidden with this nice technique offered by user2912739 in another thread.

var el = element(by.css('.your-css-class'));
return browser.wait(protractor.until.elementIsNotVisible(el));

这工作相当不错.但是,在等待从 DOM 树中删除元素时,.isDisplayed().isPresent() 或上述行似乎都不起作用.测试将继续运行,但看起来它正在尝试获取该元素但从未成功,因此它最终根据配置文件设置的超时超时.例如.这是日志.

This works pretty decent. However, when it comes to waiting for an element got removed from the DOM tree both .isDisplayed() and .isPresent() or the above lines do NOT seem to work. The test would continue run but it appears like it is trying to get that element but never succeeds so it eventually timed out based on the timeout setting the configuration file. For example. this is the log.

超时:在等待规范完成 30000 毫秒后超时

timeout: timed out after 30000 msec waiting for spec to complete

当您处理测试元素是否从 DOM 树中删除时,这种用例可能非常频繁,例如,当用户单击关闭该模态元素的操作时关闭并从页面中删除的模态,或您只想删除"以使其不再存在于页面上的元素.所以,在测试中,你只想在它从 DOM 树中移除后立即继续测试运行.

The use case of this can be quite frequent whenever you are dealing with testing if an element is removed from the DOM tree, for instance, a modal that is closed and removed from the page when user click actions that dismisses that modal element, or an element that you just want to "delete" so that it no longer exists on the page. So, in the test, you just want to continue the test run as soon as it is removed from DOM tree.

我搜索了量角器和网络驱动程序 api,似乎没有 api 可以完成这项工作.

I've searched through protractor and web driver api, and it seems there is no api that does this job.

推荐答案

不确定你从哪里得到 protractor.until,因为它不是核心库的一部分.这就是你使用量角器的方式:

not sure where you got protractor.until from as that's not part of the core library. This is how you would do it with protractor:

var el = element(by.css('.your-css-class'));
return browser.wait(function() {
  return el.isPresent().then(function(present) {
    return !present;
  })
});

一旦 feat(expectedConditions) 进入(可能是 protractor 1.7),您可以这样做:

Once feat(expectedConditions) is in (probably protractor 1.7), you can do:

var EC = protractor.ExpectedConditions;

var el = element(by.css('.your-css-class'));
return browser.wait(EC.not(EC.presenceOf(el)));

这篇关于如何等待从 DOM 中删除元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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