0
0
Selenium Pythontesting~15 mins

Docker containers for test execution in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Run Selenium test inside a Docker container
Preconditions (3)
Step 1: Create a Dockerfile that installs Python, Selenium, and a browser driver
Step 2: Build the Docker image from the Dockerfile
Step 3: Run the Docker container with the test script mounted inside
Step 4: Execute the Selenium test script inside the container
Step 5: Observe the test execution and results
✅ Expected Result: The Selenium test runs successfully inside the Docker container and outputs the test result (pass/fail) to the console
Automation Requirements - Selenium with Python
Assertions Needed:
Verify the page title is as expected
Verify a specific element is present on the page
Best Practices:
Use explicit waits instead of sleep
Use Page Object Model for maintainability
Use Docker volumes to mount test scripts
Use headless browser mode for container execution
Automated Solution
Selenium Python
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.

Common Mistakes - 4 Pitfalls
Using time.sleep() instead of explicit waits
{'mistake': 'Not running browser in headless mode inside Docker', 'why_bad': 'Docker containers usually have no display, so tests fail if browser tries to open GUI.', 'correct_approach': "Use headless browser options like '--headless' for Chrome."}
Hardcoding absolute paths for ChromeDriver
Not closing the browser after test
Bonus Challenge

Now add data-driven testing with 3 different URLs to verify their page titles inside the Docker container.

Show Hint