Test Overview
This test opens a new browser tab and a new browser window, then verifies that the number of open windows/tabs matches the expected count.
This test opens a new browser tab and a new browser window, then verifies that the number of open windows/tabs matches the expected count.
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 TestNewWindowTab(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com') def test_new_tab_and_window(self): driver = self.driver original_windows = driver.window_handles original_count = len(original_windows) # Open new tab driver.execute_script('window.open("https://example.com", "_blank");') WebDriverWait(driver, 10).until(lambda d: len(d.window_handles) == original_count + 1) # Open new window driver.execute_script('window.open("https://example.com", "_blank", "width=600,height=400");') WebDriverWait(driver, 10).until(lambda d: len(d.window_handles) == original_count + 2) # Verify total windows/tabs count self.assertEqual(len(driver.window_handles), original_count + 2) 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 opened at https://example.com with one tab | - | PASS |
| 2 | Get current window handles and count | One window/tab handle present | Count of window handles is 1 | PASS |
| 3 | Execute JavaScript to open a new tab with URL https://example.com | Two tabs open in the same browser window | Wait until window handles count is original + 1 (2) | PASS |
| 4 | Execute JavaScript to open a new window with URL https://example.com and size 600x400 | Two tabs plus one new window open (total 3 windows/tabs) | Wait until window handles count is original + 2 (3) | PASS |
| 5 | Assert that total window handles count equals original count + 2 | Three window handles present | len(driver.window_handles) == original_count + 2 | PASS |
| 6 | Test ends and browser closes | Browser closed, all windows/tabs closed | - | PASS |