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.
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.
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()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and opens Chrome browser | Browser window opens at 'https://the-internet.herokuapp.com/javascript_alerts' | - | PASS |
| 2 | Finds the button with text 'Click for JS Prompt' and clicks it | Prompt alert dialog appears on screen | - | PASS |
| 3 | Waits up to 10 seconds for alert to be present | Alert is present and active | - | PASS |
| 4 | Switches to alert, sends text 'Hello Test', and accepts the alert | Alert closes, page shows updated result text | - | PASS |
| 5 | Finds element with id 'result' and verifies its text equals 'You entered: Hello Test' | Page displays confirmation message with entered text | Assert that result text is exactly 'You entered: Hello Test' | PASS |
| 6 | Test ends and browser closes | Browser window closes | - | PASS |