使用带有 Java 的 Selenium WebDriver 断言 WebElement 不存在 [英] Assert that a WebElement is not present using Selenium WebDriver with java
问题描述
在我编写的测试中,如果我想断言页面上存在 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 这就是我想出自己的修复方法的方式,我想知道是否还有更好的方法.
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,但是 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
等等.
这篇关于使用带有 Java 的 Selenium WebDriver 断言 WebElement 不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!