Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

What are the types of wait commands in Selenium Webdriver?

Muhammad Usman Ghani

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

If we want to master using Selenium webdriver, one of the most important skills to learn is using the wait commands. They are essential for executing test scripts and help identify and resolve issues related to the time lag in web elements.

When a web page loads, different web elements (buttons, links, images) that someone wants to interact with may load at different intervals.

Selenium webdriver offers the following three commands to implement waits in tests.

  • Implicit wait
  • Explicit wait
  • Fluent wait

Implicit wait

Implicit wait directs the selenium webdriver to wait for a particular time before throwing an exception. Once this time is set, webdriver will wait for the element before the exception occurs.

Importing

We must import the following package to add implicit wait in our script.

import java.util.concurrent.TimeUnit;
Code to import required library

Syntax

The following code sets an implicit wait in our test script.

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Code to implement an implicit wait

Code

Following is an example to show how we can use implicit wait.

public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://www.google.com";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
Code example of implementing implicit wait

Explanation

  • Line 1: We create a function to implement implicit wait.
  • Line 2: We initialize driver.
  • Line 3: This is the URL of the webpage on which we want to implement implicit wait.
  • Line 4: We set an implicit wait of 30 seconds.

Explicit wait

Using the explicit wait statement, we can instruct webdriver to wait until a particular condition occurs before continuing to execute code.

If we set the default wait command, the browser will wait the same time before loading each web element. Implicit wait causes an unnecessary delay in the execution of the test script.

The explicit wait can only use explicit wait for specified elements. However, it is an improvement on the implicit wait because it allows the program to pause for dynamically loaded ajax elements.

Importing

We must import the following package to add explicit wait in our script.

import org.openqa.selenium.support.ui.ExpectedConditions
import org.openqa.selenium.support.ui.WebDriverWait
Code to import required library

Syntax

The following code sets an explicit wait in our test script.

WebDriverWait wait = new WebDriverWait(driver,30);
Code to implement an explicit wait

Code

The following example shows how we can use an explicit wait.

public void setup() throws InterruptedException {
driver=new FirefoxDriver();
driver.get("https://gmail.com");
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'COMPOSE')]")));
}
Code example of implementing explicit wait

Explanation

  • Line 1: Create a function to implement implicit wait.
  • Line 2: Initialization of the driver element.
  • Line 3: Open the webpage we want to implement explicit wait.
  • Line 4: Initialization of the wait element.
  • Line 5: Set explicit wait of visibilityOfElementLocated with a timeout of 30 seconds.

Fluent wait

Fluent wait in Selenium indicates that the maximum time Selenium webdriver has to wait before a particular condition (web element) becomes visible. It also defines how often the webdriver will check to see if the condition occurs before throwing an error, Elementnotvisibleexception.

Simply put, fluent wait repeatedly searches for a web element at regular intervals until a timeout occurs or until the object is found.

The fluent wait commands are most valuable when interacting with web elements that may take longer to load. This happens a lot in ajax applications.

Code

The following example shows how we can use a fluent wait.

FluentWait wait = new FluentWait(driver);
wait.withTimeout(5000, TimeUnit.MILLISECONDS);
wait.pollingEvery(250, TimeUnit.MILLISECONDS);
wait.ignoring(NoSuchElementException.class)
wait.until(ExpectedConditions.alertIsPresent());
Code example of implementing fluent wait

Explanation

  • Line 1: We declare and initialize a fluent wait.
  • Line 2: We specify the timeout of the wait to 5000 milliseconds (5 seconds).
  • Line 3: We specify the polling time as 250 milliseconds.
  • Line 4: We specify what exceptions to ignore.
  • Line 5: We specify the condition to wait on.

RELATED TAGS

java

CONTRIBUTOR

Muhammad Usman Ghani
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring