我正在将 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 的vs. ?如果没有,我想我可能只需要编写自己的等待功能版本。谢谢!CallableRunnable