Wait command in Selenium
If you want to become an expert at using Selenium WebDriver, one of the most important skills to master is the use of the Wait commands. They are essential for executing test scripts and help identify and resolve issues related to time lag in web elements.This article will offer a detailed description of how developers and testers can use the Wait function in Selenium. It will also break down Implicit, Explicit and Fluent Wait in order to provide clarity on when to use which function.
What is the Wait command in Selenium?
The wait functions are essential when it comes to executing Selenium tests. They help to observe and troubleshoot issues that may occur due to variation in time lag.
In automation testing, wait commands direct the test execution to pause for a certain length of time before moving onto the next step. This enables WebDriver to check if one or more web elements are present/visible/enriched/clickable, etc.
Why do users need Selenium Wait?
Most web applications are developed with Ajax and Javascript. When a page loads on a browser, the various web elements that someone wants to interact with may load at various time intervals.
This obviously creates difficulty in identifying any element. On top of that, if an element is not located then the “ElementNotVisibleException” appears. Selenium Wait commands help resolve this issue. Read more about the Common Exceptions in Selenium.
Selenium WebDriver provides three commands to implement wait in tests.
- Implicit Wait
- Explicit Wait
- Fluent Wait
Implicit Wait in Selenium
- Implicit Wait directs the Selenium WebDriver to wait for a certain measure of time before throwing an exception. Once this time is set, WebDriver will wait for the element before the exception occurs.
- Once the command is in place, Implicit Wait stays in place for the entire duration for which the browser is open. Its default setting is 0, and the specific wait time needs to be set by the following protocol.
To add implicit waits in test scripts, import the following package.
- import java.util.concurrent.TimeUnit;
Syntax
- driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Add the above code into the test script. It sets an implicit wait after the instantiation of the WebDriver instance variable.
Example Of Implicit Wait Command
Package waitExample;
- import java.util.concurrent.TimeUnit;
- import org.openqa.selenium.*;
- import org.openqa.selenium.firefox.FirefoxDriver;
- import org.testng.annotations.AfterMethod;
- import org.testng.annotations.BeforeMethod;
- import org.testng.annotations.Test;
- public class WaitTest {
- private WebDriver driver;
- private String baseUrl;
- private WebElement element;
- BeforeMethod
- public void setUp() throws Exception {
- driver = new FirefoxDriver();
- baseUrl = “http://www.google.com”;
- driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
- }
- Test
- public void testUntitled() throws Exception {
- driver.get(baseUrl);
- element = driver.findElement(By.id(“lst-ib”));
- element.sendKeys(“Selenium WebDriver Interview questions”);
- element.sendKeys(Keys.RETURN);
- List<WebElement> list = driver.findElements(By.className(“_Rm”));
- System.out.println(list.size());
- }
- AfterMethod
- public void tearDown() throws Exception {
- driver.quit();
- }
- }
However, implicit wait increases test script execution time. It makes each command wait for the defined time before resuming test execution. If the application responds normally, implicit wait can slow down the execution of test scripts.
Here is a Comprehensive Guide to Run your Selenium Test using BrowserStack Automate
Explicit Wait in Selenium
By using the Explicit Wait command, the WebDriver is directed to wait until a certain condition occurs before proceeding with executing the code.
- Setting Explicit Wait is important in cases where there are certain elements that naturally take more time to load. If one sets an implicit wait command, then the browser will wait for the same time frame before loading every web element. This causes an unnecessary delay in executing the test script.
- Explicit wait is more intelligent, but can only be applied for specified elements. However, it is an improvement on implicit wait since it allows the program to pause for dynamically loaded Ajax elements.
In order to declare explicit wait, one has to use “ExpectedConditions”. The following Expected Conditions can be used in Explicit Wait.
- alertIsPresent()
- elementSelectionStateToBe()
- elementToBeClickable()
- elementToBeSelected()
- frameToBeAvaliableAndSwitchToIt()
- invisibilityOfTheElementLocated()
- invisibilityOfElementWithText()
- presenceOfAllElementsLocatedBy()
- presenceOfElementLocated()
- textToBePresentInElement()
- textToBePresentInElementLocated()
- textToBePresentInElementValue()
- titleIs()
- titleContains()
- visibilityOf()
- visibilityOfAllElements()
- visibilityOfAllElementsLocatedBy()
- visibilityOfElementLocated()
To use Explicit Wait in test scripts, import the following packages into the script.
- import org.openqa.selenium.support.ui.ExpectedConditions
- import org.openqa.selenium.support.ui.WebDriverWait
- Then, Initialize A Wait Object using WebDriverWait Class.
- WebDriverWait wait = new WebDriverWait(driver,30);
Here, the reference variable is named <wait> for the <WebDriverWait> class. It is instantiated using the WebDriver instance. The maximum wait time must be set for the execution to layoff. Note that the wait time is measured in seconds.
Example of Explicit Wait Command
In the following example, the test script is for logging into “gmail.com” with a username and password. After a successful login, the code waits for the “compose” button to be available on the home page. Finally, it clicks on the button.
package waitExample;
- import java.util.concurrent.TimeUnit;
- import org.openqa.selenium.By;
- import org.openqa.selenium.Keys;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.firefox.FirefoxDriver;
- import org.openqa.selenium.support.ui.ExpectedConditions;
- import org.openqa.selenium.support.ui.WebDriverWait;
- import org.testng.annotations.AfterMethod;
- import org.testng.annotations.BeforeMethod;
- import org.testng.annotations.Test;
- public class ExpectedConditionExample {
- // created reference variable for WebDriver
- WebDriver driver;
- BeforeMethod
public void setup() throws InterruptedException { // initializing driver variable using FirefoxDriver driver=new FirefoxDriver(); // launching gmail.com on the browser driver.get(“https://gmail.com”); // maximized the browser window
- public void setup() throws InterruptedException {
- // initializing driver variable using FirefoxDriver
- driver=new FirefoxDriver();
- // launching gmail.com on the browser
- driver.get(“https://gmail.com”);
- // maximized the browser window
- driver.manage().window().maximize();
- driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
- }
driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); }
- Test
- public void test() throws InterruptedException {
- // saving the GUI element reference into a “element” variable of WebElement type
- WebElement element = driver.findElement(By.id(“Email”));
- // entering username
- element.sendKeys(“dummy@gmail.com”);
- element.sendKeys(Keys.RETURN);
- // entering password
- driver.findElement(By.id(“Passwd”)).sendKeys(“password”);
- // clicking signin button
- driver.findElement(By.id(“signIn”)).click();
- // explicit wait – to wait for the compose button to be click-able
- WebDriverWait wait = new WebDriverWait(driver,30);
- wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“//div[contains(text(),’COMPOSE’)]”)));
- // click on the compose button as soon as the “compose” button is visible
- driver.findElement(By.xpath(“//div[contains(text(),’COMPOSE’)]”)).click();
- }
- AfterMethod
- public void teardown() {
- // closes all the browser windows opened by web driver
- driver.quit();
- }
- }
The above code instructs Selenium WebDriver to wait for 30 seconds before throwing a TimeoutException. If it finds the element before 30 seconds, then it will return immediately. After that, it will click on the “Compose” button. In this case, the program will not wait for the entire 30 seconds, thus saving time and executing the script faster.
Fluent Wait in Selenium
The Fluent Wait command defines the maximum amount of time for Selenium WebDriver to wait for a certain condition to appear. It also defines the frequency with which WebDriver will check if the condition appears before throwing the “ElementNotVisibleException”.
- To put it simply, Fluent Wait looks for a web element repeatedly at regular intervals until timeout happens or until the object is found.
- Fluent Wait commands are most useful when interacting with web elements that can sometimes take more time than usual to load. This is largely something that occurs in Ajax applications.
- While using Fluent Wait, it is possible to set a default polling period as needed. The user can configure the wait to ignore any exceptions during the polling period.
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 apply(WebDriver driver) {
- return driver.findElement(By.id(“foo”));
- }
- });
Example of Fluent Wait Command
- Wait wait = new FluentWait<WebDriver>(driver)
- .withTimeout(50, TimeUnit.SECONDS)
- .pollingevery(3, TimeUnit.SECONDS)
- .ignoring(NoSuchElementException.class);
This command operates with two primary parameters: timeout value and polling frequency. The above code defines time out value as 50 seconds and polling frequency as 3 seconds. It directs WebDriver to wait for a maximum of 50 seconds to verify a specific condition. If the condition occurs during those 50 seconds, it will perform the next step in the test script. If not, it will throw an “ElementNotVisibleException”.