0
0
Selenium Javatesting~15 mins

Prompt alert text entry in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Enter text into a prompt alert and verify the result
Preconditions (1)
Step 1: Click the button that triggers the prompt alert
Step 2: Wait for the prompt alert to appear
Step 3: Enter the text 'HelloTest' into the prompt alert input field
Step 4: Accept the prompt alert
Step 5: Verify that the page displays the entered text 'HelloTest' in the result area
✅ Expected Result: The page shows the text 'HelloTest' confirming the prompt alert input was accepted
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify the prompt alert is displayed before entering text
Verify the entered text appears correctly on the page after accepting the alert
Best Practices:
Use explicit waits to wait for alert presence
Use Alert interface methods to interact with prompt alerts
Use meaningful locators like By.id or By.cssSelector for page elements
Use assertions from a testing framework like TestNG or JUnit
Automated Solution
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.Alert;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.time.Duration;

public class PromptAlertTest {
    WebDriver driver;
    WebDriverWait wait;

    @BeforeClass
    public void setUp() {
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        driver.get("https://example.com/prompt-alert"); // Replace with actual URL
    }

    @Test
    public void testPromptAlertTextEntry() {
        // Click the button that triggers the prompt alert
        WebElement promptButton = driver.findElement(By.id("promptButton"));
        promptButton.click();

        // Wait for the alert to be present
        Alert promptAlert = wait.until(ExpectedConditions.alertIsPresent());

        // Verify alert is displayed
        Assert.assertNotNull(promptAlert, "Prompt alert should be present");

        // Enter text into the prompt alert
        String inputText = "HelloTest";
        promptAlert.sendKeys(inputText);

        // Accept the alert
        promptAlert.accept();

        // Verify the entered text appears on the page
        WebElement resultText = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("result")));
        Assert.assertEquals(resultText.getText(), inputText, "The result text should match the entered prompt text");
    }

    @AfterClass
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

This test script uses Selenium WebDriver with Java and TestNG for assertions.

First, it opens the target page in the setUp method.

The test clicks the button with id promptButton to open the prompt alert.

It waits explicitly for the alert to appear using WebDriverWait and ExpectedConditions.alertIsPresent().

Once the alert is present, it sends the text HelloTest to the prompt input field and accepts the alert.

Finally, it waits for the result element with id result to be visible and asserts that its text matches the entered input.

The tearDown method closes the browser after the test.

This approach ensures the test waits properly for elements and alerts, uses clear locators, and verifies the expected behavior.

Common Mistakes - 4 Pitfalls
Using Thread.sleep() instead of explicit waits for alert presence
{'mistake': 'Trying to find the prompt input field as a normal web element', 'why_bad': 'Prompt alert input is part of the browser alert, not the page DOM, so it cannot be located by normal selectors.', 'correct_approach': "Use the Alert interface's sendKeys() method to enter text into the prompt alert."}
Not verifying the alert is present before interacting
Using brittle locators like absolute XPath for buttons or result text
Bonus Challenge

Now add data-driven testing with 3 different input texts for the prompt alert

Show Hint