Thread.sleep() Wait in Selenium WebDriver

Wait is one of the most important topic in Selenium WebDriver as it actually saves your scripts to fail from common application issues like Network Delays, Element loading, AJAX loading etc.

You might have used Thread.sleep() in your script, Let’s have a look at the disadvantages of using it by following scenario :

Scenario :
1. Launch https://www.google.com in Chrome Browser
2. Type ‘Selenium’ in Search Box [Suggestions related to search keyword might takes some to load]
3. Click on the Last suggestion

GoogleSearch

Sample Script


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

public class TestClass {

	public static void main(String s[]) throws InterruptedException  {
		
		
		System.setProperty("webdriver.chrome.driver", "D:/workspace/TestProject/lib/chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		
		driver.get("http://google.com");
		
		driver.findElement(By.name("q")).sendKeys("Selenium");
				
		driver.findElement(By.xpath("//div[@class='sbsb_a']//ul/li[4]")).click();
		
		
	}

}

You might get following error when you execute above script because when selenium will type Search Keyword, suggestions results will gonna take some time to get displayed. However, selenium will keep on executing statements one by one without any break as in the above script, we haven’t used any logic which would wait for the element, if it is taking time to load and finally the script will fail. Here, the Wait concept comes into picture.

Error Description

SearchResultError

If you notice above in error Description, an error which is thrown here is NoSuchElementException. And it was thrown in just 144 Milliseconds.
This exception is thrown by findElement() function.
Webdriver throws this exception, if it is unable to locate an element due to certain reasons like locator used in the script is incorrect or element is not loaded yet.

Lets use Thread.sleep() command in above script to give a specific wait of 5 seconds after typing search keyword so that Suggestions will get enough time to load and then click on the last suggestion.


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

public class TestClass {

	public static void main(String s[]) throws InterruptedException  {
		
		
		System.setProperty("webdriver.chrome.driver", "D:/workspace/TestProject/lib/chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		
		driver.get("http://google.com");
		
		driver.findElement(By.name("q")).sendKeys("Selenium");
		
		Thread.sleep(5000);
				
		driver.findElement(By.xpath("//div[@class='sbsb_a']//ul/li[4]")).click();
		
		
	}

}


Above code will execute successfully as we have given a wait of 5 seconds after typing ‘Selenium’ Keyword.

Thread.sleep disadvantages

1. Here if you notice in above script, we have given a wait of 5000 Milliseconds(5 seconds). What if, an element just take just 1-2 seconds to load as in this case, script will still have to wait for another 3 seconds which is bad as it is unnecessarily increasing the execution time.

2. When using Thread.sleep(), we have to mention wait time in advance, there is no guarantee that the element will be displayed in that specific wait time, there may be case when it will takes may be more than 5 seconds to load and again your script will fail. Hence, using sleep() in your script is not always the wise decision. Thread.sleep() is basically a static wait and it partially solves the problem.

In Order to overcome from the issues explained above, you would have to go with another wait concepts provided by Selenium WebDriver like Implicit wait, Explicit wait and Fluent Wait.

If you really like the information provided above, please don’t forget to like us on Facebook, you can also leave the comment.

Leave a Reply

Your email address will not be published. Required fields are marked *