WebDriver Wait, Implicit Wait, Explicit Wait and Fluent Wait

By Raman| on 29/12/2014 | 415 Comments

Selenium WebDriver: - Synchronizing a Test Using Waits

One of the Important Factor in test automation for a complex web application is to ensure that the flow of the test cases should be in synchronization with the application under test (AUT).

When tests are run, the application may not always respond with the same speed. For example, it might take a few seconds for a progress bar to reach 100 percent, a status message to appear, a button to become enabled, and a window or pop-up message to open. We can handle these anticipated timing problems by synchronizing our test to ensure that Selenium WebDriver waits until our application is ready before performing a certain action. There are several options that we can use to synchronize our test. 


Synchronizing a test with an implicit wait

When an implicit wait is implemented it tells WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0.Once set the implicit wait is set for the life of the WebDriver object's instance. However, an implicit wait may slow down our tests when an application responds normally, as it will wait for each element to appear in the DOM and increase the overall execution time.

driver.manage().timeouts().implicitlyWait(Time period, TimeUnit.SECONDS);

Time period: Here time value is given as input. How many seconds the driver has to wait.
TimeUnit.SECONDS: Time period is measured as second here. You can use other time unit like day, microseconds, nanoseconds etc.

Example: -

Synchronizing a test with FluentWait

When a FluentWait instance is implemented it 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.


public void test(){

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)

       .withTimeout(30, SECONDS)

       .pollingEvery(5, SECONDS)



   WebElement foo = wait.until(new Function<WebDriver, WebElement>() {

     public WebElement apply(WebDriver driver) {

       return driver.findElement(By.id("Element"));




Above code snippet will Wait 30 seconds for an element to be present on the page and check for its presence once every 5 seconds.


Synchronizing a test with an Explicit wait


When an Explicit Wait is implemented it provides a better control when compared with an implicit wait. Unlike an implicit wait, we can write custom code or conditions for wait before proceeding further in the code. An explicit wait can only be implemented in cases where synchronization is needed and the rest of the script is working fine. The Selenium WebDriver provides WebDriverWait and ExpectedCondition classes for implementing an explicit wait. The ExpectedCondition class provides a set of predefined conditions to wait before proceeding further in the code.

The following are some common conditions that we frequently come across when automating web browsers supported by the ExpectedCondition class:- 

    Predefined condition        Selenium method

An element is visible and enabled 

elementToBeClickable(By locator)

An element is selected 

elementToBeSelected(WebElement element)

Presence of an element 

presenceOfElementLocated(By locator)

Specific text present in an element

textToBePresentInElement(By locator,

java.lang.String text)


How it Works:-

We can create a wait for a set of common conditions using the ExpectedCondition class. First, we need to create an instance of the WebDriverWait class by passing the driver instance and timeout for a wait as follows:


WebDriverWait wait = new WebDriverWait(driver, 10);

Next, ExpectedCondition is passed to the wait.until() method as follows:



Note: - The WebDriverWait object will call the ExpectedCondition class object every 500 milliseconds until it returns successfully.


Example: -



public void test(){

WebDriverWait wait = new WebDriverWait(driver, 10);

wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Element")));    driver.findElement(By.id("Element")).clear();    





Custom-expected condition: WebDriver provide us to create custom wait condition.




import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.support.ui.ExpectedCondition;

import org.openqa.selenium.support.ui.WebDriverWait;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.Test;


public class testwait {


       private WebDriver driver;

       private String baseUrl;



       public void setUp() throws Exception {

             driver = new FirefoxDriver();

             baseUrl = "http://www.flipkart.com";





       public void testUntitled() throws Exception {



             // custom explicit wait for search field

             new WebDriverWait(driver, 10)

                           .until(new ExpectedCondition<WebElement>() {


                                 public WebElement apply(WebDriver d) {

                                        return d.findElement(By.id("fk-top-search-box"));












       public void tearDown() throws Exception {





NO! Comments Found

Be The first to comment on this*