Selenium Wait Commands

Selenium Wait commands role is very important while executing Selenium tests. Let’s see different wait commands such as “Implicit“, and “Explicit” wait commands in selenium.

Why do we need Waits in Selenium ?

While executing scripts, sometimes we may face an exception “ElementNotVisibleException“. This exception appears when there is a delay in loading time of the web elements which we are going to interact. To overcome this issue we need to use Wait Commands. By Using the Selenium Wait Commands, our script will wait till the web elements is load for certain time before continuing with the next step.

Different Types of Selenium Wait Commands are:

  1. Implicit Wait
  2. Explicit Wait

Implicit Wait:

The Implicit wait tells to the WebDriver to wait for certain amount of time before it throws an exception. Once we set the time, WebDriver will wait for the element based on the time we set before it throws an exception. The default setting is zero.

Note: Implicit Wait is in place for the entire time the browser is open. It is applied in global level or we can say it is applied to all the elements in the script.

Syntax

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

Test script with an explanation

package gcReddyClass;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ImplicitWaitCommands {

	static WebDriver driver;

	public static void main(String[] args) {
		System.setProperty("webdriver.chrome.driver","F:\\chromedriver.exe");
		// To create a new instance of chrome Driver
		driver = new ChromeDriver();
		driver.manage().window().maximize();
		// Implicit Wait - Here the specified Implicit Wait time frame is 10 seconds.
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		// Navigate to Orange HRM login page
		driver.get("https://opensource-demo.orangehrmlive.com/index.php/auth/login");
		// get the actual value of the title
		String aTitle = driver.getTitle();		
		// compare the actual title with the expected title
		if (aTitle.equals("OrangeHRM")) {
			System.out.println("Test Passed");
		} else {
			System.out.println("Test Failed");
		}
		// close browser
		driver.close();

	}
}

Explicit Wait:

The explicit wait is used to tell the Web Driver to wait for certain Conditions or the maximum time exceeded before throwing an “ElementNotVisibleException” exception.

Explicit wait is of two types:

  1. WebDriverWait
  2. FluentWait

WebDriverWait In Selenium :

It is applied on certain element with defined expected condition and time. This wait is only applied to the specified element. If it is not interacted with element within specified time then it will through exception.

Syntax

//WebDriverWait wait = new WebDriverWait(WebDriverRefrence,TimeOut);
WebDriverWait wait = new WebDriverWait (driver, 20);
wait.until(ExpectedConditions.<ExpectedConditionsName>(WebElement);

Test script with an explanation

package gcReddyClass;

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;

public class ExplicitWaitsCommands {

	static WebDriver driver;

	public static void main(String[] args) {
		System.setProperty("webdriver.chrome.driver","F:\\chromedriver.exe");
		// To create a new instance of chrome Driver
		driver = new ChromeDriver();
		driver.manage().window().maximize();

		// This waits up to 15 seconds before throwing a TimeoutException
		WebDriverWait wait = new WebDriverWait(driver, 15);

		// Navigate to Orange HRM login page
		driver.get("https://opensource-demo.orangehrmlive.com/index.php/auth/login");

		// get the actual value of the title
		String aTitle = driver.getTitle();

		wait.until(ExpectedConditions.titleIs("OrangeHRM"));

		// close browser
		driver.close();

	}
}

FluentWait In Selenium :

FluentWait can define the maximum amount of time to wait for a specific condition as well as the frequency with which we want to check the condition  before throwing an “ElementNotVisibleException” exception.

Let’s consider we took time out value as 20 seconds and polling frequency as 5 seconds. The maximum amount of time is (20 seconds) to wait for a condition and the frequency (5 seconds) to check the success or failure of a specified condition. If the element is located with in this time frame it will perform the operations else it will throw an “ElementNotVisibleException

Syntax

Wait wait = new FluentWait(WebDriver reference)
.withTimeout(timeout, SECONDS)
.pollingEvery(timeout, SECONDS)
.ignoring(Exception.class);
 
WebElement foo=wait.until(new Function<WebDriver, WebElement>() {
public WebElement applyy(WebDriver driver) {
return driver.findElement(By.id("foo"));
}
});

Test script with an explanation

package gcReddyClass;

import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;

public class FluentWaitCommands {
	static WebDriver driver;

	public static void main(String[] args) {

		System.setProperty("webdriver.chrome.driver","F:\\chromedriver.exe");
		// To create a new instance of chrome Driver
		driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.get("https://opensource-demo.orangehrmlive.com/index.php/auth/login");
		Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
				.withTimeout(20, TimeUnit.SECONDS)
				.pollingEvery(5, TimeUnit.SECONDS)
				.ignoring(NoSuchElementException.class);

		WebElement btnLogin = wait.until(new Function<WebDriver, WebElement>() {
			public WebElement apply(WebDriver driver) {
				WebElement element = driver.findElement(By.xpath("//span[contains(text(),'( Username : Admin | Password : admin123 )')]"));
				String getTextOnPage = element.getText();
				if (getTextOnPage.contains("Username")) {
					System.out.println(getTextOnPage+" :- FluentWait Passed");
					return element;
				} else {
					System.out.println("FluentWait Failed");
					return null;
				}
			}
		});
          driver.close();
	}
}

Next

Leave a comment

Design a site like this with WordPress.com
Get started