Docker containers for test execution in Selenium Python - Build an Automation Script
import unittest from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC class TestGoogleSearch(unittest.TestCase): def setUp(self): chrome_options = Options() chrome_options.add_argument('--headless') chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-dev-shm-usage') self.driver = webdriver.Chrome(service=Service('/usr/bin/chromedriver'), options=chrome_options) self.wait = WebDriverWait(self.driver, 10) def test_google_title_and_search_box(self): self.driver.get('https://www.google.com') # Assert page title self.assertIn('Google', self.driver.title) # Wait for search box presence search_box = self.wait.until(EC.presence_of_element_located((By.NAME, 'q'))) self.assertTrue(search_box.is_displayed()) def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main() # Dockerfile content (to be created separately): # FROM python:3.12-slim # RUN apt-get update && apt-get install -y wget gnupg2 \ # && wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | apt-key add - \ # && echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list \ # && apt-get update && apt-get install -y google-chrome-stable \ # && pip install selenium # RUN wget https://chromedriver.storage.googleapis.com/114.0.5735.90/chromedriver_linux64.zip \ # && unzip chromedriver_linux64.zip \ # && mv chromedriver /usr/bin/chromedriver \ # && chmod +x /usr/bin/chromedriver # COPY test_script.py /tests/test_script.py # WORKDIR /tests # CMD ["python", "test_script.py"]
This test script uses Python's unittest framework with Selenium WebDriver.
In setUp, we configure Chrome to run in headless mode, which is important for running inside Docker containers without a display.
The test navigates to Google, checks the page title contains 'Google', and waits explicitly for the search box element to appear, then asserts it is visible.
The tearDown method closes the browser after the test.
The Dockerfile installs Chrome, ChromeDriver, and Selenium, then copies the test script inside the container and runs it.
This setup allows running the Selenium test fully inside a Docker container, ensuring consistent environment and easy test execution.
Now add data-driven testing with 3 different URLs to verify their page titles inside the Docker container.
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
