Implicit Wait- Synchronization in Selenium Part 1

Today we are going to discuss exclusively on the Implicit wait. It has the very prominent role in synchronizing test application and testing tool altogether. We have already discussed different types of WebDriver wait in our previous tutorial, so you are advised to just go through that article so that your basic concept about various waits and synchronization will be cleared.

WebDriver Wait- A robust way to maintain Synchronization

Did you know?

Selenium WebDriver borrowed the concept of Implicit wait from another testing tool, that is, Watir.

You should be very wise in picking up wait commands among different wait commands because your selection decides the test execution speed up to some extent. You would be very surprised to know that Thread.sleep() & Implicit wait work similar while providing waiting time to the application. However, there is a difference of just a small thread (string, rope).  I will explain this minute difference later in this article.

What is Implicit Wait in Selenium WebDriver?

Implicit wait sends direction to the WebDriver to poll the Document Object Model (DOM) for a given amount of time when trying to find web element(s) if they can’t be available immediately. The default time setting is 0. Once this wait sets, it continues till the life of the WebDriver object instance.

Explanation of the above definition of Implicit Wait

So in simple language, once you set Implicit wait at a particular time then WebDriver starts looking in the DOM structure to find the element, if it finds early then it will proceed further, otherwise, it will again go back to the previous step. It polls till the higher limit of the time reaches. Its polling time starts from 0 seconds and goes till the time you have set. Hope you got the points.

Implicit wait

What is the difference between Thread.sleep() and Implicit Wait?

This is the most debatable question, but the answer is very straightforward. Thread.sleep() is not a logical wait. It simply pauses the execution for the time duration which you set. It does not do any execution behind it. Whereas Implicit wait is a logical wait and it does not pause the execution, however, it keeps finding the element with polling frequency. If an element is available in less than the defined time then WebDriver proceeds further and if not found them it throws an exception.

In case of Thread.sleep(), it waits until the given time and even if the element is visible earlier than the given time length then Thread.sleep() does not proceed ahead, rather, it waits for the full amount of time. One disadvantage, it does not poll for every second. So Thread.sleep() simply elongates the execution time, hence, you should be very wise to use Thread.sleep().

getWindowHandle- Handle multiples windows in Selenium

Syntax of Implicit wait

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

Sample program where Implicit wait has been utilized:

package SeleniumTester;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class LocatorDemo {
  @Test
  public void locatorDemo() throws Exception {
	  System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
	  
	  WebDriver driver = new ChromeDriver();
	  
	  //Implicit Wait
	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	  
	  driver.get("https://www.facebook.com");
	  
	  driver.close();
	  
	  driver.quit();
  
  }
}

 

Leave a Reply