0

I've a number of tests and using selenium to run them. I've looked a mixed reviews when search for what and where to use implicit waits. Should it only be used when initialising the test or should it be used anytime you want to implicitly wait for an element to be found?

driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);
Jordan
  • 639
  • 1
  • 13
  • 30
Craig Gallagher
  • 1,613
  • 6
  • 23
  • 52

1 Answers1

3

Implicit waits should really only be used when initializing your driver(if ever). Explicit waits are much easier to track when debugging and are designed to be more fine-grained, such as inside a Page Object.

Setting an implicit wait time on your driver has a global effect on your wait times while keeping the setting fairly hidden from the consumer or future maintainer. This can be problematic especially when paired with explicit wait times via WebDriverWait. You could end up with unexpected additions to your wait times.

Here's an example of an explicit wait:

var webDriverWait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));

webDriverWait.Until(ExpectedConditions.ElementExists(By.Id("testId"));

A more thorough comparison of the pros and cons between the two and when to use them can be found here.

Jordan
  • 639
  • 1
  • 13
  • 30
  • Thank you for your explanation and your answer. Where should I put this code `var webDriverWait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));` – Craig Gallagher Jul 06 '17 at 15:47
  • It depends. If using Page Objects, it could be created in something as specific as a single method of UI interaction (e.g., LoginPage.Login()) or inside the class (e.g., LoginPage) if you want the specified time to be uniform everywhere it's used in the Page. Ideally, you want to be as specific as makes sense in your code. – Jordan Jul 06 '17 at 16:00
  • If you want more detail on Implicit vs. Explicit waits - these answers are good - https://stackoverflow.com/a/15174978/631417, https://stackoverflow.com/a/28067495/631417 – Jordan Jul 06 '17 at 16:01
  • Could I declare it in my `[TestClass]` as all my waits are the same – Craig Gallagher Jul 06 '17 at 16:06
  • Waiting should generally not be done in test classes - https://stackoverflow.com/a/18844275/631417 – Jordan Jul 06 '17 at 16:12