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.
This test opens a webpage, waits explicitly for a button to become clickable using WebDriverWait, clicks it, and verifies the expected result appears.
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(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Chrome browser window is open and ready | - | PASS |
| 2 | Navigates to https://example.com/wait-demo | Page loads with a delayed button that appears after some time | - | PASS |
| 3 | Waits explicitly up to 10 seconds for the button with id 'delayed-button' to become clickable using WebDriverWait | Button becomes visible and clickable on the page | Checks that the button is clickable before proceeding | PASS |
| 4 | Clicks the 'delayed-button' | Button is clicked, triggering a message to appear | - | PASS |
| 5 | Finds the element with id 'result-message' and reads its text | Message 'Button clicked!' is displayed on the page | Verifies the message text equals 'Button clicked!' | PASS |
| 6 | Test ends and browser closes | Browser window is closed | - | PASS |