0
0
Selenium Javatesting~10 mins

Explicit wait (WebDriverWait) in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a webpage, waits explicitly for a button to become clickable using WebDriverWait, clicks it, and verifies the expected result appears.

Test Code - JUnit with Selenium WebDriver
Selenium 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 org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.time.Duration;

public class ExplicitWaitTest {
    WebDriver driver;
    WebDriverWait wait;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    }

    @Test
    public void testButtonClickWithExplicitWait() {
        driver.get("https://example.com/wait-demo");

        // Wait until the button with id 'delayed-button' is clickable
        WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("delayed-button")));

        button.click();

        // Verify that clicking the button shows the message with id 'result-message'
        WebElement message = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("result-message")));
        String text = message.getText();
        assertEquals("Button clicked!", text);
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensChrome browser window is open and ready-PASS
2Navigates to https://example.com/wait-demoPage loads with a delayed button that appears after some time-PASS
3Waits explicitly up to 10 seconds for the button with id 'delayed-button' to become clickable using WebDriverWaitButton becomes visible and clickable on the pageChecks that the button is clickable before proceedingPASS
4Clicks the 'delayed-button'Button is clicked, triggering a message to appear-PASS
5Finds the element with id 'result-message' and reads its textMessage 'Button clicked!' is displayed on the pageVerifies the message text equals 'Button clicked!'PASS
6Test ends and browser closesBrowser window is closed-PASS
Failure Scenario
Failing Condition: The button with id 'delayed-button' does not become clickable within 10 seconds
Execution Trace Quiz - 3 Questions
Test your understanding
What does the WebDriverWait in this test do?
AWaits for the page to load completely
BWaits until the button is clickable before clicking it
CWaits for the browser to open
DWaits for the test to finish
Key Result
Using explicit waits like WebDriverWait helps tests wait only as long as needed for elements to be ready, making tests more reliable and faster than fixed delays.