Hello Folks,

In this post we will learn:

  1. Why we need to use wait statements in selenium webdriver.
  2. Types of waits in selenium webdriver.
  3. What is Thread.sleep() and how to use it.
  4. Disadvantages of Thread.sleep() method.

“Waits” are very important concepts in Selenium WebDriver. Understanding of types and usages of waits are necessary to write perfect script so that it script should not fail.

Let’s automate something first:-

Scenario:

  1. Launch a chrome browser.
  2. Load “https://www.redbus.in/” URL.
  3. Locate and type “Ban” in “From” text box.
  4. Click on Bangalore from search result.
  5. Close the browser.

Lets write code for above scenario:


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

public class NeedOfWaits2 {

	public static void main(String[] args) {
		
		System.setProperty("webdriver.chrome.driver", "./exefiles/chromedriver.exe");
		// Opening a browser
		WebDriver driver= new ChromeDriver();
		driver.manage().window().maximize();
		// 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");
		// Clicking on first search result
		driver.findElement(By.xpath("//li[@select-id='results[0]']")).click();
		//Closing browser
		driver.quit();
		
	}
}

Run the code.

Output:

We were expecting that program will run successfully. But we are getting  NoSuchElementException.

What is meaning of NoSuchElementException?

  • NoSuchElementException is a sub class of RuntimeException.
  • It is unchecked exception.
  • It is thrown by findElement(By by) method. Note here I mentioned only findElement() method not findElements() method.
  • It is thrown when WebDriver is not able to locate a element for some reasons. It might be wrong locator or element is not loaded yet.

So, we got basic idea about NoSuchElementException.

Let’s analyse what causes our program to throw NoSuchElementException.

Let’s try it manually. When we typed “ban” in From text box and after SOME SECONDS it displays search result. And then we clicked on first search result. WebDriver will keep executing statements one by one in a flow. Did we make WebDriver to wait for “SOME SECONDS” so that search result should appear? Answer is No and which causes script to throw  NoSuchElementException.

If you see the console error message carefully, you will find a line as below:

Command duration or timeout: 64 milliseconds

You can see WebDriver decides to throw exception in just 64 ms. It has no default waiting time. So to make WebDriver to wait for some times, programmers need to write some lines of code.

Different ways of making WebDriver to wait:

  1. Thread.sleep() method.
  2. Implicit wait.
  3. Explicit wait.
  4. Fluent wait.

We will see first method in this post.

What is Thread.sleep() method:

  1. Thread is a class in JAVA.
  2. sleep() is a static method of Thread class so we can use it using class name i.e. Thread.
  3. Thread.sleep causes the current thread to suspend execution for a specified period.
  4. sleep() methods accept duration in miliseconds. ( 1 s= 1000 ms).
  5. It throws  IllegalArgumentException – if the value of millis is negative.
  6. sleep() throws a checked exception which we must either throw or handle it using try catch.
  7. Syntax: Thread.sleep(3000);

So we know one way of making selenium WebDriver to wait. Let’s implement it. We should add sleep() method where it is required. We know after typing “BAN” in From text box we need to make WebDriver wait. Lets put sleep statement there.

 

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

public class NeedOfWaits2 {

	public static void main(String[] args) throws InterruptedException {
		
		System.setProperty("webdriver.chrome.driver", "./exefiles/chromedriver.exe");
		// Opening a browser
		WebDriver driver= new ChromeDriver();
		driver.manage().window().maximize();
		// 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");
		System.out.println("Typed Ban");
		// Clicking on first search result
		Thread.sleep(5000);
		driver.findElement(By.xpath("//li[@select-id='results[0]']")).click();
		System.out.println("Selected Bangalore");
		//Closing browser
		driver.quit();
		System.out.println("browser closed");
		
	}
}

Output:

So our programming is working fine using sleep() method. But it has a lot of disadvantages.

Disadvantages of Thread.sleep() method:

  • We make WebDriver to wait for 5 seconds in above program. What if search result appears in just a second? WebDriver will still wait for another 4 seconds. It will increase test execution time.
  • We need to pass wait time in advance to sleep method. What if search result appears after 6 seconds. Our script will fail again. We can not pass dynamic waiting as required.
  • You need to write sleep() method whenever we need to make webdriver wait. That means to provide wait for two web element you need to write Thread.sleep() twice just before you locate web elements.
  •  It is not good programming practice.

Because of above disadvantages of sleep() method we go for other types of wait provided by selenium webdriver.

Other types of waits we will see in upcoming posts.

If you have nay doubt or suggestions, please comment below.

#HappyLearning

13 thoughts on “Part 1: Waits In Selenium WebDriver: Thread.sleep()

  1. Very nice, in detail explanation given. Thanks.

    I’m begginner in Automation Testing. Could you please refer me some learning videos.
    It would be a great help.

    1. Thanks Nikhil. You can view Mukesh Otwani youtube videos. Also you can follow my blog from first post.

Leave a Reply