Bird
Raised Fist0
Selenium Pythontesting~10 mins

Docker-based Grid in Selenium Python - Test Execution Trace

Choose your learning style10 modes available
Test Overview

This test uses Selenium with a Docker-based Selenium Grid to open a browser, navigate to a website, find a button by its ID, click it, and verify the page title changes as expected.

Test Code - Selenium
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Connect to Selenium Grid running in Docker
options = webdriver.ChromeOptions()
driver = webdriver.Remote(
    command_executor='http://localhost:4444/wd/hub',
    options=options
)

try:
    driver.get('https://example.com')
    
    # Wait until button with id 'start-btn' is present
    wait = WebDriverWait(driver, 10)
    button = wait.until(EC.presence_of_element_located((By.ID, 'start-btn')))
    
    button.click()
    
    # Wait until title changes to expected value
    wait.until(EC.title_is('Started Page'))
    
    assert driver.title == 'Started Page', f"Expected title 'Started Page' but got '{driver.title}'"
finally:
    driver.quit()
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and Selenium Remote WebDriver connects to Docker Selenium Grid at http://localhost:4444/wd/hubSelenium Grid is running in Docker, ready to provide browser session—PASS
2Browser session opens and navigates to 'https://example.com'Browser window shows the example.com homepage—PASS
3Waits up to 10 seconds for button with id 'start-btn' to appearButton with id 'start-btn' is present on the pageElement with id 'start-btn' is foundPASS
4Clicks the button with id 'start-btn'Page reacts to button click, navigation or content changes—PASS
5Waits until page title changes to 'Started Page'Page title is now 'Started Page'Page title equals 'Started Page'PASS
6Asserts that the page title is exactly 'Started Page'Page title matches expected valueassert driver.title == 'Started Page'PASS
7Test ends and browser session quitsBrowser closes, Selenium Grid session ends—PASS
Failure Scenario
Failing Condition: Button with id 'start-btn' is not found within 10 seconds
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test do first after connecting to the Selenium Grid?
AAsserts the page title is 'Started Page'
BNavigates the browser to 'https://example.com'
CClicks the button with id 'start-btn'
DQuits the browser session
Key Result
Always use explicit waits like WebDriverWait with expected conditions to handle dynamic page elements when testing with Selenium Grid in Docker.

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