在我编写的测试中,如果我想断言页面上存在 WebElement,我可以做一个简单的操作:
driver.findElement(By.linkText("Test Search"));
如果存在就会通过,如果不存在就会爆炸。但现在我想断言链接确实 not 存在。我不清楚如何执行此操作,因为上面的代码不返回 boolean 值。
编辑这就是我想出自己的修复方法的方法,我想知道是否还有更好的方法。
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)");
}
}
最佳答案
这样做更容易:
driver.findElements(By.linkText("myLinkText")).size() < 1
关于java - 使用带有 java 的 Selenium WebDriver 断言 WebElement 不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3283310/