Test Overview
This test uses Selenium's ExpectedConditions class to wait until a button is clickable before clicking it. It verifies that the button click leads to the expected page title change.
This test uses Selenium's ExpectedConditions class to wait until a button is clickable before clicking it. It verifies that the button click leads to the expected page title change.
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 ExpectedConditionsTest { WebDriver driver; WebDriverWait wait; @BeforeEach public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); } @Test public void testButtonClickable() { driver.get("https://example.com/buttonpage"); By buttonLocator = By.id("submit-btn"); WebElement button = wait.until(ExpectedConditions.elementToBeClickable(buttonLocator)); button.click(); wait.until(ExpectedConditions.titleIs("Submission Successful")); assertEquals("Submission Successful", driver.getTitle()); } @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/buttonpage | Page with a button having id 'submit-btn' is loaded | - | PASS |
| 3 | Waits until the button with id 'submit-btn' is clickable using ExpectedConditions.elementToBeClickable | Button is visible and enabled for clicking | Button is clickable | PASS |
| 4 | Clicks the button | Button click triggers page change | - | PASS |
| 5 | Waits until the page title is 'Submission Successful' using ExpectedConditions.titleIs | Page title changes to 'Submission Successful' | Page title is exactly 'Submission Successful' | PASS |
| 6 | Asserts that the page title equals 'Submission Successful' | Page title is 'Submission Successful' | assertEquals passes | PASS |
| 7 | Browser closes and test ends | Browser window closed | - | PASS |