Bird
Raised Fist0
Selenium Pythontesting~8 mins

Docker-based Grid in Selenium Python - Framework Patterns

Choose your learning style10 modes available
Framework Mode - Docker-based Grid
Folder Structure
selenium-python-docker-grid/
├── tests/
│   ├── test_login.py
│   ├── test_search.py
│   └── __init__.py
├── pages/
│   ├── login_page.py
│   ├── search_page.py
│   └── __init__.py
├── utils/
│   ├── selenium_helpers.py
│   ├── wait_utils.py
│   └── __init__.py
├── config/
│   ├── config.yaml
│   └── __init__.py
├── docker/
│   ├── docker-compose.yml
│   └── selenium-hub/
│       ├── hub.json
│       └── node-chrome.json
├── conftest.py
├── requirements.txt
└── README.md
Test Framework Layers
  • Docker Grid Layer: Runs Selenium Hub and browser nodes inside Docker containers for parallel and scalable test execution.
  • Driver Layer: Uses Selenium WebDriver configured to connect remotely to the Docker Selenium Hub.
  • Page Object Layer: Contains page classes representing UI pages with methods to interact with elements.
  • Test Layer: Contains test scripts using pytest that call page objects and assert expected behaviors.
  • Utility Layer: Helper functions for waits, logging, and driver setup to keep tests clean and reusable.
  • Configuration Layer: YAML files and fixtures to manage environment variables, browser options, and credentials.
Configuration Patterns
  • Environment Config: Use config/config.yaml to define URLs, browser types, and grid hub address.
  • Browser Setup: In conftest.py, read config and create remote WebDriver sessions pointing to Docker Selenium Hub.
  • Credentials: Store sensitive data in environment variables or secure vaults, accessed via Python os.environ.
  • Docker Compose: docker/docker-compose.yml defines services for selenium-hub and browser nodes (chrome, firefox).
  • Fixture Usage: Pytest fixtures manage driver lifecycle and configuration injection into tests.
Test Reporting and CI/CD Integration
  • Use pytest built-in reporting with options for verbose and junit XML output for CI systems.
  • Integrate with CI tools like GitHub Actions, Jenkins, or GitLab CI to run tests on code push.
  • CI pipeline starts Docker Selenium Grid using docker-compose up -d before tests.
  • After tests, collect reports and stop Docker containers to clean up.
  • Optional: Use Allure or HTML reports for better visualization of test results.
Best Practices
  • Use Page Object Model: Keep UI interactions in page classes to separate test logic from UI details.
  • Explicit Waits: Use Selenium explicit waits to handle dynamic page elements reliably.
  • Isolate Tests: Each test should create and quit its own WebDriver session to avoid state leaks.
  • Parameterize Tests: Use pytest parametrization to run tests across multiple browsers and environments.
  • Keep Docker Images Updated: Regularly update Selenium Docker images to get latest browser and driver fixes.
Self Check

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

Key Result
A Docker-based Selenium Grid framework uses containerized Selenium Hub and nodes with Python pytest and Page Object Model for scalable, maintainable UI testing.

Practice

(1/5)
1. What is the main purpose of using a Docker-based Selenium Grid?
easy
A. To run tests only on the local machine without network
B. To write test scripts without any browser
C. To replace Selenium WebDriver with a new tool
D. To run browser tests in isolated containers for better scalability

Solution

  1. Step 1: Understand Docker-based Grid concept

    Docker-based Grid runs browsers inside containers, isolating tests and allowing many tests to run in parallel.
  2. Step 2: Compare options with this concept

    Only To run browser tests in isolated containers for better scalability correctly describes running tests in isolated containers for scalability.
  3. Final Answer:

    To run browser tests in isolated containers for better scalability -> Option D
  4. Quick Check:

    Docker Grid = isolated, scalable browser tests [OK]
Hint: Docker Grid isolates browsers to run many tests fast [OK]
Common Mistakes:
  • Thinking Docker replaces Selenium WebDriver
  • Believing tests run only locally without network
  • Confusing Docker Grid with test script writing
2. Which Python code snippet correctly creates a Remote WebDriver session to connect to a Docker-based Selenium Grid at URL http://localhost:4444/wd/hub for Chrome browser?
easy
A. driver = webdriver.Remote('http://localhost:4444/wd/hub', browser='chrome')
B. driver = webdriver.Chrome('http://localhost:4444/wd/hub')
C. driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub', options=webdriver.ChromeOptions())
D. driver = webdriver.ChromeOptions().Remote('http://localhost:4444/wd/hub')

Solution

  1. Step 1: Recall correct Remote WebDriver syntax

    Remote WebDriver requires command_executor URL and browser options object, e.g., ChromeOptions.
  2. Step 2: Match options with correct syntax

    driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub', options=webdriver.ChromeOptions()) uses webdriver.Remote with command_executor and ChromeOptions correctly; others misuse classes or parameters.
  3. Final Answer:

    driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub', options=webdriver.ChromeOptions()) -> Option C
  4. Quick Check:

    Remote WebDriver needs command_executor and options [OK]
Hint: Use webdriver.Remote with command_executor and options [OK]
Common Mistakes:
  • Using webdriver.Chrome instead of webdriver.Remote for grid
  • Passing URL directly to webdriver.Chrome constructor
  • Incorrect chaining of ChromeOptions and Remote
3. Given this Python code connecting to a Docker Selenium Grid, what will print(driver.title) output if the page at https://example.com has title "Example Domain"?
from selenium import webdriver

driver = webdriver.Remote(
    command_executor='http://localhost:4444/wd/hub',
    options=webdriver.FirefoxOptions()
)
driver.get('https://example.com')
print(driver.title)
driver.quit()
medium
A. Example Domain
B. https://example.com
C. None
D. Error: NoSuchWindowException

Solution

  1. Step 1: Understand code flow with Remote WebDriver

    The code opens Firefox browser via Docker Grid, navigates to example.com, then prints the page title.
  2. Step 2: Recall page title for https://example.com

    The official example.com page title is "Example Domain", so print(driver.title) outputs this string.
  3. Final Answer:

    Example Domain -> Option A
  4. Quick Check:

    driver.title = page title = "Example Domain" [OK]
Hint: Page title prints exactly what the website sets [OK]
Common Mistakes:
  • Confusing URL with page title
  • Expecting None if using Remote WebDriver
  • Assuming an error without browser session
4. You run this Python test script with Docker Selenium Grid but get a connection error. What is the likely cause?
from selenium import webdriver

driver = webdriver.Remote(
    command_executor='http://localhost:5555/wd/hub',
    options=webdriver.ChromeOptions()
)
driver.get('https://example.com')
medium
A. Docker Selenium Grid is not running on port 5555
B. ChromeOptions cannot be used with Remote WebDriver
C. The URL 'https://example.com' is invalid
D. webdriver.Remote requires a local Chrome browser

Solution

  1. Step 1: Analyze connection error cause

    Connection errors usually mean the Selenium Grid server is unreachable at the given URL and port.
  2. Step 2: Check port and server status

    Port 5555 is uncommon; default Docker Grid uses 4444. If Grid is not running on 5555, connection fails.
  3. Final Answer:

    Docker Selenium Grid is not running on port 5555 -> Option A
  4. Quick Check:

    Connection error = wrong port or server down [OK]
Hint: Check Docker Grid port matches command_executor URL [OK]
Common Mistakes:
  • Thinking ChromeOptions can't be used remotely
  • Blaming website URL for connection errors
  • Assuming Remote needs local browser installed
5. You want to run tests on Chrome and Firefox browsers simultaneously using Docker Selenium Grid. Which Docker Compose service setup is correct to start both browser nodes connected to the hub?
hard
A. version: '3' services: selenium-hub: image: selenium/hub:4.10.0-20230628 ports: - '4444:4444' chrome: image: selenium/node-chrome:4.10.0-20230628 environment: - SE_EVENT_BUS_HOST=selenium-hub firefox: image: selenium/node-firefox:4.10.0-20230628 environment: - SE_EVENT_BUS_HOST=selenium-hub
B. version: '3' services: selenium-hub: image: selenium/hub:4.10.0-20230628 ports: - '4444:4444' chrome: image: selenium/node-chrome:4.10.0-20230628 depends_on: - selenium-hub environment: - SE_EVENT_BUS_HOST=selenium-hub - SE_EVENT_BUS_PUBLISH_PORT=4442 - SE_EVENT_BUS_SUBSCRIBE_PORT=4443 firefox: image: selenium/node-firefox:4.10.0-20230628 depends_on: - selenium-hub environment: - SE_EVENT_BUS_HOST=selenium-hub - SE_EVENT_BUS_PUBLISH_PORT=4442 - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
C. version: '3' services: selenium-hub: image: selenium/hub:4.10.0-20230628 ports: - '4444:4444' chrome: image: selenium/node-chrome:4.10.0-20230628 depends_on: - selenium-hub firefox: image: selenium/node-firefox:4.10.0-20230628 depends_on: - selenium-hub
D. version: '3' services: selenium-hub: image: selenium/hub:4.10.0-20230628 ports: - '4444:4444' chrome: image: selenium/node-chrome:4.10.0-20230628 depends_on: - selenium-hub environment: - SE_EVENT_BUS_HOST=selenium-hub - SE_EVENT_BUS_PUBLISH_PORT=4443 - SE_EVENT_BUS_SUBSCRIBE_PORT=4442 firefox: image: selenium/node-firefox:4.10.0-20230628 depends_on: - selenium-hub environment: - SE_EVENT_BUS_HOST=selenium-hub - SE_EVENT_BUS_PUBLISH_PORT=4442 - SE_EVENT_BUS_SUBSCRIBE_PORT=4443

Solution

  1. Step 1: Understand Docker Compose setup for Selenium Grid

    The hub service exposes port 4444. Each node (chrome, firefox) must depend on hub and set environment variables for event bus host and ports.
  2. Step 2: Check environment variables and dependencies

    version: '3' services: selenium-hub: image: selenium/hub:4.10.0-20230628 ports: - '4444:4444' chrome: image: selenium/node-chrome:4.10.0-20230628 depends_on: - selenium-hub environment: - SE_EVENT_BUS_HOST=selenium-hub - SE_EVENT_BUS_PUBLISH_PORT=4442 - SE_EVENT_BUS_SUBSCRIBE_PORT=4443 firefox: image: selenium/node-firefox:4.10.0-20230628 depends_on: - selenium-hub environment: - SE_EVENT_BUS_HOST=selenium-hub - SE_EVENT_BUS_PUBLISH_PORT=4442 - SE_EVENT_BUS_SUBSCRIBE_PORT=4443 correctly sets SE_EVENT_BUS_HOST, SE_EVENT_BUS_PUBLISH_PORT=4442, and SE_EVENT_BUS_SUBSCRIBE_PORT=4443 for both nodes, matching Selenium Grid docs.
  3. Final Answer:

    version: '3' services: selenium-hub: image: selenium/hub:4.10.0-20230628 ports: - '4444:4444' chrome: image: selenium/node-chrome:4.10.0-20230628 depends_on: - selenium-hub environment: - SE_EVENT_BUS_HOST=selenium-hub - SE_EVENT_BUS_PUBLISH_PORT=4442 - SE_EVENT_BUS_SUBSCRIBE_PORT=4443 firefox: image: selenium/node-firefox:4.10.0-20230628 depends_on: - selenium-hub environment: - SE_EVENT_BUS_HOST=selenium-hub - SE_EVENT_BUS_PUBLISH_PORT=4442 - SE_EVENT_BUS_SUBSCRIBE_PORT=4443 -> Option B
  4. Quick Check:

    Correct event bus ports and depends_on = version: '3' services: selenium-hub: image: selenium/hub:4.10.0-20230628 ports: - '4444:4444' chrome: image: selenium/node-chrome:4.10.0-20230628 depends_on: - selenium-hub environment: - SE_EVENT_BUS_HOST=selenium-hub - SE_EVENT_BUS_PUBLISH_PORT=4442 - SE_EVENT_BUS_SUBSCRIBE_PORT=4443 firefox: image: selenium/node-firefox:4.10.0-20230628 depends_on: - selenium-hub environment: - SE_EVENT_BUS_HOST=selenium-hub - SE_EVENT_BUS_PUBLISH_PORT=4442 - SE_EVENT_BUS_SUBSCRIBE_PORT=4443 [OK]
Hint: Set SE_EVENT_BUS_HOST and ports exactly as docs show [OK]
Common Mistakes:
  • Omitting depends_on causing nodes to start before hub
  • Wrong or missing event bus port environment variables
  • Swapping publish and subscribe port numbers
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