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.