Tru*_*lue 40 java assertion selenium-webdriver
在我编写的测试中,如果我想断言WebElement存在于页面上,我可以做一个简单的事情:
driver.findElement(By.linkText("Test Search"));
Run Code Online (Sandbox Code Playgroud)
如果它存在,它将通过,如果它不存在它将弹出.但现在我想断言的链接本身并不存在.我不清楚如何做到这一点,因为上面的代码不返回布尔值.
编辑这就是我提出自己修复的方法,我想知道是否还有更好的方法.
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)");
}
}
Run Code Online (Sandbox Code Playgroud)
小智 37
这样做更容易:
driver.findElements(By.linkText("myLinkText")).size() < 1
Run Code Online (Sandbox Code Playgroud)
Ser*_*rov 12
如果没有这样的元素org.openqa.selenium.NoSuchElementException,我认为你可以抓住它会被抛出driver.findElement:
import org.openqa.selenium.NoSuchElementException;
....
public static void assertLinkNotPresent(WebDriver driver, String text) {
try {
driver.findElement(By.linkText(text));
fail("Link with text <" + text + "> is present");
} catch (NoSuchElementException ex) {
/* do nothing, link is not present, assert is passed */
}
}
Run Code Online (Sandbox Code Playgroud)
不确定你指的是哪个版本的selenium,但selenium*中的一些命令现在可以这样做:http: //release.seleniumhq.org/selenium-core/0.8.0/reference.html
等等..
有一个名为ExpectedConditions:
By loc = ...
Boolean notPresent = ExpectedConditions.not(ExpectedConditions.presenceOfElementLocated(loc)).apply(getDriver());
Assert.assertTrue(notPresent);
Run Code Online (Sandbox Code Playgroud)