21

I was reading the SeleniumHQ documentation and came across the following statements.

"WARNING: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example setting an implicit wait of 10s and an explicit wait of 15 seconds, could cause a timeout to occur after 20 seconds."

For some reason, I cannot get this to make sense. The total timeout 20s is the main confusion point for me. Can anyone explain if I am missing something?

EDIT

My question is not about the implementation/consequences of mixing those waits. It's entirely about the statements and calculation of timeout on the doc.

2nd Edit

Looks like the doc is correct according to the tests below. I still need the explanation though.

Just Implicit wait

using System;
using System.Diagnostics;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace Test
{
    [TestFixture]
    public class Test
    {
        private IWebDriver _webDriver;

        [Test]
        public void ExplicitVsImplicitWaitTest()
        {
            _webDriver = new ChromeDriver();
            _webDriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
            _webDriver.Navigate().GoToUrl("https://www.google.com/");
            _webDriver.Manage().Window.Maximize();

            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            try
            {
                //new WebDriverWait(_webDriver, TimeSpan.FromSeconds(15)).Until(
                //ExpectedConditions.ElementExists(By.CssSelector("Should Fail")));
                _webDriver.FindElement(By.CssSelector("Should Fail"));
            }
            catch ( NoSuchElementException exception)
            //catch ( OpenQA.Selenium.WebDriverTimeoutException)
            {
                stopwatch.Stop();
                Console.WriteLine(stopwatch.Elapsed);
            }

            _webDriver.Quit();

        }
    }
}

Time in second: 00:00:10.0167290

Just Explicit wait

using System;
using System.Diagnostics;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;

namespace Test
{
    [TestFixture]
    public class Test
    {
        private IWebDriver _webDriver;

        [Test]
        public void ExplicitVsImplicitWaitTest()
        {
            _webDriver = new ChromeDriver();
            //_webDriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
            _webDriver.Navigate().GoToUrl("https://www.google.com/");
            _webDriver.Manage().Window.Maximize();

            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            try
            {
                new WebDriverWait(_webDriver, TimeSpan.FromSeconds(15)).Until(
                ExpectedConditions.ElementExists(By.CssSelector("Should Fail")));
                _webDriver.FindElement(By.CssSelector("Should Fail"));
            }
            //catch ( NoSuchElementException exception)
            catch ( OpenQA.Selenium.WebDriverTimeoutException)
            {
                stopwatch.Stop();
                Console.WriteLine(stopwatch.Elapsed);
            }

            _webDriver.Quit();

        }
    }
}

Time in second: 00:00:15.2463079

Mixed, Explicit and Implicit both

using System;
using System.Diagnostics;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;

namespace Test
{
    [TestFixture]
    public class Test
    {
        private IWebDriver _webDriver;

        [Test]
        public void ExplicitVsImplicitWaitTest()
        {
            _webDriver = new ChromeDriver();
            _webDriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
            _webDriver.Navigate().GoToUrl("https://www.google.com/");
            _webDriver.Manage().Window.Maximize();

            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            try
            {
                new WebDriverWait(_webDriver, TimeSpan.FromSeconds(15)).Until(
                ExpectedConditions.ElementExists(By.CssSelector("Should Fail")));
                _webDriver.FindElement(By.CssSelector("Should Fail"));
            }
            //catch ( NoSuchElementException exception)
            catch ( OpenQA.Selenium.WebDriverTimeoutException)
            {
                stopwatch.Stop();
                Console.WriteLine(stopwatch.Elapsed);
            }

            _webDriver.Quit();

        }
    }
}

Time in second: 00:00:20.5771817

Saifur
  • 16,081
  • 6
  • 49
  • 73
  • 1
    @SiKing My confusion is not on the *implementation* I am confused about the calculation of **20s** timeouts on the doc. I wanted to know how they came up with **20s** and probably the link you have provided does not talk about the same thing I am talking about here – Saifur Apr 06 '15 at 16:04
  • 1
    @SiKing: I agree there are many answers but none of them justify how the the waiting time is calculated – LittlePanda Apr 06 '15 at 16:10
  • 2
    @Saifur: Any theories? I tried this with a sample code and the timeout happened after 15 seconds. It seems explicit overrides implicit. – LittlePanda Apr 06 '15 at 16:29
  • @LittlePanda Thanks! So the timeout is 15s not 20s? Is the doc incorrect then? – Saifur Apr 06 '15 at 16:33
  • 1
    @Saifur: Yes maybe. I tried out explicit = 30 and implicit = 10 and got this excpetion `Exception in thread "main" org.openqa.selenium.TimeoutException: Timed out after 30 seconds waiting for element to be clickable: By.id: test ` – LittlePanda Apr 06 '15 at 16:40
  • In every case(implicit>explicit, explicit>implicit), explicit time overrides implicit. – LittlePanda Apr 07 '15 at 03:07
  • @LittlePanda Believe it or not the doc is right. See my edits and see if those make sense with the test you have done – Saifur Apr 07 '15 at 03:33
  • Good one. Will definitely try with the stopwatch. – LittlePanda Apr 07 '15 at 03:37

1 Answers1

34

My question is not about the implementation of those waits. It's entirely about the statements and calculation of timeout on the doc.

But you have to know how they are implemented to make sense of what is going on. Here is what happens with your mix of the two types of waits. I'm kipping over those steps that are not important for the discussion.

  1. Your script sets an implicit wait.

  2. Your script starts an explicit wait checking whether the element exists. The explicit wait works by polling. So it sends a command to the browser to check for the existence of the element.

  3. Because of the implicit wait already set, the command sent to the browser takes 10 seconds to return a failure.

  4. Your explicit wait checks whether it has reached its time limit which is 15s. It is currently at 10s (+ a tiny amount of time taken to execute the script, network latency, etc.) into the wait, which is less then 15s. So it is not done waiting and reissues the same command as in step 2 above.

  5. Because of the implicit wait, the command sent to the browser takes 10 seconds to return a failure.

  6. When the explicit wait checks again for its own timeout, more than 15s have elapsed, so it times out.

So the explicit wait polls twice, and each time takes 10 seconds which mean 20 seconds total (plus a tiny amount of time to account for the bookkeeping).

An explicit wait does not do anything to detect and compensate for an implicit wait that has been set. And it does not continue running in parallel to the commands it sends to the browser. While a browser command executes, the explicit wait is not doing any bookkeeping or able to time out. It has to wait for the browser command to complete before checking whether it should time out.

Louis
  • 146,715
  • 28
  • 274
  • 320
  • 3
    Great explanation. I'm as well got strange waits inconsistency, your post helped me to understand the mechanism (although, probably i got another issue, my "symptoms" are a bit different). – Johnny May 06 '15 at 15:29