Test Overview
This test checks that synchronization using explicit waits prevents timing failures by waiting for an element to be visible before clicking it.
This test checks that synchronization using explicit waits prevents timing failures by waiting for an element to be visible before clicking it.
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.assertTrue; import java.time.Duration; public class SynchronizationTest { WebDriver driver; WebDriverWait wait; @BeforeEach public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); } @Test public void testClickAfterElementVisible() { driver.get("https://example.com/delayed_button"); // Wait until the button is visible WebElement button = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("delayedButton"))); button.click(); // Verify that clicking the button shows a success message WebElement message = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("successMessage"))); assertTrue(message.isDisplayed(), "Success message should be displayed after clicking the button"); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window is open, ready to navigate | - | PASS |
| 2 | Navigates to https://example.com/delayed_button | Page loads with a button that appears after a delay | - | PASS |
| 3 | Waits explicitly until the button with id 'delayedButton' is visible | Button becomes visible after some seconds | Checks that the button is visible before proceeding | PASS |
| 4 | Clicks the visible button | Button is clicked, triggering a success message display | - | PASS |
| 5 | Finds the success message element with id 'successMessage' | Success message is present on the page | Verifies that the success message is displayed | PASS |
| 6 | Test ends and browser closes | Browser window is closed | - | PASS |