Types of wait statement in selenium
In Selenium WebDriver, waits are crucial for synchronizing your test automation scripts with the web application’s behavior. There are three main types of waits in Selenium:
1. Implicit Wait
- Purpose: Tells WebDriver to poll the DOM for a certain time when trying to find any element (or elements) if they are not immediately available.
- Scope: Applies globally for the WebDriver instance.
- Syntax (Java):
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
Notes:
- Once set, it’s applied for the entire life of the WebDriver instance.
- Not recommended for dynamic and complex UIs as it may cause unnecessary delays.
Use when:
- Your application is generally stable and all elements load at a predictable speed.
- You want a simple, global wait for all element searches.
- You’re writing small, simple test scripts.
Example Scenarios:
- Static websites where all elements load at once.
- Quick demos or POCs where you don’t want to add many waits.
2. Explicit Wait
- Purpose: Waits for a certain condition to occur before proceeding further in the code.
- Scope: Applied only to specified elements/conditions.
Syntax (Java):
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("myElement")));
Usage Examples:
- Wait until element is visible/clickable/present.
- Wait for title, alerts, URLs, text, etc.
Use when:
- Elements load dynamically or with AJAX calls.
- You need to wait for a specific condition (visibility, clickable, text present, etc.).
- The timing for certain elements is unpredictable.
Example Scenarios:
- Waiting for a login button to become clickable after entering credentials.
- Waiting for a success message to appear after form submission.
- Waiting for a loading spinner to disappear.
- Waiting for an element’s text to change after an action.
3. Fluent Wait
- Purpose: Similar to explicit wait, but gives more flexibility (polling frequency, ignoring exceptions).
- Syntax (Java):
import org.openqa.selenium.support.ui.FluentWait;
FluentWait<WebDriver> wait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(30))
.pollingEvery(Duration.ofSeconds(2))
.ignoring(NoSuchElementException.class);
WebElement element = wait.until(driver -> driver.findElement(By.id("myElement")));
Usage:
- When you need to customize polling intervals and exception handling.
Use when:
- Elements appear after a variable amount of time.
- You want to customize polling interval and ignore certain exceptions (like NoSuchElementException).
- The web page is highly dynamic (e.g., single-page applications, dashboards).
- Retrying logic is needed before failing.
Example Scenarios:
- Waiting for a price to update on a stock ticker.
- Waiting for a chart or graph to load on a dashboard.
- Handling slow or flaky network responses where elements may appear intermittently.
Summary Table
Wait Type | Applied To | Use Case | Example Syntax |
Implicit | All element searches | Default wait for all finds | implicitlyWait(Duration.ofSeconds(10)) |
Explicit | Specific condition | Wait for condition on an element or value | WebDriverWait + ExpectedConditions |
Fluent | Specific condition | Fine-tune polling, exception handling | FluentWait + pollingEvery + ignoring |
Note:
Thread.sleep()
- Not recommended for automation (static wait).
- Example: Thread.sleep(2000); pauses for 2 seconds, regardless of page state.
Not recommended except for debugging or in rare cases (like demo, showing the UI, or initial waits before starting a test).
Best Practice:
Use explicit or fluent waits for most real-world web automation. Reserve implicit wait only for simple scenarios.