Total Pageviews

Friday 5 September 2014

Wait Staments - Fluent Wait

Sample usage:

  • Each FluentWait instance defines the maximum amount of time to wait for a condition,
  •  As well as the frequency with which to check the condition.
  •  Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.
Syntax to use in real time Project:

// Waiting 30 seconds for an element to be present on the page, checking
   // for its presence once every 5 seconds.
   Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
       .withTimeout(30, SECONDS)
       .pollingEvery(5, SECONDS)
       .ignoring(NoSuchElementException.class);

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


------------------------------------------------------------

Methods in  Detail

withTimeout :Sets how long to wait for the evaluated condition to be true. The default timeout is FIVE_HUNDRED_MILLIS

pollingEvery: Sets how often the condition should be evaluated.

In reality, the interval may be greater as the cost of actually evaluating a condition function is not factored in. The default polling interval is FIVE_HUNDRED_MILLIS.
  • ignoreAll

    public <K extends java.lang.Throwable> FluentWait<T> ignoreAll(java.util.Collection<java.lang.Class<? extends K>> types)
    Configures this instance to ignore specific types of exceptions while waiting for a condition. Any exceptions not whitelisted will be allowed to propagate, terminating the wait.
    Parameters:
    types - The types of exceptions to ignore.
    Returns:
    A self reference.
  • ignoring

    public FluentWait<T> ignoring(java.lang.Class<? extends java.lang.Throwable> firstType,
       java.lang.Class<? extends java.lang.Throwable> secondType)
    See Also:
    ignoreAll(Collection)
  • until

    public void until(com.google.common.base.Predicate<T> isTrue)
    Repeatedly applies this instance's input value to the given predicate until the timeout expires or the predicate evaluates to true.
    Parameters:
    isTrue - The predicate to wait on.
    Throws:
    TimeoutException - If the timeout expires.
  • timeoutException

    protected java.lang.RuntimeException timeoutException(java.lang.String message,
                                              java.lang.Throwable lastException)
    Throws a timeout exception. This method may be overridden to throw an exception that is idiomatic for a particular test infrastructure, such as an AssertionError in JUnit4.
    Parameters:
    message - The timeout message.
    lastException - The last exception to be thrown and subsequently suppressed while waiting on a function.
    Returns:
    Nothing will ever be returned; this return type is only specified as a convenience.

No comments:

Post a Comment