What's the difference between implicitly wait an explicitly wait?

What's the difference between implicitly wait an explicitly wait?

Implicit wait and explicit wait are two different mechanisms used in test automation frameworks to handle synchronization issues between test scripts and the application being tested.

Implicit Wait:

  1. Implicit wait is a global setting applied to all elements in the test script. When an implicit wait is set, the web driver will wait for a certain amount of time before throwing an exception if the element is not immediately available. It is applied throughout the entire execution of the test script, so it affects all subsequent element searches.

Explicitly Wait

  1. Explicit wait, on the other hand, is a more fine-grained approach where the wait condition is applied only to specific elements or actions in the test script. With explicit wait, you can define a specific condition and the maximum amount of time to wait for that condition to be satisfied. It gives you more control over waiting for a specific element or event to occur.

Here are the examples of Implicit Wait and Explicit Wait in Java using Selenium WebDriver:

  1. Implicit Wait:

java

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;


public class ImplicitWaitExample {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Set implicit wait to 10 seconds


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


        // Find and interact with elements
        driver.findElement(By.id("username")).sendKeys("testuser");
        driver.findElement(By.id("password")).sendKeys("password");
        driver.findElement(By.id("loginButton")).click();


        // Rest of the test script...
        driver.quit();
    }
}
        

In the above example, an implicit wait of 10 seconds is set using driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS). This means that if an element is not immediately found, the driver will wait up to 10 seconds before throwing an exception.

  1. Explicit Wait:

java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;


public class ExplicitWaitExample {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        WebDriverWait wait = new WebDriverWait(driver, 10); // Set explicit wait to 10 seconds


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


        // Wait for element to be visible and interact with it
        WebElement usernameField = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
        usernameField.sendKeys("testuser");


        // Wait for element to be clickable and interact with it
        WebElement loginButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("loginButton")));
        loginButton.click();


        // Rest of the test script...
        driver.quit();
    }
}        

In the above example, an explicit wait of 10 seconds is set using WebDriverWait and ExpectedConditions. The wait.until() method waits for a specific condition to be met before proceeding. In this case, we wait for the username field to be visible and then interact with it, and we also wait for the login button to be clickable before clicking on it.

Note: Make sure to have the Selenium WebDriver and required dependencies properly configured in your Java project for the above code examples to work correctly.

To summarize, the main differences between implicit and explicit waits are:

  • Scope: Implicit wait is applied globally to all elements, while explicit wait can be applied selectively to specific elements or conditions.
  • Flexibility: Explicit wait allows you to define custom conditions and maximum wait times, providing more control and flexibility.
  • Wait Time: Implicit wait has a fixed wait time for all elements, whereas explicit wait can have different wait times for different elements or conditions.

In general, it's recommended to use explicit wait when you need more precise control over waiting for specific elements or conditions, while implicit wait can be used for simpler scenarios where a global wait time suffices.


#testengineer #qatester #java #seleniumwebdriver

To view or add a comment, sign in

More articles by Semih Aldemir

Explore content categories