Types of Waits in Selenium: Usage and Examples

Types of Waits in Selenium: Usage and Examples

In Selenium, waits are used to manage the timing issues that can arise when automating web interactions.

There are primarily three types of waits:

1. Implicit Wait

2. Explicit Wait

3. Fluent Wait

Implicit Wait

Definition: Implicit Wait tells WebDriver to wait for a certain amount of time before throwing a NoSuchElementException. It sets a default wait time between each consecutive test step/command across the entire test script.

When to Use: When you want to set a default wait time for the entire duration of the WebDriver instance. Useful for scenarios where the elements take a fixed time to load.

Example:

WebDriver driver = new ChromeDriver();

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(20));

driver.get("https://google.com");

WebElement element = driver.findElement(By.id("Id"));

element.click();        

Explicit Wait

Definition: Explicit Wait is used to wait for a certain condition to occur before proceeding further in the code. This type of wait is specific to a particular element.

When to Use: When you need more control over the wait conditions, such as waiting for an element to be visible, clickable, present, etc. Useful for scenarios where the wait time might vary for different elements.

Example:

WebDriver driver = new ChromeDriver();

driver.get("https://example.com");

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

WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("someId")));

element.click();        

Fluent Wait

Definition: Fluent Wait is similar to Explicit Wait but with more flexibility. It allows you to specify the frequency with which the condition is checked before throwing a TimeoutException. You can also ignore specific types of exceptions while waiting.

When to Use: When you need to define a custom polling frequency and exceptions to ignore. Useful for scenarios where the element load times are unpredictable and you need more granular control over the waiting mechanism.

Example:

WebDriver driver = new ChromeDriver();

driver.get("https://example.com");

Wait<WebDriver> wait =
        new FluentWait<>(driver)
            .withTimeout(Duration.ofSeconds(2))
            .pollingEvery(Duration.ofMillis(300))
            .ignoring(ElementNotInteractableException.class);

    wait.until(
        d -> {
          revealed.sendKeys("Displayed");
          return true;
        });        

Conclusion:

- Implicit Wait: Sets a default wait time for the entire WebDriver instance. Simple and easy to use, but less flexible.

- Explicit Wait: Waits for specific conditions to be met before proceeding. More flexible and specific to certain elements.

- Fluent Wait: Similar to Explicit Wait but with customizable polling frequency and exception handling. Most flexible, ideal for complex scenarios.

Choosing the right type of wait depends on your specific needs:

- Use Implicit Wait for simple cases where a default wait is sufficient.

- Use Explicit Wait for more control over wait conditions on a per-element basis.

- Use Fluent Wait for advanced scenarios requiring custom polling and exception handling.

Happy Testing!


Dheeraj Kumar Rajak

Quality Analyst | Selenium WebDriver +Java binding | TestNg | Frameworks | Jira | Jenkins | Postman | Appium | Api Testing | SQL | BDD | Cucumber | Browser Stack

5 个月

Thanks for sharing. A few things I want to highlight: 1) FluentWait implements the Wait interface, and WebDriverWait extends FluentWait. Therefore, using FluentWait, we can set custom polling periods and ignore exceptions with specific messages. 2) I could be wrong, but the main difference between WebDriverWait and FluentWait lies in their constructors. WebDriverWait has an overloaded constructor that accepts a duration along with the WebDriver instance, while FluentWait requires method chaining to configure the timeout, polling interval, and exception handling.

要查看或添加评论,请登录

Kushal Parikh的更多文章

社区洞察

其他会员也浏览了