0
0
Selenium Pythontesting~10 mins

Simple alert acceptance in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
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.

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 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()
Execution Trace - 9 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open, ready to load URL-PASS
2Navigates to 'https://the-internet.herokuapp.com/javascript_alerts'Page with JavaScript Alerts loaded, showing buttons-PASS
3Finds the button with text 'Click for JS Alert' using XPathButton element located on the page-PASS
4Clicks the alert trigger buttonJavaScript alert popup appears with message-PASS
5Waits up to 10 seconds for alert to be present using WebDriverWait and expected_conditionsAlert is present and active-PASS
6Switches to alert and reads alert textAlert text captured as 'I am a JS Alert'Assert alert text equals 'I am a JS Alert'PASS
7Accepts the alert (clicks OK)Alert disappears, page updates result text-PASS
8Finds the result element by ID 'result' and reads its textResult text is 'You successfully clicked an alert'Assert result text equals 'You successfully clicked an alert'PASS
9Test ends and browser closesBrowser window closed-PASS
Failure Scenario
Failing Condition: Alert does not appear after clicking the button or alert text is different
Execution Trace Quiz - 3 Questions
Test your understanding
What Selenium method is used to wait for the alert popup to appear?
AWebDriverWait(driver, 10).until(EC.alert_is_present())
Bdriver.find_element(By.ID, 'alert')
Cdriver.switch_to.alert.accept()
Ddriver.get('url')
Key Result
Always wait explicitly for alerts before interacting with them to avoid timing issues and ensure reliable test execution.