Verify browser window size can be set and retrieved
Preconditions (3)
✅ Expected Result: The browser window size is set to 1024x768 and verified successfully before closing the browser
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options import unittest class TestWindowSize(unittest.TestCase): def setUp(self): options = Options() # options.add_argument('--headless') # Uncomment to run headless self.driver = webdriver.Chrome(options=options) def test_window_size(self): driver = self.driver driver.get('https://example.com') # Set window size driver.set_window_size(1024, 768) # Get window size size = driver.get_window_size() # Assertions self.assertEqual(size['width'], 1024, f"Expected width 1024 but got {size['width']}") self.assertEqual(size['height'], 768, f"Expected height 768 but got {size['height']}") def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
This test uses Python's unittest framework with Selenium WebDriver.
setUp creates the browser instance before each test.
test_window_size navigates to example.com, sets the window size to 1024x768, then retrieves the size and asserts both width and height match the expected values.
tearDown closes the browser after the test to clean up.
This structure ensures the browser is always closed even if the test fails. Assertions include messages to help identify issues if the size is incorrect.
Now add data-driven testing with 3 different window sizes: (800x600), (1280x720), and (1920x1080)
set_window_size(width, height) do in a test script?set_window_size(width, height) is designed to set the browser window to a specific size.maximize_window(), close(), or refresh().set_window_size(width, height) with underscore and lowercase.driver.set_window_size(1024, 768).driver.set_window_size(800, 600) size = driver.get_window_size() print(size)
get_window_size() returns a dictionary with keys 'width' and 'height' reflecting the current window size.driver.set_window_size(1024)
set_window_size requires two parameters: width and height.set_window_size(w, h).setWindowSize (incorrect for Python Selenium). sizes = [(320, 480), (768, 1024), (1920, 1080)]
for size in sizes:
driver.set_window_size(size) loops but passes tuples directly (TypeError, expects two arguments).Pass the test and this pattern moves unknown