Selenium Wait Commands – Implicit, Explicit and Fluent Wait in Selenium

Complete guide on Selenium Wait Commands and cheat sheet.

It is very important to understand Selenium wait commands if you are trying to work on selenium automation. 

Why? Let’s see…

In Selenium test automation, the test execution is a RACE between the loading page and automation code execution. The webpage has to load before the execution of the related automation script statement for a successful test run.

If this doesn’t happen, your automation code just breaks!

So, if you want to leverage your test automation 100%, you have to master the concepts of selenium wait commands i.e. Implicit wait in Selenium, Explicit wait in Selenium and Fluent wait in Selenium.

Implicit wait in Selenium Explicit wait in Selenium, Fluent Wait in Selenium

These wait types are there to handle this synchronization issue in Selenium WebDriver. You can find the details and difference between each one of them below.

4 types of Selenium Wait command and their examples

Implicit Wait

//This is an example of implicit wait . This statement ensures that the execution waits for element to be visible for 30 seconds. 
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

This is how Implicit Wait in Selenium is used. It is written once and applicable throughout the same instance of webdriver during execution. The polling interval for finding the element is 500ms. If element is not located in specified wait time, after the timeout, it throws ElementNotVisibleException

Explicit Wait

//Explicit Wait
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'SUBMIT')]"));

This is how Explicit Wait in Selenium is used. It waits for certain condition to be met (condition as configured in the wait statement) so that execution finds the next WebElement to interact. It uses predefined methods of ExpectedConditions. The condition is checked by polling the page every 500ms. if condition is not met till the end of timeout period (30 seconds in the above example), it throws TimeoutException exception

Fluent Wait

This is the most advanced type of Selenium wait command. It is used to wait for an element but we can also ignore a defined exception.

//Fluent Wait

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
  .withTimeout(Duration.ofSeconds(30))
  .pollingEvery(Duration.ofSeconds(5))
  .ignoring(NoSuchElementException.class);

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

This is how Fluent wait in Selenium is used in automation script. It is a more detailed wait command which can be customized at very granular levels. Generally we don’t need such level of customization in our test automation.

In Fluent wait, we can configure Polling interval, exception to ignore as well as conditions specific to the WebElement.

In the above example: 

Timeout = 30 Seconds
Polling interval= 5 Seconds
Exception to ignore= NoSuchElementException

If element is not located, ElementNotVisibleException is thrown after timeout.

Thread.sleep in Selenium

Thread.sleep(5000)

Thread.sleep in Selenium is just a plain type of wait which if used, has to inserted before every WebElement operation. 

It is just a hard coded wait which doesn’t care if the element is loaded on the page during or at the end of the wait time. Execution just jumps to the next statement after completion of the defined wait time.

Similar Posts

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.