Part 5: Waits In Selenium: What Happens When We Mix Sleep With Other Types Of Waits ?

Hello Folks,

In this post we will learn about behavior of webdriver when we mix waits together.

There are four types of waits in Selenium Webdriver. Those are:

  1. Thread.sleep()
  2. Implicit wait
  3. Explicit wait (It is also called as webdriver wait.)
  4. Fluent wait. (It is also called as webdriver wait.

Every wait has its own significance and we can use it in different scenarios. But what will happen or we can say in better way, “How webdriver will behave when we use different types of waits together?”.

Let’s think about it.

Mixing Thread.sleep() with any other waits:

  • Thread.sleep() just holds the execution for specified time wherever you use this.
  • If we use any other waits with sleep method, webdriver will just hold the execution for specified time and then will follow other wait.
  • Sleep will not override any waits and vice-versa. For example, if we use sleep of 5 seconds and implicit wait as 30 seconds, in all scenarios webdriver will wait for minimum 5 seconds. Implicit wait will be applicable after that. We can say indirectly you have set time out as 5+30=35 seconds which is maximum and 5 seconds are minimum.
  • Mixing sleep with any other waits will just increase your test execution time. We will understand this with below example:

Scenario:

  1. Open any browser.
  2. Load URL: :https://www.redbus.in/”
  3. Type “Ban” in From text box and wait till you get all results.
  4. Select Bangalore.

We define implicit wait and sleep together and assume all elements are available in DOM.

 

package MakeSeleniumEasy;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class SleepWithImplicitWait {

	public static void main(String[] args) throws InterruptedException {
		
		// Opening chrome browser
		System.setProperty("webdriver.chrome.driver", "./exefiles/chromedriver.exe");
		WebDriver driver= new ChromeDriver();
		driver.manage().window().maximize();
		
		// setting implicit time
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		
		// Loading a URL
		driver.get("https://www.redbus.in/");
		
		// Locating and typing in From text box. 
		WebElement fromTextBox= driver.findElement(By.id("src"));
		fromTextBox.sendKeys("Ban");
		/* Since it is a search and choose text box, we need to wait till we get
		 * all available options. We will use sleep and implicit wait together.
		 */
		System.out.println("Wait starts:"+new Date());
		// Using sleep, we hold the execution
		
		Thread.sleep(5000);
		
		
		
		// Clicking on first search result
		driver.findElement(By.xpath("//li[@select-id='results[0]']")).click();
		System.out.println("Selected Bangalore");
		System.out.println("Wait ends:"+new Date());
		//Closing browser
		driver.quit();
		System.out.println("browser closed");
	}
}

Output:

Wait starts:Fri Jun 23 22:41:25 IST 2017
Selected Bangalore
Wait ends:Fri Jun 23 22:41:31 IST 2017
browser closed

Now run the same program by removing sleep:

Output:
Wait starts:Fri Jun 23 22:43:10 IST 2017
Selected Bangalore
Wait ends:Fri Jun 23 22:43:11 IST 2017
browser close

Observe both the output. Using sleep and implicit wait together increased your test execution time. It is not good. We should avoid mixing sleep with other waits.

I use term “avoid” because you will find many scenarios where you need to use sleep with other waits. Sometimes predefined timeout is not enough to handle scenarios. Suppose you have defined implicit wait as 30 seconds which is applicable for entire life time of webdriver.  You come up with scenario where you need to increase wait time. You can change implicit timeout but it will impact whole session of webdriver. So easy way is to use Thread.sleep() for extra wait time.

The main learning from above post is that webdriver behaves normally when we use sleep and other waits together. It will not get confused which wait to use. If will follow sleep and other wait in order.

In next post we will see what happens when we mix implicit and explicit wait.

If you have nay doubt, feel free to comment or contact me.

If you like my posts, please like, comment, share and subscribe. Do not forget to check Frequently Asked JAVA Programs.

#HappyLearning

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning