Waiting is a critical aspect of automation testing, especially when dealing with dynamic web pages or elements that take time to load. Selenium WebDriver Java provides different ways of waiting, including Implicit Wait, Explicit Wait, and Fluent Wait. In this article, Novatesting will discuss the reasons for using wait in Selenium Webdriver Java, the types of wait, and a training exercise with its answer.
I. When to use Wait?
Without waits, the Selenium WebDriver might execute the test scripts faster than the web page loads, leading to test failures or unexpected results. To handle these unpredictable delays and ensure that the WebDriver waits for the page to load before executing the next step, we use Wait.
II. Types of Wait:
There are three main types of Wait in Selenium WebDriver Java: Implicit Wait, Explicit Wait, and Fluent Wait.
- Implicit Wait: Implicit Wait sets a default wait time for all the elements in the entire test script. Once set, the implicit wait remains in effect for the lifetime of the WebDriver session. Syntax:
scss
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
Example:
scss
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.get("http://www.google.com");
WebElement element = driver.findElement(By.name("q"));
In this example, the implicit wait of 15 seconds will be set for the entire WebDriver session, and the findElement
method will wait for 15 seconds to find the “q” element. After 15 seconds, the NoSuchElementException
will emerge.
Notes on Implicit Wait:
- Implicit wait applies to all the elements, not just a single element.
- Implicit wait can slow down the test execution time.
2. Explicit Wait:
Explicit Wait is more flexible than Implicit Wait as it can be used only for specific elements, and it can be defined for a specific amount of time.
When to use explicit wait: Use explicit wait when you want to wait for a specific condition to occur before executing the next step in the test script.
Example of Explicit wait:
scss
WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 15);
driver.get("http://www.google.com");
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.name("q")));
In this example, we use the WebDriverWait
class and the until
method to wait for the “q” element to be clickable before executing the next step.
Expected conditions: Some typical expected conditions are:
- elementToBeClickable
- presenceOfElementLocated
- visibilityOfElementLocated
3. Fluent Wait:
Fluent Wait is a combination of Implicit Wait and Explicit Wait. It enables us to specify the maximum amount of time to wait for a specific condition to occur and the frequency with which the code should check for that condition.
When to use: Use Fluent Wait when you want to wait for a specific condition to occur and check for that condition periodically.
Example of Fluent Wait:
Consider the following example where we want to wait for a specific element to be present and visible on a page:
scss
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("element_id")));
In this example, we have created an instance of WebDriverWait
with a maximum wait time of 30 seconds. The until
method is used to specify the expected conditions that the element should meet. In this case, we have used ExpectedConditions.visibilityOfElementLocated
to wait for the visibility of an element with the id
attribute of “element_id”.
Fluent Wait allows us to customize the wait mechanism by setting the wait time, ignoring specific exceptions, polling frequency, and so on. This makes it a powerful tool for complex applications where elements may load dynamically.
III. Exercise and Answer for Wait in Selenium WebDriver Java
Consider the following scenario:
A user is searching for a specific product on an e-commerce website. The search results page takes time to load, and the user wants to wait until the page has loaded completely before verifying the product details.
Task:
- Write a test case using Fluent Wait to verify the product details on the search results page.
- Use the expected condition of
presenceOfElementLocated
for the product details. - Set a wait time of 15 seconds and a polling interval of 500 milliseconds.
Answer:
java
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.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import java.time.Duration;
import java.util.NoSuchElementException;
public class SearchTest {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.example.com/search?q=product");
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(Duration.ofSeconds(15))
.pollingEvery(Duration.ofMillis(500))
.ignoring(NoSuchElementException.class);
WebElement productDetails = wait.until(driver -> driver.findElement(By.id("productDetails")));
System.out.println(productDetails.getText());
driver.quit();
}
}
With the above discussion, Novatesting has provided some knowledge related to Wait. Understanding the different types of wait and when to use them is critical to successful automation testing using Selenium WebDriver Java.