Test Overview
This test opens a web page with a prompt alert, enters text into the prompt, accepts it, and verifies the result message on the page.
This test opens a web page with a prompt alert, enters text into the prompt, accepts it, and verifies the result message on the page.
import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; 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 PromptAlertTest { private WebDriver driver; private WebDriverWait wait; @BeforeEach public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); } @Test public void testPromptAlertTextEntry() { driver.get("https://the-internet.herokuapp.com/javascript_alerts"); driver.findElement(By.xpath("//button[text()='Click for JS Prompt']")).click(); Alert promptAlert = wait.until(ExpectedConditions.alertIsPresent()); promptAlert.sendKeys("Selenium Test"); promptAlert.accept(); String resultText = driver.findElement(By.id("result")).getText(); assertEquals("You entered: Selenium Test", resultText); } @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://the-internet.herokuapp.com/javascript_alerts | Page with JavaScript alerts buttons is loaded | Page title or URL can be verified (implicit) | PASS |
| 3 | Finds button with text 'Click for JS Prompt' and clicks it | Prompt alert dialog appears on screen | Alert presence is detected by WebDriverWait | PASS |
| 4 | Switches to alert, enters text 'Selenium Test' into prompt | Prompt alert contains input text 'Selenium Test' | Alert is interactable and accepts text input | PASS |
| 5 | Accepts the prompt alert | Alert disappears, page updates result text | Alert is accepted successfully | PASS |
| 6 | Finds element with id 'result' and reads its text | Result text is visible on page | Result text equals 'You entered: Selenium Test' | PASS |
| 7 | Test ends and browser closes | Browser window is closed | - | PASS |