Learn about Implicit and Explicit Waits in Selenium WebDriver:
In our previous tutorial, we covered WebDriver’s looping and conditional operations, which included methods for handling visibility options of web elements. These methods are useful for dealing with different types of conditions.
Recommended IPTV Service Providers
- IPTVGREAT – Rating 4.8/5 ( 600+ Reviews )
- IPTVRESALE – Rating 5/5 ( 200+ Reviews )
- IPTVGANG – Rating 4.7/5 ( 1200+ Reviews )
- IPTVUNLOCK – Rating 5/5 ( 65 Reviews )
- IPTVFOLLOW -Rating 5/5 ( 48 Reviews )
- IPTVTOPS – Rating 5/5 ( 43 Reviews )
In this free Selenium training series, we will dive into the topic of Selenium WebDriver’s various types of waits. We will also explore the various navigation options available in WebDriver.
Waits are crucial for troubleshooting issues that occur when redirecting to different web pages, refreshing the page, and loading new web elements. Sometimes, there may be delays due to Ajax calls or reloading of the page, resulting in delayed visibility of web elements on the page.
Users often navigate between web pages back and forth. For simulation purposes, WebDriver provides the navigate() commands/methods, which allow users to navigate through web pages based on the browser’s history.
WebDriver provides two types of waits to handle recurring page loads, web element loads, window appearance, pop-ups, error messages, and element reflection on the web page:
- Implicit Wait
- Explicit Wait
Let’s discuss each of them in detail with a practical approach.
What You Will Learn:
WebDriver Implicit Wait
Implicit waits are used to set a default waiting time (e.g., 30 seconds) between each test step/command in the test script. The subsequent test step will only execute after the specified time has elapsed since the execution of the previous test step/command.
- The implicit wait is a single line of code and can be declared in the setup method of the test script.
- Compared to explicit waits, implicit waits are more transparent and easy to use. The syntax and approach are simpler than explicit waits.
However, implicit waits have drawbacks. They can increase the execution time of the test script as each command waits for a fixed amount of time before resuming execution.
To troubleshoot this issue, WebDriver introduces explicit waits. We can apply explicit waits whenever needed instead of waiting for each test step to execute.
import java.util.concurrent.TimeUnit – To apply implicit waits, we need to import this package into our test script.
Add this line of code to your test script immediately after initializing the WebDriver instance variable. This sets the implicit wait in your test script.
The implicit wait requires two parameter values. The first argument indicates the waiting time in numeric digits. The second argument indicates the time unit of measurement. In the code above, the default wait time is set to 30 seconds with the time unit set as “seconds”.
WebDriver Explicit Wait
Explicit waits are used to pause execution until a certain condition is met or the maximum time has elapsed. Unlike implicit waits, explicit waits are applied to specific instances only.
WebDriver introduces classes like WebDriverWait and ExpectedConditions to enforce explicit waits in test scripts. In this discussion, we will use “gmail.com” as an example.
Scenario to be Automated
- Launch the web browser and open “gmail.com”
- Enter a valid username
- Enter a valid password
- Click on the sign-in button
- Wait for the Compose button to be visible after page load
WebDriver Code using Explicit wait
Please note that for script creation, we will use the “Learning_Selenium” project created in the previous tutorials.
Step 1: Create a new Java class named “Wait_Demonstration” under the “Learning_Selenium” project.
<…rest of the text is truncated for brevity…>