0
0
Selenium Pythontesting~15 mins

Browser options and capabilities in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Set Chrome browser options and verify window size
Preconditions (2)
Step 1: Create a ChromeOptions object
Step 2: Set the browser window size to 1024x768 using ChromeOptions
Step 3: Start Chrome browser with these options
Step 4: Navigate to https://www.example.com
Step 5: Verify that the browser window size is 1024x768
✅ Expected Result: The Chrome browser opens with window size 1024x768 and loads https://www.example.com successfully
Automation Requirements - Selenium with Python
Assertions Needed:
Verify browser window size is 1024x768 after browser launch
Verify page title contains 'Example Domain' after navigation
Best Practices:
Use ChromeOptions to set browser capabilities
Use explicit waits if needed
Use assertions from unittest or pytest
Close the browser after test
Automated Solution
Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import unittest

class TestChromeOptions(unittest.TestCase):
    def setUp(self):
        chrome_options = Options()
        chrome_options.add_argument('--window-size=1024,768')
        self.driver = webdriver.Chrome(options=chrome_options)

    def test_window_size_and_navigation(self):
        self.driver.get('https://www.example.com')
        width = self.driver.execute_script('return window.innerWidth')
        height = self.driver.execute_script('return window.innerHeight')
        self.assertEqual(width, 1024, 'Window width should be 1024')
        self.assertEqual(height, 768, 'Window height should be 768')
        self.assertIn('Example Domain', self.driver.title, 'Page title should contain Example Domain')

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()

This test uses ChromeOptions to set the browser window size before launching Chrome. The --window-size=1024,768 argument sets the window size. After launching, the test navigates to https://www.example.com. It then uses JavaScript to get the actual window inner width and height and asserts they match the expected size. It also checks the page title to confirm navigation succeeded. The test uses Python's unittest framework for structure and assertions. The tearDown method ensures the browser closes after the test to keep the environment clean.

Common Mistakes - 3 Pitfalls
Setting window size after browser launch instead of using ChromeOptions
Using hardcoded sleep instead of checking window size dynamically
Not closing the browser after test
Bonus Challenge

Now add data-driven testing to launch Chrome with three different window sizes: 800x600, 1280x720, and 1920x1080, verifying each size.

Show Hint