Fluent Wait in Selenium

Learn via video courses
Topics Covered

Overview

Fluent Wait in Selenium is a feature that allows automation testers to wait for a certain condition to occur on a web page before proceeding with the test. Unlike the traditional 'hard wait' function, Fluent Wait is more flexible and customizable, allowing testers to set conditions such as polling intervals and exceptions to the wait time. This feature helps to improve the stability and reliability of automated tests, especially when dealing with dynamic web pages where elements may take some time to load or change.

Introduction

Wait statements ensure the script waits for a certain condition before proceeding to the next step. With wait statements, the script may try to interact with web elements that have yet to load, causing the script to fail. Wait statements help to overcome the asynchronous nature of web pages, where elements load at different speeds and times.

What are Wait Commands In Selenium?

Wait for Commands in Selenium allow automated testers to pause their test scripts for a specific period or until a particular condition is met. They are necessary because web pages may take a long time to load, and some elements may not be immediately available. Wait Commands come in different types, including Implicit Wait, Explicit Wait, and Fluent Wait, which provide more flexibility by allowing testers to set custom conditions for waiting. These Wait Commands ensure that the test script runs smoothly and without errors, even when dealing with dynamic web pages.

Why do Users Need Selenium to Wait for Commands?

Users need Selenium Wait commands for a few reasons:

  • To ensure that the test script does not fail due to timing issues. Web pages can take different amounts of time to load, and some elements may not be immediately available, causing errors in the test script. Wait commands help to avoid these issues by instructing the web driver to wait for a specified period or until a particular condition is met.
  • To improve the stability and reliability of automated tests. Wait for commands can help to make the tests more robust by ensuring that the script waits for elements to load before interacting with them, preventing errors and unexpected behavior.

Implement Waits in Tests with Selenium WebDriver

Here are some examples of how to implement Wait commands in tests with Selenium WebDriver:

Implicit Wait

Implicit Wait instructs the web driver to wait a certain amount before throwing a "NoSuchElementException" error if an element is not found. The implicit wait is set once for the entire duration of the web driver instance. Here's an example:

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

This code sets an implicit wait of 10 seconds. The script will throw an error if an element is not found within this time.

Explicit Wait

Explicit Wait allows testers to wait for a specific condition to be met before proceeding with the test script. This type of wait can be more precise than Implicit Wait. Here's an example:

This code waits for an element with the ID "movement" to be clickable for 20 seconds. If the element is not clickable within this time, the script will throw an error.

Fluent Wait

Fluent Wait provides more flexibility than Implicit and Explicit Wait by allowing testers to set conditions such as polling intervals and exceptions to the wait time. Here's an example:

This code waits for an element with the ID "movement" to be visible for up to 30 seconds, polling every 5 seconds. The script will throw an error if the element is not visible within this time. The Fluent Wait can also ignore specific exceptions thrown during the wait time.

Fluent Wait in Selenium

  • Fluent Wait is a type of wait command in Selenium that allows testers to wait for an element to meet certain conditions before proceeding with the test script.
  • It provides more control over the wait process by allowing testers to set custom conditions such as polling intervals and exceptions to the wait time.
  • Compared to Implicit and Explicit Wait, Fluent Wait offers more flexibility in managing timing issues in dynamic web pages.
  • To use Fluent Wait, a new Fluent Wait object is created with the web driver instance, and various conditions are set using methods such as withTimeout, pollingEvery, and ignoring.
  • The wait time and polling interval are set using the withTimeout and pollingEvery methods, respectively, while the ignoring method specifies any exceptions to ignore during the wait time.

Syntax of Fluent Wait in Selenium

Let's breakdown the syntax of Fluent Wait in Selenium:

  • Wait<WebDriver>: This creates a new Wait object that waits for a WebDriver instance.
  • new FluentWait<WebDriver>(driver): This creates a new Fluent Wait object that waits for a specific WebDriver instance.
  • withTimeout(Duration.ofSeconds(30)): This sets the maximum time to wait for a certain condition to be met. In this example, the wait time is set to 30 seconds.
  • polling every (Duration.ofSeconds(5)): This sets the polling interval, or the time interval, to wait between each check to see if the condition has been met. In this example, the interval is set to 5 seconds.
  • ignoring(NoSuchElementException.class): This specifies any exceptions to ignore during the wait.

Differential Features Of Fluent Wait

Let's look into some key differential features of Fluent Wait in Selenium, which distinguish it from other wait commands like Implicit Wait and Explicit Wait:

  • More control over wait conditions: Fluent Wait gives testers more control over the wait process by setting custom conditions such as polling intervals and waiting time exceptions.
  • Flexibility in handling timing issues: Unlike Implicit Wait, which waits for a fixed amount of time regardless of whether the element is ready, or Explicit Wait, which waits for a certain condition to be met or a timeout to occur, Fluent Wait provides more flexibility in managing timing issues in dynamic web pages.
  • Precise timing: Fluent Wait checks for the element repeatedly at set intervals, ensuring that the test script proceeds only when the element is ready. This helps to avoid unnecessary delays in the test script.
  • Fluent chaining of commands: With Fluent Wait, testers can chain multiple commands in a fluent API style, making it easy to specify the wait conditions for a particular element.
  • Support for different conditions: Fluent Wait supports a range of conditions such as visibility of an element, presence of an element, and text to be present in an element. This allows testers to specify the conditions to be met before continuing with the test script.

Test script with an explanation – Selenium FluentWait

Let's look at a sample test script.

The above code does the following:

  • First, the necessary Selenium libraries are imported, and the path to the ChromeDriverf executable is set.
  • Next, a new ChromeDriver instance is created, and the browser navigates to the example.com page.
  • A FluentWait instance specifies the maximum time to wait for the condition to be met, the interval between each check, and the exception to ignore during the wait time.
  • An element is identified using the By.id method, and a condition is set to wait for the element to be visible using the ExpectedConditions.visibilityOfElementLocated method.
  • The wait. Until the () method is called wait for the specified condition to be met before proceeding with the script.
  • The element. click() method performs a click action on the element.
  • Finally, the ChromeDriver instance is closed using the driver.quit() method.
  • This script uses FluentWait to wait for a specific element on the example.com page to be visible before clicking it. The wait time is set to a maximum of 30 seconds, with a polling interval of 5 seconds between each check. The NoSuchElementException exception is ignored during the wait time. Once the element is visible, the script clicks it and quits the ChromeDriver instance.

Conclusion

  • Wait Commands in Selenium allow automated testers to pause test scripts for a specific period or until a particular condition is met, preventing errors due to timing issues.
  • There are three types of Selenium Wait Commands: Implicit, Explicit, and Fluent.
  • Implicit Wait instructs the web driver to wait for a certain amount before throwing a "NoSuchElementException" error if an element is not found.
  • Explicit Wait allows testers to wait for a specific condition to be met before proceeding with the test script.
  • Fluent Wait in selenium provides more flexibility than Implicit and Explicit Wait by allowing testers to set conditions such as polling intervals and exceptions to the wait time.
  • Fluent Wait offers more control over the wait process by allowing testers to set custom conditions for waiting, including wait time and polling intervals.
  • Wait for Commands are essential for dealing with dynamic web pages, where elements may not be immediately available or may take different amounts of time to load.