断言使用Selenium WebDriver和java不存在WebElement [英] Assert that a WebElement is not present using Selenium WebDriver with java

查看:195
本文介绍了断言使用Selenium WebDriver和java不存在WebElement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我写的测试中,如果我想断言WebElement存在于页面上,我可以做一个简单的事情:

In tests that I write, if I want to assert a WebElement is present on the page, I can do a simple:

driver.findElement(By.linkText("Test Search"));

如果它存在则会通过,如果它不存在则会弹出。但现在我想断言链接存在。我不清楚如何做到这一点,因为上面的代码没有返回布尔值。

This will pass if it exists and it will bomb out if it does not exist. But now I want to assert that a link does not exist. I am unclear how to do this since the code above does not return a boolean.

编辑这就是我想出自己的修复方法,我想知道是否还有更好的方法。

EDIT This is how I came up with my own fix, I'm wondering if there's a better way out there still.

public static void assertLinkNotPresent (WebDriver driver, String text) throws Exception {
List<WebElement> bob = driver.findElements(By.linkText(text));
  if (bob.isEmpty() == false) {
    throw new Exception (text + " (Link is present)");
  }
}


推荐答案

不确定你指的是哪种版本的硒,但是selenium *中的一些命令现在可以这样做:
http://release.seleniumhq.org/selenium-core/0.8.0/reference.html

Not Sure which version of selenium you are referring to, however some commands in selenium * can now do this: http://release.seleniumhq.org/selenium-core/0.8.0/reference.html


  • assertNotSomethingSelected

  • assertTextNotPresent

等。

这篇关于断言使用Selenium WebDriver和java不存在WebElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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