Waits in Selenium automation

When we automate the test, there are two components such as the software application (web element) that is to be tested and the test automation tool (web driver)that is used for executing the test.

Both these components will have their own speed and the test scripts should be written facilitating both these components to work with the same speed. If they have not worked with the same speed the chances  for “Element Not Found” errors or ‘ElementNotVisibleException’ are more. In such cases synchronization (waits) will help both the components to work with the same speed. When we consider the synchronization in Selenium Webdriver, there are two different types of wait as: 

  • Implicit Wait
  • Explicit Wait :
    • WebDriver Wait()
    • Fluent Wait()

Implicit Wait

The Implicit Wait in Selenium is used to tell the web driver to wait for a certain amount of time before it throws a “No Such Element Exception”. The default setting is 0. Once we set the time, the web driver will wait for any element (where we use findElement() method) for that time before throwing an exception. (also known as Global wait).

Syntax

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

Same with the other waits like scriptTimeout and pageLoadTimeout :-

driver.manage().timeouts().scriptTimeout(Duration.ofMinutes(2));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
Note: Polling frequency is 500 ms

Explicit Wait

The Explicit Wait in Selenium is used to tell the Web Driver to wait for certain conditions (Expected Conditions) or maximum time exceeded before throwing “ElementNotVisibleException” exception. It applied only for specified web element, Instead of waiting for all the statements in the program. It gives better options than implicit wait as it waits for dynamically loaded Ajax elements. (also known as local wait).

Syntax

WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("locator")))
Note: Polling frequency is 500 ms

Fluent Wait

The Fluent Wait in Selenium is used to define maximum time for the web driver to wait for a condition, as well as the frequency with which we want to check the condition before throwing an “ElementNotVisibleException” exception. It checks for the web element at regular intervals until the object is found or timeout happens. (also known as polling time).

Let’s consider a scenario where an element is loaded at different intervals of time. The element might load within 10 seconds, 20 seconds or even more then that if we declare an explicit wait of 20 seconds. It will wait till the specified time before throwing an exception. In such scenarios, the fluent wait is the ideal wait to use as this will try to find the element at different frequency until it finds it or the final timer runs out.

Syntax

    Wait<WebDriver> fluentWait = new FluentWait<WebDriver>(driver)
        .withTimeout(Duration.ofSeconds(30))
        .pollingEvery(Duration.ofSeconds(5))
        .ignoring(NoSuchElementException.class);

What is polling in implicit wait?

The implicit wait is done until the element is found or the maximum time has elapsed. But for fallbacks, implicit wait in Selenium comes with a feature called polling. With polling, implicit wait keeps searching for the element at regular intervals till the time it finds the element or the maximum time has elapsed.

Which is the best type of Selenium wait?

There is no “best” type of Selenium wait when it comes to testing a web application. Different use cases demand different types of wait conditions and therefore ask for appropriate commands.

Share the Knowledge

You May Also Like

About the Author: Sariful I.

Leave a Reply

Your email address will not be published. Required fields are marked *