0
0
Selenium Pythontesting~15 mins

Parallel execution in CI in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Run two simple Selenium tests in parallel on CI
Preconditions (4)
Step 1: Create two simple Selenium test functions that open https://example.com and check the page title
Step 2: Configure pytest to run tests in parallel using pytest-xdist with 2 workers
Step 3: Push the test code and pytest configuration to the CI repository
Step 4: Trigger the CI pipeline to run the tests
Step 5: Observe that both tests run at the same time (in parallel) in the CI logs
Step 6: Verify that both tests pass successfully
✅ Expected Result: Both Selenium tests run simultaneously in the CI environment and pass, confirming parallel execution works.
Automation Requirements - pytest with Selenium WebDriver and pytest-xdist
Assertions Needed:
Verify page title is exactly 'Example Domain' in both tests
Verify both tests complete successfully in parallel
Best Practices:
Use explicit waits if needed (though example.com loads fast)
Use pytest fixtures for WebDriver setup and teardown
Avoid sharing WebDriver instances between tests
Use pytest-xdist -n 2 to run tests in parallel
Keep tests independent and stateless
Automated Solution
Selenium Python
import pytest
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

@pytest.fixture
 def driver():
    options = Options()
    options.add_argument('--headless')
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    service = Service()
    driver = webdriver.Chrome(service=service, options=options)
    yield driver
    driver.quit()


def test_example_domain_title_1(driver):
    driver.get('https://example.com')
    assert driver.title == 'Example Domain'


def test_example_domain_title_2(driver):
    driver.get('https://example.com')
    assert driver.title == 'Example Domain'

# To run in parallel, use the command:
# pytest -n 2 test_parallel.py

The driver fixture creates a new Chrome WebDriver instance for each test, ensuring tests do not share browser sessions.

Both test_example_domain_title_1 and test_example_domain_title_2 open https://example.com and assert the page title is exactly 'Example Domain'.

Running pytest -n 2 test_parallel.py uses pytest-xdist to run the two tests in parallel with 2 workers.

This setup is ideal for CI environments where parallel execution speeds up test runs.

Headless Chrome options are used to run browsers without UI, suitable for CI servers.

Common Mistakes - 4 Pitfalls
Sharing a single WebDriver instance across multiple tests
Not using pytest-xdist or equivalent to enable parallelism
Hardcoding waits or using time.sleep instead of explicit waits
Running tests with visible browser windows in CI
Bonus Challenge

Now add data-driven testing with 3 different URLs and verify their titles in parallel

Show Hint