【发布时间】:2016-04-07 15:46:29
【问题描述】:
当找不到元素的 xpath 时,我希望在 selenium 中抛出自定义错误消息 我创建了一个类
package NewPackage;
public class ExplicitAssertionError extends AssertionError {
private static final long serialVersionUID = 1L;
public ExplicitAssertionError (String msg) {
super(msg);
}
}
我写过这样的代码来测试一个页面
public class ResultPage extends WDBase {
public void assertTravelNamePresent(final String busName) {
final String xpath="//div[@class='company'][contains(.,'"+busName+"')]";
if (!driver.findElement(By.xpath(xpath)).isDisplayed()) {
throw new ExplicitAssertionError("invalid text found");
}
}
}
在这里,当传递了与页面的 xpath 不匹配的无效文本时,将抛出 NoSuchElementException 而不是错误消息。 在找不到元素的情况下,任何人都可以为我提供使用 assertTrue/False 使测试用例失败的解决方案 谢谢
【问题讨论】:
标签: java firefox xpath selenium-webdriver