0
0
Selenium Javatesting~10 mins

Prompt alert text entry in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - JUnit with Selenium WebDriver
Selenium Java
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();
        }
    }
}
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensChrome browser window is open and ready-PASS
2Navigates to https://the-internet.herokuapp.com/javascript_alertsPage with JavaScript alerts buttons is loadedPage title or URL can be verified (implicit)PASS
3Finds button with text 'Click for JS Prompt' and clicks itPrompt alert dialog appears on screenAlert presence is detected by WebDriverWaitPASS
4Switches to alert, enters text 'Selenium Test' into promptPrompt alert contains input text 'Selenium Test'Alert is interactable and accepts text inputPASS
5Accepts the prompt alertAlert disappears, page updates result textAlert is accepted successfullyPASS
6Finds element with id 'result' and reads its textResult text is visible on pageResult text equals 'You entered: Selenium Test'PASS
7Test ends and browser closesBrowser window is closed-PASS
Failure Scenario
Failing Condition: Prompt alert does not appear or text input fails
Execution Trace Quiz - 3 Questions
Test your understanding
What Selenium method is used to enter text into the prompt alert?
AsendKeys() on WebElement
BsendKeys() on Alert object
CsetText() on Alert object
Dtype() on WebElement
Key Result
Always use explicit waits like WebDriverWait to handle alerts reliably before interacting with them.