Bird
Raised Fist0
Selenium Pythontesting~10 mins

Docker containers for test execution in Selenium Python - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a Selenium WebDriver inside a Docker container.

Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

service = Service('[1]')
driver = webdriver.Chrome(service=service)
driver.get('https://example.com')
driver.quit()
Drag options to blanks, or click blank then click option'
A/usr/bin/chromedriver
B/usr/local/bin/geckodriver
C/bin/selenium
D/usr/bin/firefoxdriver
Attempts:
3 left
💡 Hint
Common Mistakes
Using the path for GeckoDriver instead of ChromeDriver.
Using a non-existent driver path inside the container.
2fill in blank
medium

Complete the Docker command to run a Selenium test with Chrome in headless mode.

Selenium Python
docker run -d -p 4444:4444 --shm-size=2g selenium/standalone-chrome:latest [1]
Drag options to blanks, or click blank then click option'
A--headless
B--privileged
C--detach
D--rm
Attempts:
3 left
💡 Hint
Common Mistakes
Using --headless as a Docker option instead of a browser argument.
Using --privileged unnecessarily.
3fill in blank
hard

Fix the error in the Selenium test code to connect to the remote WebDriver in Docker.

Selenium Python
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

options = webdriver.ChromeOptions()
options.add_argument('--headless')
driver = webdriver.Remote(command_executor='http://[1]:4444/wd/hub', desired_capabilities=DesiredCapabilities.CHROME, options=options)
driver.get('https://example.com')
driver.quit()
Drag options to blanks, or click blank then click option'
A127.0.0.1
Blocalhost
Cdockerhost
D0.0.0.0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0.0.0.0 which is not a valid target address.
Using dockerhost which is not a standard hostname.
4fill in blank
hard

Fill both blanks to create a Docker Compose service for Selenium Chrome with proper port and shared memory size.

Selenium Python
""version: '3'
services:
  selenium-chrome:
    image: selenium/standalone-chrome:latest
    ports:
      - '[1]:4444'
    shm_size: '[2]'
"""
Drag options to blanks, or click blank then click option'
A4444
B5555
C2g
D512m
Attempts:
3 left
💡 Hint
Common Mistakes
Mapping the wrong port number.
Setting shared memory size too small causing browser crashes.
5fill in blank
hard

Fill all three blanks to write a Python dictionary comprehension that filters test results with status 'passed' and maps test names to durations.

Selenium Python
test_durations = {test[1] test_results if test_results[test]['status'] [2] 'passed'}
result = {k: v[3] for k, v in test_durations.items()}
Drag options to blanks, or click blank then click option'
A['name']
B==
C* 1000
D[test]
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect dictionary keys.
Using assignment '=' instead of comparison '=='.
Forgetting to multiply duration to convert units.

Practice

(1/5)
1. What is the main benefit of using Docker containers for Selenium test execution?
easy
A. They replace the need for writing test scripts
B. They provide a clean, consistent environment for tests to run
C. They automatically fix test failures
D. They make tests run faster on any machine

Solution

  1. Step 1: Understand Docker container purpose

    Docker containers isolate the environment so tests run the same everywhere.
  2. Step 2: Compare options

    Only They provide a clean, consistent environment for tests to run correctly describes this benefit; others are incorrect or unrelated.
  3. Final Answer:

    They provide a clean, consistent environment for tests to run -> Option B
  4. Quick Check:

    Docker containers = consistent test environment [OK]
Hint: Docker isolates environment to avoid setup issues [OK]
Common Mistakes:
  • Thinking Docker speeds up tests automatically
  • Believing Docker fixes test code errors
  • Confusing Docker with test script writing
2. Which Python Selenium WebDriver URL should you use to connect to a Selenium server running inside a Docker container?
easy
A. "http://localhost:4444/wd/hub"
B. "http://127.0.0.1:8080"
C. "http://docker:5555"
D. "http://0.0.0.0:80"

Solution

  1. Step 1: Identify default Selenium server URL in Docker

    The Selenium server in Docker usually listens on port 4444 at localhost.
  2. Step 2: Match URL options

    Only "http://localhost:4444/wd/hub" matches the standard Selenium Docker URL format.
  3. Final Answer:

    "http://localhost:4444/wd/hub" -> Option A
  4. Quick Check:

    Default Selenium Docker URL = localhost:4444/wd/hub [OK]
Hint: Selenium Docker server default port is 4444 [OK]
Common Mistakes:
  • Using wrong port numbers
  • Using IPs other than localhost
  • Missing the /wd/hub path
3. Given this Python code snippet connecting to a Selenium Docker container, what will be the output if the container is not running?
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")
medium
A. Connection failed
B. Connected
C. SyntaxError
D. TimeoutError

Solution

  1. Step 1: Understand connection attempt

    The code tries to connect to Selenium server at localhost:4444.
  2. Step 2: Consider container not running

    If the container is down, connection fails and WebDriverException is raised, triggering except block.
  3. Final Answer:

    Connection failed -> Option A
  4. Quick Check:

    Container down causes WebDriverException = "Connection failed" [OK]
Hint: If container off, connection throws WebDriverException [OK]
Common Mistakes:
  • Expecting 'Connected' even if container is off
  • Confusing exception types
  • Thinking code has syntax errors
4. You wrote this Docker command to run Selenium server:
docker run -d -p 4444:4444 selenium/standalone-chrome
But your Selenium Python tests fail to connect. What is the most likely issue?
medium
A. You used the wrong port mapping in Docker command
B. You forgot to install Selenium Python package
C. Docker container is not running or crashed
D. Your test code uses an incorrect WebDriver import

Solution

  1. Step 1: Check Docker container status

    If tests can't connect, first check if container is running or crashed.
  2. 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.
  3. Final Answer:

    Docker container is not running or crashed -> Option C
  4. Quick Check:

    Connection fails if container stopped = Docker container is not running or crashed [OK]
Hint: Check container running status first when connection fails [OK]
Common Mistakes:
  • Assuming code import errors cause connection failure
  • Ignoring container crash possibility
  • Confusing port mapping syntax
5. You want to run multiple Selenium Python tests in parallel using Docker containers. Which approach best ensures isolated, reliable test runs?
hard
A. Run tests locally without Docker to avoid container overhead
B. Run all tests in a single container sharing the same browser session
C. Use Docker but share the same Selenium server URL for all tests
D. Run one Docker container per test with unique ports and clean state

Solution

  1. Step 1: Understand parallel test isolation

    Running one container per test ensures each test has a clean browser and no interference.
  2. Step 2: Evaluate other options

    Sharing containers or sessions risks test interference and flaky results; running locally loses Docker benefits.
  3. Final Answer:

    Run one Docker container per test with unique ports and clean state -> Option D
  4. Quick Check:

    Parallel tests need isolated containers = Run one Docker container per test with unique ports and clean state [OK]
Hint: Use separate containers per test for isolation [OK]
Common Mistakes:
  • Sharing one container causing test conflicts
  • Ignoring container cleanup between tests
  • Running tests locally losing environment consistency
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard