Prompt alert text entry in Selenium Java - Build an Automation Script
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.
Now add data-driven testing with 3 different input texts for the prompt alert
Practice
Solution
Step 1: Understand what a prompt alert is
A prompt alert is a popup that asks the user to enter some text.Step 2: Compare with other alert types
Unlike simple alerts or confirmation alerts, prompt alerts allow text input.Final Answer:
To allow the user to enter text in a popup window -> Option AQuick Check:
Prompt alert = text input popup [OK]
- Confusing prompt alerts with simple alerts
- Thinking prompt alerts only show messages
- Assuming prompt alerts cannot accept input
Solution
Step 1: Recall Selenium Alert interface methods
The Alert interface provides sendKeys() to input text into prompt alerts.Step 2: Verify method names
Only sendKeys() is the correct method; others do not exist in Selenium Alert API.Final Answer:
alert.sendKeys("text") -> Option AQuick Check:
Use sendKeys() to input text [OK]
- Using non-existent methods like enterText()
- Trying to use setText() which is not in Alert API
- Confusing sendKeys() with WebElement methods
Alert alert = driver.switchTo().alert();
alert.sendKeys("hello");
alert.accept();Solution
Step 1: Analyze alert switching and text entry
The code switches to the alert, sends text 'hello' to the prompt alert.Step 2: Understand alert acceptance
Calling accept() closes the alert by clicking OK, confirming the input.Final Answer:
The prompt alert will receive 'hello' and then close by accepting -> Option BQuick Check:
sendKeys + accept = input text and confirm [OK]
- Assuming alert closes without text input
- Expecting dismiss() to be called instead of accept()
- Not switching to alert before sendKeys()
Alert alert = driver.switchTo().alert();
alert.sendKeys("test");
alert.dismiss();Solution
Step 1: Check alert method usage
sendKeys() inputs text, but dismiss() cancels the prompt, ignoring input.Step 2: Correct method to confirm input
accept() should be used to confirm and submit the entered text.Final Answer:
Using dismiss() instead of accept() after sendKeys() -> Option DQuick Check:
Use accept() to confirm prompt input [OK]
- Calling dismiss() cancels input silently
- Confusing alert with WebElement
- Forgetting to switch to alert before sendKeys()
Solution
Step 1: Understand alert handling best practice
Store the alert in a variable to reuse it for sendKeys() and accept().Step 2: Check order of operations
First sendKeys() to enter text, then accept() to confirm and close alert.Final Answer:
Alert alert = driver.switchTo().alert(); alert.sendKeys("user123"); alert.accept(); -> Option CQuick Check:
Store alert, sendKeys, then accept [OK]
- Calling accept() before sendKeys()
- Using dismiss() which cancels input
- Calling switchTo().alert() twice unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
