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

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:

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.

 

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:

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.

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

 

 

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