0
0
Selenium Pythontesting~8 mins

Docker-based Grid in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Docker-based Grid
Folder Structure
selenium-python-docker-grid/
├── tests/
│   ├── test_login.py
│   ├── test_search.py
│   └── __init__.py
├── pages/
│   ├── login_page.py
│   ├── search_page.py
│   └── __init__.py
├── utils/
│   ├── selenium_helpers.py
│   ├── wait_utils.py
│   └── __init__.py
├── config/
│   ├── config.yaml
│   └── __init__.py
├── docker/
│   ├── docker-compose.yml
│   └── selenium-hub/
│       ├── hub.json
│       └── node-chrome.json
├── conftest.py
├── requirements.txt
└── README.md
Test Framework Layers
  • Docker Grid Layer: Runs Selenium Hub and browser nodes inside Docker containers for parallel and scalable test execution.
  • Driver Layer: Uses Selenium WebDriver configured to connect remotely to the Docker Selenium Hub.
  • Page Object Layer: Contains page classes representing UI pages with methods to interact with elements.
  • Test Layer: Contains test scripts using pytest that call page objects and assert expected behaviors.
  • Utility Layer: Helper functions for waits, logging, and driver setup to keep tests clean and reusable.
  • Configuration Layer: YAML files and fixtures to manage environment variables, browser options, and credentials.
Configuration Patterns
  • Environment Config: Use config/config.yaml to define URLs, browser types, and grid hub address.
  • Browser Setup: In conftest.py, read config and create remote WebDriver sessions pointing to Docker Selenium Hub.
  • Credentials: Store sensitive data in environment variables or secure vaults, accessed via Python os.environ.
  • Docker Compose: docker/docker-compose.yml defines services for selenium-hub and browser nodes (chrome, firefox).
  • Fixture Usage: Pytest fixtures manage driver lifecycle and configuration injection into tests.
Test Reporting and CI/CD Integration
  • Use pytest built-in reporting with options for verbose and junit XML output for CI systems.
  • Integrate with CI tools like GitHub Actions, Jenkins, or GitLab CI to run tests on code push.
  • CI pipeline starts Docker Selenium Grid using docker-compose up -d before tests.
  • After tests, collect reports and stop Docker containers to clean up.
  • Optional: Use Allure or HTML reports for better visualization of test results.
Best Practices
  • Use Page Object Model: Keep UI interactions in page classes to separate test logic from UI details.
  • Explicit Waits: Use Selenium explicit waits to handle dynamic page elements reliably.
  • Isolate Tests: Each test should create and quit its own WebDriver session to avoid state leaks.
  • Parameterize Tests: Use pytest parametrization to run tests across multiple browsers and environments.
  • Keep Docker Images Updated: Regularly update Selenium Docker images to get latest browser and driver fixes.
Self Check

Where in this folder structure would you add a new page object class for the "User Profile" page?

Key Result
A Docker-based Selenium Grid framework uses containerized Selenium Hub and nodes with Python pytest and Page Object Model for scalable, maintainable UI testing.