ExplicitWait – FluentWait

FluentWait allows us to wait for something till certain condition is satisfied, or till time out, whichever comes the first.

From java doc:

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.

Let’s take a look at all the factors that make up a fluentWait:

Figure 1: FluentWait

The condition we wait on is defined in until(..) method which takes a Function or Predicate as parameters.

Define a condition using Function or Predicate

What is a Function
What is a Function? An expression that takes some input and generates some output.

And how output is generated based on input? We define this in apply() method. An Function example:

Figure 2: a Function

What is a Predicate
Let’s take a look at its declaration:

public interface Predicate<T>
Determines a true or false value for a given input.

Predicate is similar to Function but simpler: It always return a Boolean while Function can return anything. An example:

Predicate<WebDriver> predicate = new Predicate<WebDriver>()
{ public boolean apply(WebDriver driver)
{ return false; }

Now feel .until part is much easier to code(:=)?

Utilize ExpectedConditions for condition-checking
To make our life easier, WebDriver has implemented most frequently used conditions for us in ExpectedConditions class. It contains methods to check one or a group of element(s)’s visibility, clickable status, attribute values etc etc. It also provides method to check frame and alert’s availability and many other methods. Check java doc here:

FluentWait Sample Usage

An example of waiting 30 seconds for an element to be present on the page, checking for its presence once every 5 seconds:

Wait wait = new FluentWait(driver)
.withTimeout(30, SECONDS)
.pollingEvery(5, SECONDS)

WebElement foo = wait.until(new Function() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id(“foo”));

You can also tell driver to ignore more than one type of exceptions while waiting/polling:

List<Class <? extends Exception>> exceptionsToIgnore = new ArrayList<Class <? extends Exception>>();

Wait wait = new FluentWait(driver)
.pollingEvery(1, TimeUnit.SECONDS)
. ignoreAll(exceptionsToIgnore);
WebElement element = null;
element = wait.until(new Function(){
public WebElement apply(WebDriver driver){
return driver.findElement(locator);
} });

FluentWait can take generic input other than WebDriver
Most time we pass in a WebDriver, but we don’t have to. We could use a WebElement, or a By, or anything we want really. Here is an example that checking on a count down of played time in an embedded player:

Figure 3: Player Bar

WebElement countUp = driver.findElement( By.id(“player_slideBar_timePlayed”));
new WebDriverWait(driver,10). until(ExpectedConditions.visibilityOf(countUp));
new FluentWait(countUp)
.withTimeout(10, TimeUnit.SECONDS)
.until(new Function() {
public Boolean apply(WebElement element) {
return element.getText().endsWith(“04”);
The code first waits for the player side bar ‘time played’ element to become visible using a WebDriverWait, then go further to wait till the track has been played for 4 seconds.

Q&A on ImplicitWait:

  • What we are waiting for?
    Wait for .until condition is satisfied
  • How long do we wait?
    It is defined explicitly in timeout parameter of .withTimeout() method
  • How frequently we check the result?
    Defined in pollingFrequency. Configurable
  • What we are checking?
    Per .until condition specified in the code
  • What to do if on show at the end?
  • When to use?
    When you try to test the presence of an element that may appear after every x seconds/minutes, and many other things like wait till an element’s property satisfy certain condition.

Leave a Reply

Your email address will not be published. Required fields are marked *