Test Overview
This test opens a browser, navigates to a simple page, clicks a button, and verifies the result. It shows how controlling the browser step-by-step drives the test flow.
This test opens a browser, navigates to a simple page, clicks a button, and verifies the result. It shows how controlling the browser step-by-step drives the test flow.
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 BrowserControlTest { WebDriver driver; WebDriverWait wait; @BeforeEach public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); } @Test public void testButtonClickChangesText() { driver.get("https://example.com/testpage"); WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("changeTextBtn"))); button.click(); WebElement message = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("message"))); String text = message.getText(); assertEquals("Text changed!", text); } @AfterEach public void tearDown() { driver.quit(); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and ChromeDriver is initialized | Browser window opens, ready for commands | - | PASS |
| 2 | Browser navigates to https://example.com/testpage | Page loads with a button labeled 'Change Text' and a hidden message element | Wait until button with id 'changeTextBtn' is clickable | PASS |
| 3 | Find button by id 'changeTextBtn' and click it | Button is clicked, page updates message text | - | PASS |
| 4 | Wait for message element with id 'message' to be visible | Message element appears with text 'Text changed!' | Verify message text equals 'Text changed!' | PASS |
| 5 | Close browser and end test | Browser window closes | - | PASS |