0
0
Selenium Pythontesting~10 mins

Prompt alert with text input in Selenium Python - 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 - Selenium
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import unittest

class TestPromptAlert(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://the-internet.herokuapp.com/javascript_alerts')

    def test_prompt_alert_input(self):
        driver = self.driver
        # Click the button that triggers the prompt alert
        prompt_button = driver.find_element(By.XPATH, '//button[text()="Click for JS Prompt"]')
        prompt_button.click()

        # Wait for alert to be present
        WebDriverWait(driver, 10).until(EC.alert_is_present())

        # Switch to alert and send text
        alert = driver.switch_to.alert
        alert.send_keys('Hello Test')
        alert.accept()

        # Verify the result text
        result = driver.find_element(By.ID, 'result').text
        self.assertEqual(result, 'You entered: Hello Test')

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and opens Chrome browserBrowser window opens at 'https://the-internet.herokuapp.com/javascript_alerts'-PASS
2Finds the button with text 'Click for JS Prompt' and clicks itPrompt alert dialog appears on screen-PASS
3Waits up to 10 seconds for alert to be presentAlert is present and active-PASS
4Switches to alert, sends text 'Hello Test', and accepts the alertAlert closes, page shows updated result text-PASS
5Finds element with id 'result' and verifies its text equals 'You entered: Hello Test'Page displays confirmation message with entered textAssert that result text is exactly 'You entered: Hello Test'PASS
6Test ends and browser closesBrowser window closes-PASS
Failure Scenario
Failing Condition: Alert does not appear or text in result does not match expected
Execution Trace Quiz - 3 Questions
Test your understanding
What action triggers the prompt alert in this test?
ANavigating to the page URL
BClicking the button labeled 'Click for JS Prompt'
CSending keys to the alert
DWaiting for the alert to appear
Key Result
Always wait explicitly for alerts before interacting with them to avoid timing issues, and verify the page updates after alert actions.