简体   繁体   English

使用 webdriver 和 selenium 验证元素不存在

[英]Verify an element is not existing using webdriver and selenium

I'm using WebdriverIO and selenium-standalone to write automated tests that will verify that various parts of our user interface are working.我正在使用WebdriverIOselenium-standalone编写自动化测试,以验证我们用户界面的各个部分是否正常工作。

I need to verify that an element is not present on the page.我需要验证页面上不存在元素。 For example, our system allows staff to track various types of resources that we are referring clients to.例如,我们的系统允许员工跟踪我们向客户推荐的各种类型的资源。 If a staff member accidentally adds the wrong resource, they can delete it, and I want to verify that the resource was actually deleted and is not present on the page.如果工作人员不小心添加了错误的资源,他们可以删除它,我想验证该资源是否确实被删除并且不在页面上。

WebdriverIO has an .isExisting() property, but no way to check if something is not existing (or not visible/present). WebdriverIO 有一个.isExisting()属性,但无法检查某些内容是否不存在(或不可见/不存在)。 I could also use Chai assertions to figure this out, but haven't delved into that world yet.我也可以使用Chai断言来解决这个问题,但还没有深入研究那个世界。

Here's a snippet of my code:这是我的代码片段:

it('I can delete a resource from a need', function() {
    return driver
    .moveToObject('span.ccx-tasklist-task') // Hover mouse over resource
    .click('div.referral-controls a.btn.dropdown-standalone') // Click Resource drop-down
    .click('div.referral-controls.ccx-dropdown-menu-selected li > a') // Delete Resource
    .pause(2000);
    // Need to Verify that resource was deleted here

Any advice?有什么建议吗? Let me know if you need more information.如果您需要更多信息,请与我们联系。

您可以将reverse选项设置为truewaitForExist

.waitForExist( '[id$=OpenNeedsPanel] div.commodities', 500, true )

我能够验证页面上不存在这样的元素:

 .isExisting('[id$=OpenNeedsPanel] div.commodities').should.eventually.equal(false);

you can simply use the below mentioned line of code您可以简单地使用下面提到的代码行

int temp=driver.findElements(By.xpath("your x-path expression")).size();

you can even replace your xpath locator with your other locators as well like id,class,link etc.你甚至可以用你的其他定位器以及 id、class、link 等替换你的 xpath 定位器。

Now, if the value of temp>0, it means , your element exists现在,如果 temp>0 的值,则意味着,您的元素存在

You can refer https://webdriver.io/docs/api/element/waitForExist.html你可以参考https://webdriver.io/docs/api/element/waitForExist.html

Wait for an element for the provided amount of milliseconds to be present within the DOM.等待元素在 DOM 中出现提供的毫秒数。 Returns true if the selector matches at least one element that exists in the DOM, otherwise throws an error.如果选择器与 DOM 中存在的至少一个元素匹配,则返回 true,否则抛出错误。 If the reverse flag is true, the command will instead return true if the selector does not match any elements.如果反向标志为真,则如果选择器不匹配任何元素,该命令将改为返回真。

resource.waitForExist(1000, true);

where 1000 is the timeout in ms.其中 1000 是以毫秒为单位的超时时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Selenium Webdriver验证电子邮件 - How to verify EMail using Selenium Webdriver 如何使用 Selenium WebDriver 检查元素是否进入视图? - How to check if an element is into view using Selenium WebDriver? 如何使用 JavaScript 单击 Selenium WebDriver 中的元素? - How to click an element in Selenium WebDriver using JavaScript? 如何与使用Selenium Webdriver隐藏的元素进行交互? - How to interact with an element that is hidden using Selenium Webdriver? 如何使用selenium webdriver找到伪元素的父母? - How do I find parents of pseudo element using selenium webdriver? 无法使用Selenium WebDriver单击SVG路径元素 - Not able to click an svg path element using selenium webdriver 如何使用Selenium WebDriver单击列表中的特定元素? - How to click on particular element in a list using selenium webdriver? Selenium-WebDriver 如何使用 javascript 和 firefox 浏览器突出显示元素 - Selenium-WebDriver how to highlight element using javascript and firefox browser 使用Selenium WebDriver和JavaScript从XPath找到Element的文本 - Get text from XPath located Element using Selenium WebDriver with JavaScript 处理动态更改的元素ID-使用Selenium WebDriver - Handling dynamically changing element id - using selenium webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM