How to Use Different Types of Wait Commands in Selenium WebDriver?

About: Soumya.D is 43-year-old Author and Professor who enjoys working and writing on different topics. She writes articles for technology enthusiasts.

Waits support the testers to rectify/readjust issues while redirecting to various web pages by refreshing the complete web page and reloading the new elements of the webpage. At certain events, there will be Ajax call-requests too. So, a time delay can be seen whilst reloading the web pages and the web elements to be reflected.

Testing developers are often seen traversing through various web pages to and fro. Then, the navigate() method comes in handy, presented by the WebDriver helps the tester to simulate the real-time scenarios by navigating between the web applications referring to the browser’s history.

WebDriver provides the tester with two categories of waits to handle the frequent recurring page loads, web element loads, the display of tabs, alerts, error prompts and appearance of web elements.

As we know, a web application uses AJAX and Javascript. When a web application is loaded using the browser, the web element that we want to interact with may load at a later point of time, as several elements are set with some priority at the backend, hence, they load at different time intervals.

It makes it difficult for the driver to identify the web element and if the web element is not located it’ll throw “ElementNotFoundException” exception. This issue can be solved by using the Waits.

Types of Selenium Waits

Mainly there are two most used types of waits in Selenium Automation Testing:

  1. Implicit Wait
  2. Explicit Wait

Let us now move ahead and discuss each one of them in detail:

  1. Implicit Wait: The implicit wait will tell the driver to wait for a certain amount of time before it throws a "No Such Element Exception". The implicit wait is used to set the default waiting time(say 10 seconds) between consecutive test commands/methods in the entire program. The driver will pause the implementation of the subsequent test method and will wait for 10 seconds after the previous test method is executed.

Library to be imported:

import java.util.concurrent.TimeUnit;

Syntax:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

After the instantiation of WebDriver variable instance, include the above line of code to set an implicit wait in your script. The implicit wait contains two values as arguments. The first parameter contains the value of the time in number for which the system is required to wait(here, 10). The second parameter contains the time measurement scale(here as “seconds”).

Below is a code snippet for Implicit Wait:

package example;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;

public class implicitWaitTest {

WebDriver driver;

@BeforeTest

public void launchBowser() {

System.setProperty("webdriver.chrome.driver","C:\\Users\\Intellipaat-Team\\Downloads\\driver\\chromedriver.exe");

driver = new ChromeDriver();

driver.get("http://www.newtours.demoaut.com/");

}

@Test

public void selWait() {

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS); driver.findElement(By.xpath("//input[@name='userName']")).sendKeys("donutsandcupcakes"); driver.findElement(By.xpath("//input[@name='password']")).sendKeys("donutsandcupcakes"); driver.findElement(By.xpath("//input[@name='login']")).click();

}

@AfterTest

public void tearDown() {

driver.quit();

}

}

2. Explicit Wait: Explicit waits are used to pause the testing-execution until a specific condition is met or the maximum time set has passed. Explicit Waits are implemented as waiting time for a specific instance only, unlike implicit waits.

Selenium uses the WebDriverWait and ExpectedConditions classes to implement wait in the program.

Library to be imported:

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

Object instantiation for WebDriverWait Class:

WebDriverWait waitVariable = new WebDriverWait(driver, 10);

The above syntax is an instantiation of WebDriverWait class variable waitVariable. The first parameter includes the value of the WebDriver instance variable which will halt the program, and the second parameter include the maximum wait time measured in seconds.

Expected Condition:

waitVariable.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@name='password']"))).sendKeys("donutsandcupcakes");

The above syntax waits for an expected condition to happen or waits for a specific amount of time, whichever occurs or goes first. In our example below, we wait for the password element with xpath “//input[@name='password']” to be visible and loaded as a part of the home page load and thus, then we move ahead with calling the click command on the “login” button.

package example;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

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

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

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;

public class explicitWait {

WebDriver driver;

@BeforeTest

public void launchBrowser() {

System.setProperty("webdriver.chrome.driver","C:\\Users\\Intellipaat-Team\\Downloads\\driver\\chromedriver.exe");

driver = new ChromeDriver();

driver.get("http://www.newtours.demoaut.com/");

}

@Test

public void selExpWait() {

WebDriverWait waitVariable = new WebDriverWait(driver, 10); driver.findElement(By.xpath("//input[@name='userName']")).sendKeys("donutsandcupcakes"); waitVariable.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@name='password']"))).sendKeys("donutsandcupcakes");

driver.findElement(By.xpath("//input[@name='login']")).click();

}

@AfterTest

public void tearDown() {

driver.quit();

}

}

Various kinds of Expected Conditions used in Explicit Wait:

ExpectedConditions class allows us with scenarios where we have to wait for a condition to happen before executing the actual test step. The ExpectedConditions class includes a broad variety of expected conditions that can be located with the help of the WebDriverWait reference variable and until() method.

  1. visibilityOf()
  2. visibilityOfAllElements()
  3. visibilityOfAllElementsLocatedBy()
  4. visibilityOfElementLocated()
  5. alertIsPresent()
  6. titleIs()
  7. titleContains()
  8. frameToBeAvaliableAndSwitchToIt()
  9. invisibilityOfTheElementLocated()
  10. invisibilityOfElementWithText()
  11. textToBePresentInElement()
  12. textToBePresentInElementLocated()
  13. textToBePresentInElementValue()
  14. presenceOfAllElementsLocatedBy()
  15. presenceOfElementLocated()
  16. elementSelectionStateToBe()
  17. elementToBeClickable()
  18. elementToBeSelected()

Implicit, and Explicit Wait are the different types of waits used in Selenium. Usage of these waits are totally based on the elements which are loaded at different intervals of time. Using Thread.Sleep() is not usually suggested while Testing the web-application. If you are interested to learn more concepts of Selenium on a much deeper level and want to become a professional in the testing domain, check out Selenium certification training course!

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1:

Be the First to Share

    Recommendations

    • Instrument Contest

      Instrument Contest
    • Make it Glow Contest

      Make it Glow Contest
    • Reuse Contest

      Reuse Contest

    Discussions