[英]WebDriver FluentWait - can the Function/Predicate throw an Exception?
我正在將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
? 如果沒有,我想我可能只需要編寫自己的版本的wait功能。 謝謝!
如果使自定義異常拋出擴展為RuntimeException而不是Excetion,則應該可以拋出該異常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.