Bird
Raised Fist0
Selenium Javatesting~10 mins

Prompt alert text entry in Selenium Java - Test Execution Trace

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a prompt alert in Selenium testing?
easy
A. To allow the user to enter text in a popup window
B. To display a message without user input
C. To confirm an action with OK or Cancel buttons only
D. To automatically close the browser window

Solution

  1. Step 1: Understand what a prompt alert is

    A prompt alert is a popup that asks the user to enter some text.
  2. Step 2: Compare with other alert types

    Unlike simple alerts or confirmation alerts, prompt alerts allow text input.
  3. Final Answer:

    To allow the user to enter text in a popup window -> Option A
  4. Quick Check:

    Prompt alert = text input popup [OK]
Hint: Prompt alerts let users type text in popups [OK]
Common Mistakes:
  • Confusing prompt alerts with simple alerts
  • Thinking prompt alerts only show messages
  • Assuming prompt alerts cannot accept input
2. Which Selenium Java method is used to enter text into a prompt alert?
easy
A. alert.sendKeys("text")
B. alert.enterText("text")
C. alert.type("text")
D. alert.setText("text")

Solution

  1. Step 1: Recall Selenium Alert interface methods

    The Alert interface provides sendKeys() to input text into prompt alerts.
  2. Step 2: Verify method names

    Only sendKeys() is the correct method; others do not exist in Selenium Alert API.
  3. Final Answer:

    alert.sendKeys("text") -> Option A
  4. Quick Check:

    Use sendKeys() to input text [OK]
Hint: Use sendKeys() to type text in prompt alerts [OK]
Common Mistakes:
  • Using non-existent methods like enterText()
  • Trying to use setText() which is not in Alert API
  • Confusing sendKeys() with WebElement methods
3. What will happen if you run this Selenium Java code snippet?
Alert alert = driver.switchTo().alert();
alert.sendKeys("hello");
alert.accept();
medium
A. A NoAlertPresentException will be thrown
B. The prompt alert will receive 'hello' and then close by accepting
C. The alert will close without entering text
D. The alert will dismiss without accepting

Solution

  1. Step 1: Analyze alert switching and text entry

    The code switches to the alert, sends text 'hello' to the prompt alert.
  2. Step 2: Understand alert acceptance

    Calling accept() closes the alert by clicking OK, confirming the input.
  3. Final Answer:

    The prompt alert will receive 'hello' and then close by accepting -> Option B
  4. Quick Check:

    sendKeys + accept = input text and confirm [OK]
Hint: sendKeys + accept inputs text and confirms prompt [OK]
Common Mistakes:
  • Assuming alert closes without text input
  • Expecting dismiss() to be called instead of accept()
  • Not switching to alert before sendKeys()
4. Identify the error in this Selenium Java code for entering text in a prompt alert:
Alert alert = driver.switchTo().alert();
alert.sendKeys("test");
alert.dismiss();
medium
A. alert variable should be WebElement
B. Missing driver initialization
C. sendKeys() cannot be used on alerts
D. Using dismiss() instead of accept() after sendKeys()

Solution

  1. Step 1: Check alert method usage

    sendKeys() inputs text, but dismiss() cancels the prompt, ignoring input.
  2. Step 2: Correct method to confirm input

    accept() should be used to confirm and submit the entered text.
  3. Final Answer:

    Using dismiss() instead of accept() after sendKeys() -> Option D
  4. Quick Check:

    Use accept() to confirm prompt input [OK]
Hint: Use accept() after sendKeys(), not dismiss() [OK]
Common Mistakes:
  • Calling dismiss() cancels input silently
  • Confusing alert with WebElement
  • Forgetting to switch to alert before sendKeys()
5. You want to test a prompt alert that asks for a username. Which Selenium Java code snippet correctly enters "user123" and confirms the alert?
hard
A. driver.switchTo().alert().sendKeys("user123"); driver.switchTo().alert().accept();
B. Alert alert = driver.switchTo().alert(); alert.dismiss(); alert.sendKeys("user123");
C. Alert alert = driver.switchTo().alert(); alert.sendKeys("user123"); alert.accept();
D. driver.switchTo().alert().accept(); driver.switchTo().alert().sendKeys("user123");

Solution

  1. Step 1: Understand alert handling best practice

    Store the alert in a variable to reuse it for sendKeys() and accept().
  2. Step 2: Check order of operations

    First sendKeys() to enter text, then accept() to confirm and close alert.
  3. Final Answer:

    Alert alert = driver.switchTo().alert(); alert.sendKeys("user123"); alert.accept(); -> Option C
  4. Quick Check:

    Store alert, sendKeys, then accept [OK]
Hint: Assign alert to variable before sendKeys and accept [OK]
Common Mistakes:
  • Calling accept() before sendKeys()
  • Using dismiss() which cancels input
  • Calling switchTo().alert() twice unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard