0
0
Selenium Pythontesting~15 mins

Why Grid enables parallel execution in Selenium Python - Automation Benefits in Action

Choose your learning style9 modes available
Verify parallel execution of tests using Selenium Grid
Preconditions (3)
Step 1: Configure two test scripts to run on Selenium Grid with different browser capabilities
Step 2: Start both test scripts simultaneously targeting the Grid Hub URL
Step 3: Observe the execution of both tests
Step 4: Verify that both tests run at the same time on different nodes
✅ Expected Result: Both tests execute in parallel on different nodes connected to the Selenium Grid Hub, completing independently without waiting for each other
Automation Requirements - pytest with selenium
Assertions Needed:
Verify each test connects to Selenium Grid Hub URL
Verify each test runs on different browser capabilities
Verify tests complete successfully without errors
Best Practices:
Use pytest fixtures to manage WebDriver instances
Use explicit waits instead of sleep
Use capability dictionaries to specify browser and platform
Use parallel test execution with pytest-xdist plugin
Automated Solution
Selenium Python
import pytest
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

@pytest.fixture(params=[
    {"browserName": "chrome"},
    {"browserName": "firefox"}
])
def driver(request):
    capabilities = request.param
    grid_url = "http://localhost:4444/wd/hub"
    driver = webdriver.Remote(command_executor=grid_url, desired_capabilities=capabilities)
    yield driver
    driver.quit()

@pytest.mark.parametrize("url", ["https://example.com", "https://example.org"])
def test_parallel_execution(driver, url):
    driver.get(url)
    wait = WebDriverWait(driver, 10)
    heading = wait.until(EC.presence_of_element_located((By.TAG_NAME, "h1")))
    assert heading.is_displayed(), f"Heading not displayed on {url}"

This test script uses pytest with a fixture driver that runs tests on Selenium Grid. The fixture uses params to specify two browser capabilities: Chrome and Firefox.

The webdriver.Remote connects to the Selenium Grid Hub URL. Each test runs on a different browser node in parallel when executed with pytest-xdist.

The test navigates to a URL and waits explicitly for the main heading to appear, then asserts it is visible. This confirms the page loaded successfully.

Using pytest.mark.parametrize allows testing multiple URLs. Running with pytest -n 2 will run both browser tests in parallel, demonstrating Selenium Grid's parallel execution capability.

Common Mistakes - 4 Pitfalls
Using Thread.sleep instead of explicit waits
Hardcoding local WebDriver instead of using Remote WebDriver
Not parameterizing browser capabilities
Running tests sequentially without parallel test runner
Bonus Challenge

Now add data-driven testing with 3 different URLs and 3 different browsers to run tests in parallel on Selenium Grid.

Show Hint