问题描述
在我写的测试中,如果我想断言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 *中的一些命令现在可以这样做:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!