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

Dựng front-end bằng Clean Architecture

Khi nhắc đến Clean Architecture, nhiều người thường chỉ nghĩ đến backend – nơi cần cấu trúc rõ ràng để quản lý nghiệp vụ...
Avatar photo Toan Nguyen Thai
5 min read

Khám phá Monorepo: Cách bứt phá quản lý mã…

Trong thế giới phát triển phần mềm hiện đại, việc quản lý mã nguồn đang trở nên phức tạp hơn bao giờ hết. Các...
Avatar photo dan.nguyenhai@ncc.asia
4 min read

Tìm hiểu về RabbitMQ? Ứng dụng của RabbitMQ

RabbitMQ là gì? RabbitMQ là một AMQP message broker hay còn gọi là phần mềm quản lý hàng đợi message. Hiểu đơn giản, đây là...
Avatar photo Duong Tran Duc
2 min read

Leave a Reply

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