Waits in Selenium Web Driver

While working on an automation project, the biggest challenge you will face is synchronization ie. syncing up automation scripts with the application under test. There are few web pages or web elements which load within no time and there are few which take comparatively longer time to load. In this tutorial, we will learn several types of wait statements that Selenium WebDriver offers.

In Selenium WebDriver, to sync up scripts there are four types of wait:

  • Page Load Timeout
  • Implicit Wait
  • Explicit Wait
  • Fluent Wait

1. PageLoadTimeout –

This is the maximum time selenium waits for a page to load successfully on a browser. If the page takes more than this time, it will throw a Timeout Exception.

 driver.manage().timeouts().pageLoadTimeout(90, TimeUnit.SECONDS);

EXPLANATION- In the above code, pageLoadTimeout() method is accepting two arguments, one is waiting time and in another, we are specifying the Time Unit.

Here Selenium WebDriver instance will wait a maximum of 90 seconds for a webpage to load. If it is loaded before the specified wait time, the execution will move to the next line of the script. If it doesn’t get loaded in 90 seconds it will throw the Timeout Exception.

Now, let’s understand how a Web page gets loaded in a Web browser?

  • WEB PAGE- It is a document commonly written in Hypertext Markup Language (HTML) that is accessible through the Internet or other network using an Internet browser. A web page is accessed by entering a URL address and may contain text, graphics, and hyperlinks to other web pages and files. The page you are reading now is an example of a web page.
  • WEB BROWSER- It knows how to go to a Web server on the Internet and request a page so that the browser can pull the page through the network and into your machine. A Web browser knows how to interpret the set of HTML tags within the page in order to display the page on your screen as the page’s creator intended it to be viewed.
  • WEB SERVER – A Web server is a piece of computer software that can respond to a browser’s request for a page, and deliver the page to the Web browser through the Internet.

How Webpage gets loaded

Fig. How does page load work?

Whenever we type a URL in a browser, an HTTP request is triggered to the Web Server, which can be a GET or a POST request. WebServer responds to the browser with a GET response which has an attribute called content-length. The browser waits for the data which comes in chunks till data equal to this content-length attribute is received and then it generates an event trigger which signifies that page is loaded successfully. Selenium Page “load Timeout” command waits for this event trigger.


2. Implicit Wait –

Some Web Elements takes less time to load on the screen whereas other takes a lot of time.

The implicit wait can be considered as element detection timeout. Once defined in a script, this wait will be set for all the Web Elements on a page.

While searching for the element, the selenium webdriver keeps polling to check whether that element is available in the DOM of the HTML page.

It the maximum time (Threshold value), selenium code waits to interact with that Web Element, before throwing “Element not found exception”.

It can be added in the code as below –

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

3. Explicit Wait

This wait can be considered as conditional wait and is applied to a particular Web Element with a condition. There are many conditions that can be applied using explicit wait.

Say, for example, there is a Web Element on a page that takes more than expected time to appear on the page, so instead of increasing Implicit wait for a particular Web Element, we can apply explicit wait to that element with a condition.

public void waitTillElementVisible(){
 
WebDriverWait wait = new WebDriverWait(Driver, 90);
 
wait.until(ExpectedConditions.visibilityOfElementsLocatedBy(By.xpath("//input[@type='text']")));
 
}

Like this, there can be “n” number of conditions for which you can apply explicit wait, say, for example, wait till an alert is present, or wait till the color property of a WebElement change and so on.

Refer to the below screenshot for better illustration of different conditions in explicit wait.

Explicit Wait

                                                                                                          Explicit Wait

4. Fluent Wait-

In Fluent wait, a maximum time is defined to wait for a condition and along with that polling time is also defined. Polling time is the frequency with which the condition is checked. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.

Let us look at the code:

public static void fluentWait(WebDriver driver, int timeout, int pollingTime, By by){
		
                Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
				.withTimeout(Duration.ofSeconds(timeout))
				.pollingEvery(Duration.ofSeconds(pollingTime))
				.ignoring(NoSuchElementException.class);
				
		
		wait.until(ExpectedConditions.visibilityOfElementLocated(by));
	}

In this code, the maximum time to wait is passed as timeout, polling time (a frequency with which condition is checked again) is passed as “pollingTime” and we are waiting for a condition to get true i.e. visibility of Element located by.

Note: To test Implicit, Explicit and Fluent wait try the next scenario which is “Mouse Hover” operation without any timeout and then sees the difference with timeouts.

For any questions, queries or comments. Feel free to write to us at amrita@qatechhub.com or support@qatechhub.com. Happy learning 🙂

Saurabh Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra: