Test Overview
This test opens a webpage with a button that triggers a simple alert popup. It verifies that the alert appears and accepts it successfully.
This test opens a webpage with a button that triggers a simple alert popup. It verifies that the alert appears and accepts it successfully.
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 TestSimpleAlertAcceptance(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://the-internet.herokuapp.com/javascript_alerts') def test_accept_simple_alert(self): driver = self.driver # Click the button that triggers the alert button = driver.find_element(By.XPATH, '//button[text()="Click for JS Alert"]') button.click() # Wait for the alert to be present WebDriverWait(driver, 10).until(EC.alert_is_present()) # Switch to alert and accept it alert = driver.switch_to.alert alert_text = alert.text alert.accept() # Verify the alert text is as expected self.assertEqual(alert_text, 'I am a JS Alert') # Verify the result text on the page after accepting alert result = driver.find_element(By.ID, 'result').text self.assertEqual(result, 'You successfully clicked an alert') def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window is open, ready to load URL | - | PASS |
| 2 | Navigates to 'https://the-internet.herokuapp.com/javascript_alerts' | Page with JavaScript Alerts loaded, showing buttons | - | PASS |
| 3 | Finds the button with text 'Click for JS Alert' using XPath | Button element located on the page | - | PASS |
| 4 | Clicks the alert trigger button | JavaScript alert popup appears with message | - | PASS |
| 5 | Waits up to 10 seconds for alert to be present using WebDriverWait and expected_conditions | Alert is present and active | - | PASS |
| 6 | Switches to alert and reads alert text | Alert text captured as 'I am a JS Alert' | Assert alert text equals 'I am a JS Alert' | PASS |
| 7 | Accepts the alert (clicks OK) | Alert disappears, page updates result text | - | PASS |
| 8 | Finds the result element by ID 'result' and reads its text | Result text is 'You successfully clicked an alert' | Assert result text equals 'You successfully clicked an alert' | PASS |
| 9 | Test ends and browser closes | Browser window closed | - | PASS |