Running tests at the same time saves time and finds problems faster.
0
0
Parallel execution in CI in Selenium Python
Introduction
You want to check your website on many browsers quickly.
You have many tests and want results faster.
You want to use your computer or server better by running tests together.
You want to catch errors early by testing many parts at once.
You use a Continuous Integration (CI) system to test code automatically.
Syntax
Selenium Python
from selenium import webdriver from concurrent.futures import ThreadPoolExecutor def run_test(browser_name): if browser_name == 'chrome': driver = webdriver.Chrome() elif browser_name == 'firefox': driver = webdriver.Firefox() else: return 'Browser not supported' driver.get('https://example.com') title = driver.title driver.quit() return title browsers = ['chrome', 'firefox'] with ThreadPoolExecutor() as executor: results = list(executor.map(run_test, browsers)) print(results)
This example uses Python's ThreadPoolExecutor to run tests in parallel.
Each test opens a browser, runs, and closes it independently.
Examples
This runs two tests in parallel using submit method.
Selenium Python
from concurrent.futures import ThreadPoolExecutor # Run two tests at the same time with ThreadPoolExecutor(max_workers=2) as executor: executor.submit(run_test, 'chrome') executor.submit(run_test, 'firefox')
This runs tests on three browsers at once and collects results.
Selenium Python
browsers = ['chrome', 'firefox', 'edge'] with ThreadPoolExecutor(max_workers=3) as executor: results = list(executor.map(run_test, browsers)) print(results)
Sample Program
This script runs tests on Chrome and Firefox at the same time. Each test opens the browser, gets the page title, then closes the browser. The results print after both finish.
Selenium Python
from selenium import webdriver from concurrent.futures import ThreadPoolExecutor def run_test(browser_name): if browser_name == 'chrome': driver = webdriver.Chrome() elif browser_name == 'firefox': driver = webdriver.Firefox() else: return f'{browser_name} not supported' driver.get('https://example.com') title = driver.title driver.quit() return f'{browser_name} title: {title}' browsers = ['chrome', 'firefox'] with ThreadPoolExecutor(max_workers=2) as executor: results = list(executor.map(run_test, browsers)) for result in results: print(result)
OutputSuccess
Important Notes
Make sure your CI environment supports running browsers in parallel.
Use separate browser instances to avoid conflicts.
Limit the number of parallel tests to avoid overloading your system.
Summary
Parallel execution runs tests at the same time to save time.
Use ThreadPoolExecutor in Python to run Selenium tests in parallel.
Good for testing multiple browsers or many tests quickly in CI.