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
Docker containers for test execution in Selenium Python - Framework Patterns
- Driver Layer:
utils/driver_factory.pycreates 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.yamlstores environment and browser settings;config/docker-compose.ymldefines container setup. - Docker Setup:
Dockerfilebuilds the test environment image with Python, Selenium, and dependencies.
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/
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.
- 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.
Where in this folder structure would you add a new page object for the "User Profile" page?
Practice
Solution
Step 1: Understand Docker container purpose
Docker containers isolate the environment so tests run the same everywhere.Step 2: Compare options
Only They provide a clean, consistent environment for tests to run correctly describes this benefit; others are incorrect or unrelated.Final Answer:
They provide a clean, consistent environment for tests to run -> Option BQuick Check:
Docker containers = consistent test environment [OK]
- Thinking Docker speeds up tests automatically
- Believing Docker fixes test code errors
- Confusing Docker with test script writing
Solution
Step 1: Identify default Selenium server URL in Docker
The Selenium server in Docker usually listens on port 4444 at localhost.Step 2: Match URL options
Only "http://localhost:4444/wd/hub" matches the standard Selenium Docker URL format.Final Answer:
"http://localhost:4444/wd/hub" -> Option AQuick Check:
Default Selenium Docker URL = localhost:4444/wd/hub [OK]
- Using wrong port numbers
- Using IPs other than localhost
- Missing the /wd/hub path
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
try:
driver = webdriver.Remote(
command_executor='http://localhost:4444/wd/hub',
options=webdriver.ChromeOptions()
)
print("Connected")
except WebDriverException:
print("Connection failed")Solution
Step 1: Understand connection attempt
The code tries to connect to Selenium server at localhost:4444.Step 2: Consider container not running
If the container is down, connection fails and WebDriverException is raised, triggering except block.Final Answer:
Connection failed -> Option AQuick Check:
Container down causes WebDriverException = "Connection failed" [OK]
- Expecting 'Connected' even if container is off
- Confusing exception types
- Thinking code has syntax errors
docker run -d -p 4444:4444 selenium/standalone-chromeBut your Selenium Python tests fail to connect. What is the most likely issue?
Solution
Step 1: Check Docker container status
If tests can't connect, first check if container is running or crashed.Step 2: Evaluate other options
The other options are less likely because the Docker command uses correct port mapping and Selenium package or imports cause different errors.Final Answer:
Docker container is not running or crashed -> Option CQuick Check:
Connection fails if container stopped = Docker container is not running or crashed [OK]
- Assuming code import errors cause connection failure
- Ignoring container crash possibility
- Confusing port mapping syntax
Solution
Step 1: Understand parallel test isolation
Running one container per test ensures each test has a clean browser and no interference.Step 2: Evaluate other options
Sharing containers or sessions risks test interference and flaky results; running locally loses Docker benefits.Final Answer:
Run one Docker container per test with unique ports and clean state -> Option DQuick Check:
Parallel tests need isolated containers = Run one Docker container per test with unique ports and clean state [OK]
- Sharing one container causing test conflicts
- Ignoring container cleanup between tests
- Running tests locally losing environment consistency
Ready to prove it?
Pass the test and this pattern moves unknown
