Java 最佳实践-通用webdriverWait,直到存在加载弹出窗口,如果出现错误,则测试失败
我的问题是我有很多方法,比如:Java 最佳实践-通用webdriverWait,直到存在加载弹出窗口,如果出现错误,则测试失败,java,selenium-webdriver,error-handling,webdriver,Java,Selenium Webdriver,Error Handling,Webdriver,我的问题是我有很多方法,比如: public void waitForLoading(WebElement loadingElement, WebElement errorElement) { long timeOut = Long.parseLong(PropertyReader.getInstance().getProperty("DEFAULT_TIME_OUT")); try { WebDriverWait wait = new WebDriver
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方法。我做了一次搜索,但有几个案子需要处理。错误消息出现->测试失败。等待加载内容,然后它消失,->检查搜索结果
如果我想连续检查出现的一些错误消息元素,并且在这种情况下抛出异常,那么如何扩展此代码?所以我可以独立处理超时异常和另一个错误消息
此sript因IF而失败。ErrorElement没有出现在页面上,--->NoTouchElementException我看到的最简单的方法是:
public void waitForLoading() {
long timeOut = Long.parseLong(PropertyReader.getInstance().getProperty("DEFAULT_TIME_OUT"));
try {
WebDriverWait wait = new WebDriverWait(DriverFactory.getInstance().getDriver(), timeOut);
if (!wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("wait_element")));)
{
throw new NoSuchElementException();
}
} catch (TimeOutException e) {
System.out.println("Timed out after " + timeOut + "seconds waiting for loading the results.");
}
}
您可以根据需要捕获不同的异常。在您的情况下,您希望捕获
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
}
}
public void waitForLoading(){
long timeOut=long.parseLong(…);
试一试{
WebDriverWait wait=新建WebDriverWait(…);
等待。直到(预期的条件。不可见性感觉定位(…);
如果(){
抛出新的CustomErrorMessageAppearedException();
}
}捕获(超时异常e){
System.out.println(“在…之后超时”);
}捕获(CustomErrorMessageAppearedException e){
//处理错误消息
}
}
这是最好的解决方案,因为它同时处理错误消息出现选项。非常感谢。我需要问一些问题。问题是,测试失败了,没有任何异常:在这里,if似乎不是一个好的选择。有什么建议吗?不接受回答再问一个问题不是个好主意。如果您还有其他问题,请创建一个新问题并引用此问题/答案。