簡體   English   中英

WebDriver FluentWait-函數/謂詞是否可以引發異常?

[英]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 上面的代碼當前存在編譯錯誤,因為據我所知, FunctionPredicate均不允許拋出Exception ,並且沒有任何子類可以。

有什么辦法嗎? 是否存在某種形式的FunctionPredicate 引發異常,例如Java的CallableRunnable 如果沒有,我想我可能只需要編寫自己的版本的wait功能。 謝謝!

如果使自定義異常拋出擴展為RuntimeException而不是Excetion,則應該可以拋出該異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM