0
0
Selenium Pythontesting~8 mins

Fixtures for browser setup/teardown in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Fixtures for browser setup/teardown
Folder Structure
selenium-python-project/
├── tests/
│   ├── test_login.py
│   ├── test_search.py
│   └── conftest.py          # Fixtures for setup/teardown
├── pages/
│   ├── login_page.py
│   └── search_page.py
├── utils/
│   └── helpers.py
├── config/
│   └── config.yaml          # Environment and browser configs
├── reports/
│   └── test_report.html
├── requirements.txt
└── pytest.ini               # Pytest configuration
    
Test Framework Layers
  • Fixtures Layer:
    Located in tests/conftest.py. Contains browser setup and teardown using pytest fixtures. This layer manages browser lifecycle for tests.
  • Page Object Layer:
    Classes in pages/ folder represent web pages. They encapsulate element locators and actions.
  • Test Layer:
    Test scripts in tests/ folder use fixtures and page objects to perform test scenarios.
  • Utilities Layer:
    Helper functions and utilities in utils/ support common tasks like waits or data handling.
  • Configuration Layer:
    Configuration files in config/ hold environment variables, browser options, and credentials.
Configuration Patterns

Use a config.yaml file to store environment URLs, browser types, and credentials. Load this config in fixtures to decide which browser to launch and which environment to test.

Example snippet in conftest.py:

import pytest
import yaml
from selenium import webdriver

@pytest.fixture(scope="session")
def config():
    with open('config/config.yaml') as f:
        return yaml.safe_load(f)

@pytest.fixture(scope="function")
def browser(config):
    browser_name = config['browser']
    if browser_name == 'chrome':
        driver = webdriver.Chrome()
    elif browser_name == 'firefox':
        driver = webdriver.Firefox()
    else:
        raise ValueError(f"Unsupported browser: {browser_name}")
    driver.maximize_window()
    yield driver
    driver.quit()
Test Reporting and CI/CD Integration
  • Use pytest with plugins like pytest-html to generate readable HTML reports saved in reports/.
  • Integrate tests in CI/CD pipelines (e.g., GitHub Actions, Jenkins) to run tests automatically on code push.
  • Configure CI to install dependencies, run tests with fixtures, and archive reports as build artifacts.
  • Use environment variables in CI to switch browsers or environments dynamically.
Best Practices for Fixtures in Selenium Python
  • Scope wisely: Use session scope for config loading and function scope for browser to isolate tests.
  • Explicit teardown: Always quit the browser in fixture teardown to avoid resource leaks.
  • Parameterize fixtures: Allow browser type and environment to be passed via config or command line for flexibility.
  • Keep fixtures simple: Fixtures should only handle setup/teardown, not test logic.
  • Reuse fixtures: Share fixtures across tests by placing them in conftest.py.
Self Check

Where in this framework structure would you add a new fixture to handle user login before tests?

Key Result
Use pytest fixtures in conftest.py to manage browser setup and teardown cleanly and flexibly.