Test Overview
This test opens a web page, clicks a button that opens a pop-up window, switches to the pop-up, verifies its title, closes it, and returns to the main window.
This test opens a web page, clicks a button that opens a pop-up window, switches to the pop-up, verifies its title, closes it, and returns to the main window.
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 TestPopupHandling(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/popup') def test_popup_window(self): driver = self.driver main_window = driver.current_window_handle # Click button that opens popup button = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.ID, 'open-popup-btn')) ) button.click() # Wait for new window WebDriverWait(driver, 10).until( lambda d: len(d.window_handles) > 1 ) # Switch to popup window for handle in driver.window_handles: if handle != main_window: driver.switch_to.window(handle) break # Verify popup title popup_title = driver.title self.assertEqual(popup_title, 'Popup Window') # Close popup driver.close() # Switch back to main window driver.switch_to.window(main_window) # Verify main window title main_title = driver.title self.assertEqual(main_title, 'Main Page') def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and opens browser | Browser opens and navigates to https://example.com/popup | - | PASS |
| 2 | Finds button with ID 'open-popup-btn' and clicks it | Main page displayed with button visible | - | PASS |
| 3 | Waits until a new window appears | Two windows open: main and popup | Check that number of window handles > 1 | PASS |
| 4 | Switches to the popup window | Popup window is active | - | PASS |
| 5 | Checks popup window title equals 'Popup Window' | Popup window title is visible | Assert popup_title == 'Popup Window' | PASS |
| 6 | Closes popup window | Popup window closed, main window still open | - | PASS |
| 7 | Switches back to main window | Main window active | - | PASS |
| 8 | Checks main window title equals 'Main Page' | Main window title visible | Assert main_title == 'Main Page' | PASS |
| 9 | Test ends and browser closes | Browser closed | - | PASS |