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

Using ‘WebDriverWait‘ and ‘ExpectedConditions‘ in Selenium is essential for handling dynamic web pages in Java. These tools help you wait for certain conditions to be met before proceeding with the next steps in your script, thereby enhancing the reliability and robustness of your tests. Below is a detailed explanation with examples.

'WebDriverWait'

WebDriverWait‘ is a specialized form of ‘FluentWait‘ that allows you to set a maximum wait time for a specific condition to be met. If the condition is met before the timeout, the code proceeds; otherwise, it throws a ‘TimeoutException‘.

'ExpectedConditions'

ExpectedConditions‘ is a utility class that provides various conditions which can be waited for, such as the presence of an element, the visibility of an element, the element to be clickable, etc.

Example Scenario

Assume we have a web page where a button, once clicked, will display a message after some time. We want to wait until this message is visible before proceeding.

Maven Dependencies

First, ensure you have the necessary dependencies in your ‘pom.xml‘ if you’re using Maven:

				
					<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.1.0</version>
    </dependency>
</dependencies>

				
			

Example Code

Here is an example of using ‘WebDriverWait‘ and ‘ExpectedConditions‘ in Java:

				
					import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class SeleniumWaitExample {

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

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

        // Navigate to the web page
        driver.get("http://example.com");

        // Locate the button and click it
        WebElement button = driver.findElement(By.id("startButton"));
        button.click();

        // Create an instance of WebDriverWait with a timeout of 10 seconds
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

        // Wait until the message is visible
        WebElement message = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("message")));

        // Print the message text
        System.out.println("Message: " + message.getText());

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

				
			

Explanation

1. Set up WebDriver:

				
					System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();

				
			

2. Navigate to the web page:

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

				
			

3. Locate the button and click it:

				
					WebElement button = driver.findElement(By.id("startButton"));
button.click();

				
			

4. Create an instance of 'WebDriverWait':

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

				
			

5. Wait until the message is visible:

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

				
			

6. Print the message text:

				
					System.out.println("Message: " + message.getText());

				
			

7. Close the browser:

				
					driver.quit();

				
			

Common ExpectedConditions

Here are some commonly used 'ExpectedConditions':

elementToBeClickable:

				
					WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("elementId")));

				
			

presenceOfElementLocated:

				
					WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("elementId")));

				
			

visibilityOf:

				
					WebElement element = wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("elementId"))));

				
			

alertIsPresent:

				
					Alert alert = wait.until(ExpectedConditions.alertIsPresent());

				
			

titleContains:

				
					Boolean titleContains = wait.until(ExpectedConditions.titleContains("partialTitle"));

				
			

Using ‘WebDriverWait‘ and ‘ExpectedConditions‘ in Selenium helps in creating robust and reliable automated tests by handling the dynamic nature of web applications. By waiting for specific conditions, you can avoid issues like ‘NoSuchElementException‘ and ‘StaleElementReferenceException‘.

Scroll to Top