如何使用FluentWait的timeoutException方法?

时间:2014-09-02 20:17:08

标签: selenium selenium-webdriver

尝试使用fluentWait类的timeoutException方法时,

FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver);
wait.timeoutException("test timeoutException",new MyException("hello")); 

获得警告

The method timeoutException(String, Throwable) from the type FluentWait<WebDriver> is not visible

为什么这样?如何使用这种方法?

2 个答案:

答案 0 :(得分:0)

如果您查看FluentWait,您会注意到timeoutExcpetion具有受保护的访问权限,因此您无法像这样使用它。

我不确定你想要达到的目标,但你通常会像:

一样使用它
FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver);
wait.withTimeout(10, TimeUnit.SECONDS)
        .ignoring(NoSuchElementException.class)
        .pollingEvery(1, TimeUnit.SECONDS)
        .withMessage("Trololo")
        .until(ExpectedConditions.invisibilityOfElementLocated(By.id("id")));

答案 1 :(得分:0)

通过覆盖timeoutException方法,我们可以在等待超时时触发不同的异常。 请考虑以下代码,其中覆盖了timeoutException方法。

FluentWait<WebDriver> wait=new  FluentWait<WebDriver>(driver){
    @Override
    protected RuntimeException timeoutException(String Message,Throwable lastException)    
    {
        throw new MyException("A New Exception instead of TimeOutException");
    }
};   
wait.ignoring(NoSuchElementException.class)
    .withTimeout(2, TimeUnit.SECONDS);

WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
  public WebElement apply(WebDriver driver) {
    return driver.findElement(By.id("foo"));
  }
});

因此,当发生超时时,将抛出自定义异常而不是TimeoutException

com.example.tests.MyException: A New Exception instead of TimeOutException
at com.example.tests.sampleTest$1.timeoutException(sampleTest.java:40)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:228)
at com.example.tests.sampleTest.test(sampleTest.java:45)
相关问题