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 |
driver.switch_to.new_window() to open new tabs or windows by specifying the type as 'tab' or 'window'.driver.switch_to.new_window('tab').driver.switch_to.new_window() with argument 'window' to open a new window.driver.switch_to.new_window('window') uses the correct method and argument. driver.new_window('window') and driver.open_window('window') use invalid method names, and driver.switch_to.new_window('tab') opens a tab, not a window.driver.get('https://example.com')
original_handle = driver.current_window_handle
driver.switch_to.new_window('tab')
driver.get('https://selenium.dev')
print(len(driver.window_handles))driver.switch_to.new_window('tab'), a new tab opens, so total windows/tabs become 2.driver.window_handles list contains all open windows/tabs. After opening the new tab, its length is 2.driver.get('https://example.com')
driver.new_window('tab')
driver.get('https://selenium.dev')driver.switch_to.new_window('tab'). The code misses switch_to.driver.new_window('tab') directly is invalid and will raise an AttributeError.driver.current_window_handle in a variable to switch back later.driver.switch_to.new_window('tab') to open a new tab, then load the URL with driver.get().driver.switch_to.window(original) to return focus to the original tab.Pass the test and this pattern moves unknown