Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests

Implicit wait in Selenium is used to instruct the web driver to wait for a certain amount of time before throwing a ‘NoSuchElementException‘. It is generally applied to all the elements in the web driver instance. This wait time is set once and remains applicable for the entire duration of the WebDriver instance.

Key Points

  • Purpose: To avoid ‘NoSuchElementException‘ by giving the DOM sufficient time to load elements before any action is performed.
  • Scope: It is global and applied to all elements in the test script.
  • Default Value: 0 seconds. If not set, the WebDriver will not wait implicitly.

How Implicit Wait Works

When you set an implicit wait, the WebDriver polls the DOM to find the element until the specified timeout is reached. Once the element is found, the WebDriver proceeds with the next command in the script.

Syntax

The implicit wait is set using the ‘implicitlyWait‘ method of the WebDriver instance.

Example Code

1. Setting Implicit Wait

				
					import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class ImplicitWaitExample {
    public static void main(String[] args) {
        // Set the path for the ChromeDriver executable
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        // Initialize the ChromeDriver
        WebDriver driver = new ChromeDriver();

        // Apply implicit wait
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

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

        // Find an element
        WebElement element = driver.findElement(By.id("elementId"));

        // Perform actions on the element
        element.click();

        // Close the browser
        driver.quit();
    }
}

				
			
  • We set the implicit wait to 10 seconds using driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);.
  • The WebDriver will wait up to 10 seconds before throwing a ‘NoSuchElementException‘ if the element is not immediately available.

2. Implicit Wait in a Framework (e.g., Page Object Model)

In a framework like the Page Object Model (POM), implicit wait can be set in the base class or setup method which is inherited by all page classes.

BaseTest.java

				
					import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class BaseTest {
    protected WebDriver driver;

    public void setUp() {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

				
			

LoginPage.java

				
					import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;

public class LoginPage {
    WebDriver driver;

    public LoginPage(WebDriver driver) {
        this.driver = driver;
    }

    By username = By.id("username");
    By password = By.id("password");
    By loginButton = By.id("login");

    public void login(String user, String pass) {
        driver.findElement(username).sendKeys(user);
        driver.findElement(password).sendKeys(pass);
        driver.findElement(loginButton).click();
    }
}

				
			

LoginTest.java

				
					public class LoginTest extends BaseTest {
    public static void main(String[] args) {
        LoginTest test = new LoginTest();
        test.setUp();

        LoginPage loginPage = new LoginPage(test.driver);
        test.driver.get("https://www.example.com/login");
        loginPage.login("username", "password");

        test.tearDown();
    }
}

				
			
  • The implicit wait is set in the ‘BaseTest‘ class, which is inherited by ‘LoginTest'.
  • This ensures that all elements across the test script are subject to the same implicit wait time.

Important Considerations

  1. Combination with Explicit Waits: While implicit waits can be useful, combining them with explicit waits can sometimes lead to unexpected wait times. It is generally advisable to use explicit waits for specific conditions and elements.
  2. Performance Impact: Using a high implicit wait can slow down test execution because WebDriver waits for the specified time before throwing an exception.
  3. Consistency: Ensure consistency in wait times throughout the test to avoid flaky tests and unpredictable behaviors.

Implicit wait is a fundamental feature in Selenium WebDriver that helps in making tests more robust and reliable by handling dynamic content and elements that may take time to load.

Scroll to Top