0
0
Selenium Pythontesting~8 mins

Chrome configuration in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Chrome configuration
Folder Structure
selenium-python-project/
├── src/
│   ├── pages/
│   │   └── login_page.py
│   ├── tests/
│   │   └── test_login.py
│   ├── utils/
│   │   └── chrome_options.py
│   └── config/
│       └── config.yaml
├── drivers/
│   └── chromedriver.exe (or chromedriver for Linux/Mac)
├── requirements.txt
└── pytest.ini
Test Framework Layers
  • Driver Layer: Manages the Chrome WebDriver setup and teardown using selenium.webdriver.Chrome.
  • Page Objects: Python classes representing web pages, using locators and methods to interact with page elements.
  • Tests: Pytest test functions or classes that use page objects and driver instances to perform test scenarios.
  • Utilities: Helper modules like chrome_options.py to configure ChromeDriver options (headless mode, window size, etc.).
  • Configuration: YAML or similar files to store environment variables, browser preferences, and credentials.
Configuration Patterns

Use a config.yaml file to store settings like:

  • Browser options (e.g., headless: true/false)
  • Window size
  • Implicit wait times
  • URLs and credentials

Example chrome_options.py utility to read config and apply Chrome options:

from selenium.webdriver.chrome.options import Options
import yaml

class ChromeOptionsFactory:
    @staticmethod
    def get_options(config_path='src/config/config.yaml') -> Options:
        options = Options()
        with open(config_path, 'r') as file:
            config = yaml.safe_load(file)
        chrome_cfg = config.get('chrome', {})

        if chrome_cfg.get('headless', False):
            options.add_argument('--headless=new')  # modern headless mode
        if window_size := chrome_cfg.get('window_size'):
            options.add_argument(f'--window-size={window_size}')
        if chrome_cfg.get('disable_gpu', False):
            options.add_argument('--disable-gpu')
        # Add more options as needed

        return options

In test setup, use this factory to create the driver:

from selenium import webdriver
from src.utils.chrome_options import ChromeOptionsFactory

def create_driver():
    options = ChromeOptionsFactory.get_options()
    driver = webdriver.Chrome(options=options)
    driver.implicitly_wait(10)  # seconds
    return driver
Test Reporting and CI/CD Integration
  • Use pytest with plugins like pytest-html to generate readable HTML reports.
  • Configure CI pipelines (GitHub Actions, Jenkins, GitLab CI) to run tests on code push or pull requests.
  • Store ChromeDriver binaries in drivers/ and ensure CI agents have compatible Chrome versions.
  • Use environment variables or config files to switch between headless and headed modes in CI vs local runs.
Best Practices for Chrome Configuration Framework
  1. Separate configuration from code: Keep Chrome options and environment settings in config files, not hardcoded.
  2. Use explicit waits: Avoid relying only on implicit waits; use WebDriverWait for better stability.
  3. Support headless mode: Enable headless Chrome for faster CI runs but allow easy switching to headed mode for debugging.
  4. Keep drivers updated: Match ChromeDriver version with installed Chrome browser to avoid compatibility issues.
  5. Use Page Object Model: Encapsulate page interactions to keep tests clean and maintainable.
Self Check

Where in this folder structure would you add a new utility to configure Chrome browser options for different environments?

Key Result
Organize Chrome WebDriver setup using config files and utility classes for flexible, maintainable test automation.