Implicit & Explicit Wait

1 min read

1. Implicit Wait

An implicit wait applies universally to all elements in a Selenium script. It pauses for a specified duration before throwing an exception if it can’t find an element. You set this wait once per session and can’t modify it later. The default value is 0.

Set the implicit wait to 10 seconds with this statement right after initializing the WebDriver instance:

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(TIMEOUT));

Note: It’s crucial to remember that failing to set the implicit wait can cause the test to fail.

2. Explicit Wait

We use the ExpectedConditions class to define conditions, such as element presence or absence. If we don’t meet these conditions within the specified timeframe, WebDriver raises an exception.

WebDriver consistently polls at a fixed frequency of 500 ms to verify the expected conditions. Since we don’t globally configure explicit waits, we can employ different conditions and timeouts for each task.

Upon reviewing the previous test case, we realized that without an implicit wait, the test would fail. To address this, we can introduce explicit waits.

To begin, we’ll instantiate a Wait object with a 10-second timeout for our test scenario:

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(TIMEOUT));

Learn more from this original link: Implicit Wait vs Explicit Wait in Selenium Webdriver.

3. Implicit Wait vs. Explicit Wait

  • Timeouts: Implicit wait establishes a default timeout for the entire test runtime, whereas explicit wait sets timeouts for particular conditions.
  • Condition: Implicit wait waits for an element’s appearance on the page, whereas explicit wait waits for specific conditions like element presence or clickability.
  • Scope: Implicit wait operates globally, while explicit wait functions locally for a particular element.
  • Exception: When WebDriver cannot locate the element within the specified timeout, implicit wait triggers a NoSuchElementException. In contrast, explicit wait raises a TimeoutException when the element fails to meet the condition within the designated timeout.

Note: Implicit wait proves useful when waiting for elements to appear on the page after a set duration. On the other hand, explicit wait is preferable when awaiting a precise condition.

Avatar photo

Leave a Reply

Your email address will not be published. Required fields are marked *