Hello Folks,

In last post we have seen the behavior of webdriver when mixing sleep with other waits.

In this post, we will learn behavior of webdriver when we mix implicit wait and explicit wait.

I hope, you will have good idea about implicit wait and explicit wait from my old posts.

We know when we define implicit wait , it is set for whole lifetime of webdriver while explicit wait is set for a particular step.

We will divide this topic in below scenarios:

  1. When element s found without any delay.
  2. When element is not found.

When element is found without any delay:

We will declare an implicit wait and explicit wait both in program.

Let’s see output of below program:

public class MixImplicitExplicitWait {

	public static void main(String[] args) {
		
		// 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/");
		
		// defining explicit wait
		WebDriverWait wait= new WebDriverWait(driver, 20);
		// Locating and typing in From text box. 
		
		System.out.println("Wait starts:"+new Date());
		WebElement fromTextBox= wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("src"))));
		System.out.println("Wait ends:"+new Date());
		driver.quit();
		
	}
}

Output:
Wait starts:Sat Jul 01 13:14:46 IST 2017
Wait ends:Sat Jul 01 13:14:46 IST 2017

If you observe output, when element is found quickly, mixing of implicit wait and explicit wait will not impact on webdriver behavior.

 When element is not found:

This is the scenario where mixing of implicit and explicit wait changes webdriver behavior. We will see how but in three sub scenarios:

a. When implicit wait and explicit wait timeout are same.
b. When implicit wait time is greater than explicit wait time.
c. When explicit wait time is greater than implicit wait time.

 When implicit wait and explicit wait timeout are same:

I will keep implicit wait and explicit wait time same as 10 seconds. I will pass wrong locator so that webdriver will not be able to locate element. I will catch the exception to print wait ends time.

 

public class MixImplicitExplicitWait {

	public static void main(String[] args) {
		
		// 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(10, TimeUnit.SECONDS);
				
		// Loading a URL
		driver.get("https://www.redbus.in/");
		
		// defining explicit wait
		WebDriverWait wait= new WebDriverWait(driver, 10);
		// Locating and typing in From text box. 
		
		System.out.println("Wait starts:"+new Date());
		try{
		WebElement fromTextBox= wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("Wrong Locator"))));
		}catch(Exception e)
		{
			System.out.println("Unable to locate element.");
		}
		System.out.println("Wait ends:"+new Date());
		driver.quit();
		
	}
}

Output of run 1:
Wait starts:Sat Jul 01 13:35:33 IST 2017
Unable to locate element.
Wait ends:Sat Jul 01 13:35:43 IST 2017

WAIT TIME: 10 secods

Output of run 2:

Wait starts:Sat Jul 01 14:02:23 IST 2017
Unable to locate element.
Wait ends:Sat Jul 01 14:02:43 IST 2017

WAIT TIME: 20 seconds

You run the same program multiple times. Maximum time you will get wait time as 10 seconds but sometimes 20 seconds.  You will see wait time as 20 seconds rarely.

 When implicit wait time is greater than explicit wait time:

I will just make implicit wait time greater than explicit wait. Run this program multiple times and observe output:

public class MixImplicitExplicitWait {

	public static void main(String[] args) {
		
		// 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/");
		
		// defining explicit wait
		WebDriverWait wait= new WebDriverWait(driver, 10);
		// Locating and typing in From text box. 
		
		System.out.println("Wait starts:"+new Date());
		try{
		WebElement fromTextBox= wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("Wrong Locator"))));
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		System.out.println("Wait ends:"+new Date());
		
		
		driver.quit();
		
	}
}

Output:

 

In above case, you will always see time out equal to implicit wait.

 

When explicit wait time is greater than implicit wait time:

In this case just make explicit wait time greater than implicit wait and run program multiple times.

public class MixImplicitExplicitWait {

	public static void main(String[] args) {
		
		// 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(10, TimeUnit.SECONDS);
				
		// Loading a URL
		driver.get("https://www.redbus.in/");
		
		// defining explicit wait
		WebDriverWait wait= new WebDriverWait(driver, 30);
		// Locating and typing in From text box. 
		
		System.out.println("Wait starts:"+new Date());
		try{
		WebElement fromTextBox= wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("Wrong Locator"))));
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		System.out.println("Wait ends:"+new Date());
		
		
		driver.quit();
		
	}
}

Output:

You will timeout equal to implicit wait time.

 

So with above example you will find different behavior. You can also notice how implicit wait can ruin your explicit wait functionality. Implicit wait destroys meaning of using explicit wait when using together. So it is advised not to use implicit wait and explicit wait together.

Now we will see reason behind these:

Actually when we use both waits together, both waits will be applied at the same time and it get messed up. We can see it scenarios by scenarios:

  1. Explicit Wait= Implicit Wait => Say 10 seconds.

Both waits get activated at same time to locate element. Explicit wait keeps searching for an element till it is found and implicit wait allows webdriver to search till timeout. When explicit wait starts and looks for element, because of implicit wait it needs to wait for 10 seconds because element is not found. So both waits completes 10 seconds wait time.

But sometime you get wait time as 20 seconds because of synchronization issue. Implicit wait throws exception if element is not found. So till the time implicit wait throws exception,  explicit waits starts another iteration which makes again to wait for 10 seconds. So in this case you will get timeout as 20 seconds.

2. Implicit wait(30) > Explicit Wait(10)

When explicit wait stars looking for element, it needs to wait for 30 seconds because of implicit wait time.

3. Implicit wait(10) < Explicit Wait(30)

When explicit wait starts looking for element, it needs to wait for 10 seconds because of implicit wait. After that implicit wait throws exception because of not able to locate element.  Exception stops explicit wait to search further and doe snot allow to reach its timeout.

 

That’s it guys. This post took 9 hours for me. I did a lot of experiments before concluding any point. If you find any mistakes or have any doubt feel free to comment.

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

#HappyLearning

 

 

34 thoughts on “Part 6: Waits In Selenium: What Happens When We Mix Implicit Wait And Explicit Wait ?

  1. Hi Amod,

    Thanks for the explanation. I had faced the same question in interview, fortunately I was correct.

    But, I have a doubt, if it is not a good idea to use Implicit wait then everyone should have used explicit wait only, when they to go for Implicit wait? In what case we need to use it?

    Thanks in advance!

    Thank you,
    Shriniwas

    1. Hello,
      It comes with experience. Every wait has its advantages. Explicit and fluent waits are more efficient. People use as per their convenient. Implicit you need to define once but explicit wait is exclusive for an action. You need to always extra codes for explicit wait.

  2. Hello Amod,

    I have also dove same thing and findings are same .in every case implicit wait will ruin the explicit wait . Then what is the solution or workaround.how can we will use both in single code?

    Thanks In Advance

      1. Hello Amod,

        I found 1 more thing there are 2 ways to set condition in explicit wait

        1.wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector(“input.newsrchbtn11”)));

        2.wait.until(ExpectedConditions.presenceOfElementLocated((By)driver.findElement(By.cssSelector(“input.newsrchbtn11”))));

        if i use 1st line code to set condition then Explicit wait will work correctly bcs in 1st wait m not using driver.findelement , So implicit wait will not work .

        But if i will go with second line of code then only implicit wait will ruin explicit wait.

  3. It would be better if you elaborate the conclusion. Specially 2nd point where implicit wait greater than explicit wait, how it results into exception..?

    1. Because I amhave given wrong loctor so that it should throw an exception. And conclusion I have given clearly. Where you have doubt??

Leave a Reply