There is a lot of discussion in the selenium space about explicit and implicit waits.
Getting ones wait strategy is clearly key to solving many intermittent test failures.
There are warning however such as if you mix them together you can create deadlock failure situations.
I find the terms confusing and misleading.
Can anyone succinctly describe the differences between them.
Are they conceptual as was as implemented in code?
Do I need to use webdriver waits over my own strategy?
3 Answers
Authoritative answer from Jim Evans, Selenium contributor: never mix implicit and explicit waits - and stick with explicit. Article explains also your other questions.
Converting our code to explicit-waits-only decreased flakiness the test considerably, I suspect for the reasons Jim explains in his answer linked above.
Another good read explaining waiting: How to WebDriverWait
Answers linked in Niels' comment are also worth a read.
Great answers but I feel like for the most part they answer "What should be used" and not as much "What the difference is".
I will be using Java as my example since that's the easiest to dig through the source of.
Explicit Wait
WebDriverWait
WebDriverWait inherits from the FluentWait class. The primary function is in the until method. This method takes a function and applies, if the result is true it will return the value. If the result is false or an exception, it will check if the total time has expired. If so, it will throw a timeoutException. If not, it will sleep for the polling time and then try again.
It is pretty simple and straightforward. Nothing overly fancy. It also has unit tests which you can verify here.
Implicit Wait
In the RemoteWebDriver the implicitlyWait is defined by calling DriverCommand.SET_TIMEOUT. This processes into an HttpRequest calling (to the best of my current knowledge, no debuggers hooked up to confirm) the ConfigureTimeout class. From here, I believe, it is passed into the WebDriver and the WebDriver manages it.
Some implicitWait tests.
The Difference
The difference is ..... well .... We don't know the implementation. The implementation within IE might be identical to the implementation in Selenium. Unlikely, but might be. By having the consistenty with the WebDriverWait and hhaving it controlled outside of the Driver, it is giving us a consistent and reliable execution across all browsers.
Why choose Explicit
The issue with Implicit Wait is that it can not be defined. It may be different on Chrome than Firefox and IE. This causes inconsistencies in your tests based on the different implementation within the driver itself. So the choice is between a behavior which is not clearly defined which may vary versus a clearly defined consistent behavior. I would choose the latter, personally.
Simple words: Implicit wait allows you to wait for a fixed timeout.
Manage ().Timeouts ().ImplicitlyWait (TimeSpan.FromSeconds(10));
The above line will enforce implicit wait for 10 seconds and then execute further code.
Whereas, Explicit wait allows you to wait until a certain condition is met e.g. a page is completely loaded, or an element is visible.
WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.
Webdriver wait = new WebdriverWait(driver, 10); wait.Until(ExpectedConditions.ElementIsVisible(By.Id("someElement"));
The above line waits for 10 seconds to check if element with id 'someElement' is visible, if it is not found it throws exception and if its found further code will be executed.