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 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through 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

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

  1. Hi Amod/All,

    In second/third scenario u have given a wrong conclusion

    Considering following case no. 2 :

    If u have Implicit wait 10 sec

    And Explicit wait 5 sec
    And search criteria find element by locator only

    //Don’t use any try catch and see what console says in exception

    console will directly say it has waited 5 seconds only for that Element

    You have given 10 seconds of timeout but it has waited only 5 seconds and rest 5 seconds it has waited because both the waits are triggered Same time but not for that Element.

    In third scenario u have taken search criteria as driver.findelement so in same case by default ur webelemnt follows implicit wait

    Conclusion : in second case it has waited 5 seconds only and not 10 seconds.

    In third case if u will take search criteria as driver.findelement it will by default take implicit wait timing even though explicit wait is greater than implicit.

    This is as per my observations and analysis please correct me if someone has other observations/analysis/results

    Thanks :
    Omkar

    1. Hi Omkar,
      Final conclusion is that it will be messed up when you mix both waits. Your above conclusions may reverse when you run the same tests multiple times. So it is really difficult to say what will be exact timeout.

  2. In my selenium hybrid framework i have a testbaseclass which has an implicit wait defined. now all other classes extends this testbase class. if i need to give an explicit wait for one class say class B and given that mixing waits is not good idea… is there a way to comment implicit wait fr class B?

      1. You should create a method for setting explicit wait in ur framrwork. Call and set wait value as required in ur test scripts.

  3. Hi,

    I tried the same thing for IW > EW and IW< EW , My results are exactly opposite to your results .

    PFB code :

    driver.get("https://www.google.com&quot;);

    WebDriverWait wait = new WebDriverWait(driver, 30);
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    Stopwatch stopwatch = Stopwatch.createStarted();
    try{
    driver.findElement(By.id("lst-ibfgdfg"));
    }
    catch(Exception e)
    {System.out.println("Selenium wait for this run was ########################## : " +stopwatch.elapsed(TimeUnit.SECONDS));
    stopwatch.stop();
    }

    try{
    stopwatch.reset();
    stopwatch.start();
    wait.until(ExpectedConditions.invisibilityOf(driver.findElement(By.id("lst-ib"))));

    }

    catch (Exception e)
    {
    stopwatch.stop();
    System.out.println("Selenium wait for this run was ########################## : " +stopwatch.elapsed(TimeUnit.SECONDS));
    }

    try{
    stopwatch.reset();
    stopwatch.start();
    driver.findElement(By.id("lst-ibfgdfg"));
    }
    catch(Exception e)
    {System.out.println("Selenium wait for this run was ########################## : " +stopwatch.elapsed(TimeUnit.SECONDS));
    stopwatch.stop();
    }
    driver.quit();
    }
    }

    RESULTS are :

    Selenium wait for this run was ########################## : 15
    Selenium wait for this run was ########################## : 30
    Selenium wait for this run was ########################## : 15

  4. 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.

  5. 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.

  6. 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

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.
%d bloggers like this: