0
0
Selenium Pythontesting~10 mins

Docker-based Grid in Selenium Python - Test Execution Trace

Choose your learning style9 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.