Implicit Wait and Clear Wait in Selenium WebDriver

Tram Ho

In Selenium – Waits (instructions wait / wait) is a factor that plays a very important role when executing test cases. Waits helps users to troubleshoot while redirecting to different websites by refreshing the entire web page and reloading new web elements. For some reason related to the handling of web elements, Ajax, and Javascript on the web application side, loading the elements cannot happen at the same time. So using Waits will create time lag while loading and reflect web components, avoid loading data error.

In this article we will explore Implicit Wait and Explicit Wait are 2 popular Wait. These are two types of wait (wait) to handle periodic page load, web element loading; opens windows, popups, error messages and reflects web elements on the web page.

WebDriver Implicit Wait

The Implicit wait is used to provide a default timeout (say 30 seconds) between each consecutive test step / command in the entire test script. Accordingly, the next test step will only be executed after 30 seconds from the time the previous test command has finished executing.

Key Notes:

  • The Implicit wait is a single line of code that is declared in the setup method of the test script.
  • Compared to Explicit wait, Implicit wait is more explicit and simpler. The syntax and approach of the Implicit wait is also simpler.

In addition to the advantages of being easy and simple to apply, the Implicit wait also has a number of disadvantages such as: it increases the time to execute the test script because after each command will be stopped to wait for a specified amount of time. when continuing to execute the next command.

So to fix this problem, WebDriver provides an Explicit wait, where we can apply wait whenever a problem / situation arises instead of having to wait while doing each step of testing. .

Import Statements:

import java.util.concurrent.TimeUnit – In order to be able to access and apply the Implicit wait in our test scripts, we must import this package into the test script.

Syntax:

Put this line of code into the test script immediately after the WebDriver variation initialization. This is all it takes to place an implicit wait in your test script.

Code Walkthrough:

The Implicit wait consists of 2 parameters / arguments. The first argument indicates the number of times the system should wait (10 in this example). The second argument indicates the time unit (seconds in this example). Together, in the code above, we have set 10 seconds as the Implicit wait default timeout.

WebDriver Explicit Wait

The Explicit wait is used to pause the execution of the script until a specified condition is met or the maximum time has elapsed. Unlike Implicit wait, Explicit wait is only applied to a specific case instead of all commands in the script.

WebDriver provides classes WebDriverWait and ExpectedConditions to use for the Explicit wait. Let’s get started with Explicit wait through an example on the site ” gmail.com “.

Scenario:

  • Launch a web browser and open the page ” gmail.com
  • Enter a valid username
  • Enter a valid password
  • Click the Login button
  • Wait for the “Compose” button to appear after the page loads

WebDriver Code uses the Explicit wait

We will use the project “Learning_Selenium” created in the previous articles to create the script for the above scenario.

Step 1: Create a new java class named “Wait_Demonstration” in the project “Learning_Selenium”.

Step 2: Copy and paste the following code into the “Wait_Demonstration.java” class you just created. Here is the code corresponding to the scenario created above.

Import Statements

In the code above, the ExpectedConditions and WebDriverWait packages are imported at the beginning of the class before creating an executable script for the Explicit wait section:

  • import org.openqa.selenium.support.ui.ExpectedConditions
  • import org.openqa.selenium.support.ui.WebDriverWait

There are also a number of other packages mentioned in previous articles: for example, pakages refer to the Select class to handle dropdowns …

Initialize Object for the WebDriverWait class

Create a “wait” reference variable for the WebDriverWait class and initialize it using the WebDriver variant and declare the maximum wait time for execution to pause. This maximum timeout is in “seconds” (in this example the set is 30 seconds).

How to initialize WebDriver has been mentioned in previous articles.

Expected Condition

The above command is meant to wait for a specified amount of time or a condition is expected to happen, whichever occurs first.

To be able to do this, we use the “wait” reference variable of the WebDriverWait class created in the previous step along with the ExpectedConditions class and an actual condition expected to happen (visibilityOfElementLocated (By.xpath … )). As soon as the expected condition occurs, the program controller will move on to the next execution step instead of being forced to wait for the entire 30 seconds (maximum time set above).

Wait for the button “Compose” to appear and load finished, the program continues to call the next command: click the button “Compose”.

Types of Expected Conditions

The ExpectedConditions class provides a great helper for solving situations where we must determine the condition that happens before we execute the actual step tests.

The ExpectedConditions class comes with a variety of expected conditions that can be accessed with the help of the WebDriverWait reference variable and the until() .

Some typical Expected Conditions:

# 1) elementToBeClickable () – The expected condition is to wait for an element to be clickable, ie that the element must be present / displayed on the screen and enabled (clickable).

Sample Code:

# 2) textToBePresentInElement () – The expected condition is to wait for an element to contain the specified text.

Sample Code:

# 3) alertIsPresent () – The expected condition is waiting for an alert box to appear.

Sample Code:

wait.until(ExpectedConditions.alertIsPresent()) !=null);

# 4) titleIs () – The expected condition is to wait for a page to have a specific title.

Sample Code:

wait.until(ExpectedConditions.titleIs(“gmail”));

# 5) frameToBeAvailableAndSwitchToIt () – The expected condition is to wait for a frame to be available and as soon as there is a frame the control will switch to it automatically.

Sample Code:

Navigate with WebDriver

A very common user behavior is to repeatedly click back and forth on the web browser’s back and forward buttons to navigate to different websites visited during the current session above. Browser history. To simulate such actions, WebDriver introduces the Navigator commands. Here are some detailed commands:

# 1) navigate () .back ()

The command navigates to a website previously visited

Sample code:

The above command does not ask for a parameter and takes the user back to a previous web page in the web browser’s history.

# 2) navigate (). Forward ()

This command allows the user to navigate to the next web page with a reference to the history of the browser.

Sample code:

The above forward command also requires no parameters and takes the user to the next web page in the history of the web browser (the next web page of the site navigated to with the back command).

# 3) navigate (). Refresh ()

This command allows the user to refresh the current web page by reloading all the web elements in the page.

Sample code:

This is also a command without parameters.

# 4) navigate (). To ()

This command allows the user to launch a new web browser window and navigate to the specified URL.

Sample code:

Different from the above commands, this is a command with the input parameter 1 URL (the address you want to navigate to). This URL is specified to open in a newly launched web browser.

Epilogue

The above translation is still missing, if you are interested, you can find the original article here: https://www.softwaretestinghelp.com/selenium-webdriver-waits-selenium-tutorial-15/

In addition to the two types of wait mentioned in the article, there is another type of wait that is quite popular is the Fluent wait. Using the Fluent wait is in situations like, sometimes we come across elements that need one to two seconds to load, but other times it takes longer, for example, tens of seconds. Fluent wait will search again and again until it finds that element, or until time out. If you are interested, you can learn more in this article: https://vananhtooo.wordpress.com/2017/11/13/implicit-wait-explicit-wait-va-fluent-wait-trong-selenium-webdriver/

Share the news now

Source : Viblo