Home Guide Understanding Selenium Timeouts

Understanding Selenium Timeouts

By Neha Vaidya, Community Contributor -

Selenium is one of the most widely used tools for automation testing. Every command in Selenium Webdriver is an asynchronous operation.

What are Selenium Timeouts?

Consider a situation in which WebDriver fails to execute the test case as the webpage takes too long to load. In such cases, it is essential to mention the wait time for the page load to avoid test case failure. This is where Timeouts play an important role. They are used to set an interval of time between actions performed on the test.

Timeouts are usually performed using Selenium wait commands.

The various Selenium Webdriver timeouts used while testing an application are as follows:

  • implicitlyWait()
  • setScriptTimeout()
  • pageLoadTimeout()
  • Thread.sleep()

Let’s discuss each method in detail.

1. implicitlyWait()

This timeout is used to specify the amount of time the driver should wait while searching for an element if it is not immediately present.

The syntax is as follows:

implicitlyWait(long time, java.util.concurrent.TimeUnit unit);
  • time – The amount of time to wait for
  • unit – The unit of measure for time

When searching for a particular single element, the driver should pause page loading until the element has been found. If it doesn’t wait, then the timeout expires before throwing a NoSuchElementException.


Read article on Commons Exceptions in Selenium Webdriver: How to handle them?


When searching for multiple elements, the driver should pause the page until at least one element has been found or the timeout has expired.

Example:

implicitlyWait(20, TimeUnit.SECONDS);

In this statement, the WebDriver will wait for 20 seconds before proceeding to the next action.

Code Snippet:

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class TimeoutExample {
public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver", "Path of Chrome Driver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.ebay.com/");

// Implicit wait timeout for 20seconds
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

driver.findElement(By.xpath("//input[@id='gh-ac']")).sendKeys("Mobile");

//xpath for search button
WebElement search = driver.findElement(By.xpath("//input[@id='gh-btn']"));

search.click();

}
}

On executing the code above, the driver will wait for 20 seconds on the particular website even if the web element is not found.

Note: If a user wants to increase the implicit wait timeout, it should be done carefully as this will affect the test run time.

2. setScriptTimeout();

This is used to set the amount of time the WebDriver must wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, then the script will be allowed to run indefinitely.

Syntax:

setScriptTimeout(long time,java.util.concurrent.TimeUnit unit);
  • time – The timeout value.
  • unit – The unit of time

The default timeout for setScriptTimeout method is zero. If you do not set time, then there are chances that executeAsyncScript method may fail because the JavaScript code may take more than the time allotted. To avoid such failures, set the setScriptTimeout. This is mainly used for Javascript objects and executors.

Example:

// setScriptTimeout for 10 seconds
driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);
((JavascriptExecutor) driver).executeScript("alert('hello world');");
((JavascriptExecutor) driver).executeAsyncScript("window.setTimeout(arguments[arguments.length - 1], 500);");

In the example above, if the time is not used, then there will be an error stating: “Timed out waiting for async script result”. To avoid this error, one should use setScriptTimeout.

3. pageLoadTimeout in Selenium

This sets the time to wait for a page to load completely before throwing an error. If the timeout is negative, page loads can be indefinite.

Syntax:

pageLoadTimeout(long time,java.util.concurrent.TimeUnit unit);

This timeout is applicable only to driver.manage() and driver.navigate.to() methods.

Example:

public class PageLoadTest {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "Path of driver");
WebDriver driver = new ChromeDriver();
// set the time of 30 seconds for page to complete the loading

driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.get("https://www.google.com/");
}
}

In the code above, if your page does not load within 30 seconds, WebDriverException will be thrown.

Selenium Timeouts must be included to create effective, comprehensive and seamlessly running test cases. This article intends to help in this regard by briefly explaining how Timeouts work, and how they can be incorporated into Selenium test scripts.

To know more about Automation, refer to this piece on Selenium Automation.

BrowserStack Logo Run Selenium Tests on 2000+ Browsers & Devices Get Started Free