T
- The input type for each condition used with this instance.public class FluentWait<T> extends java.lang.Object implements Wait<T>
Wait
interface that may have its timeout and polling interval
configured on the fly.
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.
Sample usage:
// 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")); } });
This class makes no thread safety guarantees.
Modifier and Type | Field and Description |
---|---|
private Clock |
clock |
static Duration |
FIVE_HUNDRED_MILLIS |
private java.util.List<java.lang.Class<? extends java.lang.Throwable>> |
ignoredExceptions |
private T |
input |
private Duration |
interval |
private java.util.function.Supplier<java.lang.String> |
messageSupplier |
private Sleeper |
sleeper |
private Duration |
timeout |
Constructor and Description |
---|
FluentWait(T input) |
FluentWait(T input,
Clock clock,
Sleeper sleeper) |
Modifier and Type | Method and Description |
---|---|
<K extends java.lang.Throwable> |
ignoreAll(java.util.Collection<java.lang.Class<? extends K>> types)
Configures this instance to ignore specific types of exceptions while waiting for a condition.
|
FluentWait<T> |
ignoring(java.lang.Class<? extends java.lang.Throwable> exceptionType) |
FluentWait<T> |
ignoring(java.lang.Class<? extends java.lang.Throwable> firstType,
java.lang.Class<? extends java.lang.Throwable> secondType) |
FluentWait<T> |
pollingEvery(long duration,
java.util.concurrent.TimeUnit unit)
Sets how often the condition should be evaluated.
|
private java.lang.Throwable |
propagateIfNotIgnored(java.lang.Throwable e) |
protected java.lang.RuntimeException |
timeoutException(java.lang.String message,
java.lang.Throwable lastException)
Throws a timeout exception.
|
<V> V |
until(java.util.function.Function<? super T,V> isTrue)
Repeatedly applies this instance's input value to the given function until one of the following
occurs:
the function returns neither null nor false,
the function throws an unignored exception,
the timeout expires,
the current thread is interrupted
|
FluentWait<T> |
withMessage(java.lang.String message)
Sets the message to be displayed when time expires.
|
FluentWait<T> |
withMessage(java.util.function.Supplier<java.lang.String> messageSupplier)
Sets the message to be evaluated and displayed when time expires.
|
FluentWait<T> |
withTimeout(long duration,
java.util.concurrent.TimeUnit unit)
Sets how long to wait for the evaluated condition to be true.
|
public static final Duration FIVE_HUNDRED_MILLIS
private final T input
private final Clock clock
private final Sleeper sleeper
private Duration timeout
private Duration interval
private java.util.function.Supplier<java.lang.String> messageSupplier
private java.util.List<java.lang.Class<? extends java.lang.Throwable>> ignoredExceptions
public FluentWait(T input)
input
- The input value to pass to the evaluated conditions.public FluentWait<T> withTimeout(long duration, java.util.concurrent.TimeUnit unit)
FIVE_HUNDRED_MILLIS
.duration
- The timeout duration.unit
- The unit of time.public FluentWait<T> withMessage(java.lang.String message)
message
- to be appended to default.public FluentWait<T> withMessage(java.util.function.Supplier<java.lang.String> messageSupplier)
messageSupplier
- to be evaluated on failure and appended to default.public FluentWait<T> pollingEvery(long duration, java.util.concurrent.TimeUnit unit)
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
.
duration
- The timeout duration.unit
- The unit of time.public <K extends java.lang.Throwable> FluentWait<T> ignoreAll(java.util.Collection<java.lang.Class<? extends K>> types)
K
- an Exception that extends Throwabletypes
- The types of exceptions to ignore.public FluentWait<T> ignoring(java.lang.Class<? extends java.lang.Throwable> exceptionType)
exceptionType
- exception to ignoreignoreAll(Collection)
public FluentWait<T> ignoring(java.lang.Class<? extends java.lang.Throwable> firstType, java.lang.Class<? extends java.lang.Throwable> secondType)
firstType
- exception to ignoresecondType
- another exception to ignoreignoreAll(Collection)
public <V> V until(java.util.function.Function<? super T,V> isTrue)
until
in interface Wait<T>
V
- The function's expected return type.isTrue
- the parameter to pass to the ExpectedCondition
org.openqa.selenium.TimeoutException
- If the timeout expires.private java.lang.Throwable propagateIfNotIgnored(java.lang.Throwable e)
protected java.lang.RuntimeException timeoutException(java.lang.String message, java.lang.Throwable lastException)
message
- The timeout message.lastException
- The last exception to be thrown and subsequently suppressed while waiting
on a function.