In Selenium, waits are mechanisms that allow you to control the timing of interactions with web elements during test execution. Waits are used to synchronize the test script with the web application’s dynamic behaviour, such as element visibility, presence, or availability. Selenium provides three types of waits:
Implicit Wait:
The implicit wait is a global wait applied to all web elements in the test script.
It instructs Selenium WebDriver to wait for a certain amount of time before throwing an exception if an element is not immediately available.
The implicit wait is set using the driver.manage().timeouts().implicitlyWait() method.
Example:
Explicit Wait:
Explicit wait allows you to wait for a specific condition to be met before proceeding with the test script.
It provides more fine-grained control over waiting by specifying the condition and maximum wait time.
Explicit waits use the WebDriverWait class along with an expected condition to wait for.
Example:
Fluent Wait:
The fluent wait is similar to the explicit wait, but it provides additional flexibility in defining the polling interval and exceptions to ignore during the wait.
It allows you to define the maximum wait time and the frequency at which the condition is checked.
Fluent waits uses the Wait interface and its implementations, such as FluentWait.
Example:
With explicit and fluent waits, you can wait for specific conditions, such as element visibility, presence, clickability, or custom conditions defined by you. They provide more control and flexibility over synchronization with the application, allowing your test script to wait until the desired condition is met before proceeding.
It’s important to choose the appropriate type of wait based on your test requirements and the specific synchronization needs of the web application you are testing.