Craig Gallagher
Craig Gallagher

Reputation: 1633

Implicit wait Selenium

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);

Upvotes: 0

Views: 4859

Answers (1)

Jordan
Jordan

Reputation: 659

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.

Upvotes: 3

Related Questions