我正在扩展 Selenium 的 By
类进入更广泛Locator
类可以接受不同类型的位置标准,并将为我们的 SearchContext
版本提供新方法和/或 WebDriver
.
我有以下方法等待唯一元素存在、显示并启用:
public void waitForElementPresent(BSWebDriver driver, int timeoutSeconds) {
try {
FluentWait<BSWebDriver> wait = new FluentWait<BSWebDriver>(driver)
.withTimeout(timeoutSeconds, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
wait.until(new Function<BSWebDriver, Boolean>() {
public Boolean apply(BSWebDriver driver) {
return isElementPresent(driver);
}
});
} catch (TimeoutException timeoutEx) {
throw new WaitForElementException(this, timeoutSeconds,
WAIT_FOR.PRESENT);
}
}
isElementPresent
是一种处理元素是否存在(即它存在、显示和启用)的检查的方法,但无需等待。问题是isElementPresent
引发自定义错误 Exception
如果定位器不能唯一标识某个元素(即,如果返回多个与位置条件匹配的元素)。目前,上述代码中存在编译错误,因为据我所知,Function
都没有。也不Predicate
允许抛出 Exception
,并且没有任何子类可以这样做。
有什么办法可以做到这一点吗?是否有某种形式的 Function
或Predicate
可以抛出异常,就像 Java 的 Callable
与 Runnable
?如果没有,我想我可能只需要编写我自己的等待功能版本。谢谢!
最佳答案
如果您让自定义异常抛出扩展 RuntimeException 而不是 Excetion,您应该能够正常抛出它。
关于java - WebDriver FluentWait - 函数/谓词可以抛出异常吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13199224/