0
0
Selenium Pythontesting~8 mins

Docker containers for test execution in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Docker containers for test execution
Folder Structure
selenium-python-docker/
├── tests/
│   ├── test_login.py
│   └── test_search.py
├── pages/
│   ├── login_page.py
│   └── search_page.py
├── utils/
│   ├── driver_factory.py
│   └── helpers.py
├── config/
│   ├── config.yaml
│   └── docker-compose.yml
├── Dockerfile
├── requirements.txt
└── README.md
    
Test Framework Layers
  • Driver Layer: utils/driver_factory.py creates Selenium WebDriver instances inside Docker containers.
  • Page Objects: pages/ contains page classes representing UI elements and actions.
  • Tests: tests/ holds test scripts using pytest to run tests against pages.
  • Utilities: utils/ has helper functions and driver setup logic.
  • Configuration: config/config.yaml stores environment and browser settings; config/docker-compose.yml defines container setup.
  • Docker Setup: Dockerfile builds the test environment image with Python, Selenium, and dependencies.
Configuration Patterns

Use config/config.yaml to define environment variables like URLs, browser types, and timeouts. This file is read by tests to adapt behavior.

config/docker-compose.yml configures services such as Selenium Grid or standalone browser containers (e.g., Chrome) and the test runner container.

Example snippet from config.yaml:

url: "https://example.com"
browser: "chrome"
timeout: 10
    

Example snippet from docker-compose.yml:

version: '3'
services:
  selenium-hub:
    image: selenium/hub:4.10.0-20230613
    ports:
      - "4444:4444"
  chrome:
    image: selenium/node-chrome:4.10.0-20230613
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
  tests:
    build: .
    depends_on:
      - selenium-hub
    environment:
      - BASE_URL=https://example.com
    command: pytest tests/
    
Test Reporting and CI/CD Integration

Use pytest plugins like pytest-html or pytest-junitxml to generate readable test reports inside the Docker container.

Reports are saved to mounted volumes so CI/CD pipelines can access them after test runs.

Integrate Docker test execution in CI/CD pipelines (GitHub Actions, Jenkins, GitLab CI) by running docker-compose up --abort-on-container-exit to execute tests in containers.

Fail the pipeline if tests fail by checking container exit codes.

Best Practices
  • Isolate Tests: Run tests in clean Docker containers to avoid environment conflicts.
  • Use Selenium Grid: Use Selenium Hub and browser nodes for scalable parallel test execution.
  • Keep Config Separate: Store environment and browser settings outside code in config files.
  • Mount Volumes: Share test reports and logs between containers and host for easy access.
  • Automate with CI/CD: Integrate Docker test runs into pipelines for consistent automated testing.
Self Check

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

Key Result
Use Docker containers to run Selenium Python tests in isolated, consistent environments with Selenium Grid for scalable browser management.