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
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…