我的问题是我有 customWaitMethods,例如:
public void waitForLoading(WebElement loadingElement, WebElement errorElement) {
long timeOut = Long.parseLong(PropertyReader.getInstance().getProperty("DEFAULT_TIME_OUT"));
try {
WebDriverWait wait = new WebDriverWait(DriverFactory.getInstance().getDriver(), timeOut);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id(loadingElement.toString())));
if (errorElement.isDisplayed()) {
throw new TestException();
}
} catch (TimeoutException e) {
System.out.println("Timed out after default time out");
} catch (TestException e) {
System.out.println("Unexpected error occurred, environment error");
e.printStackTrace();
}
}
我需要一些通用的 customWait 方法。我进行了搜索,但需要处理几种情况。出现错误消息 -> 测试失败。等待加载内容,它就消失了,->查看搜索结果。
如果我想连续检查某些 error_message 元素是否出现,并且在这种情况下我会抛出异常,我该如何扩展此代码?那么我可以独立处理超时异常和其他错误消息吗?
由于 IF,此脚本失败。 ErrorElement没有出现在页面上,---> nosuchelementException
最佳答案
您可以根据需要捕获
不同的异常
。在您的情况下,您希望catch
TimeoutException
来处理超时。然后捕获
不同类型的异常来处理错误消息:
public void waitForLoading() {
long timeOut = Long.parseLong(...);
try {
WebDriverWait wait = new WebDriverWait(...);
wait.until(ExpectedConditions.invisibilityOfElementLocated(...));
if (<error-message-appears>) {
throw new CustomErrorMessageAppearedException();
}
} catch (TimeoutException e) {
System.out.println("Timed out after...");
} catch (CustomErrorMessageAppearedException e) {
// handle error message
}
}
关于java - 最佳实践 - 通用 webdriver 等待加载弹出窗口存在,如果出现错误则测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44788787/