Running tests at the same time saves time and finds problems faster.
Parallel execution in CI in 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.
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')
browsers = ['chrome', 'firefox', 'edge'] with ThreadPoolExecutor(max_workers=3) as executor: results = list(executor.map(run_test, browsers)) print(results)
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.
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)
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.
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.
Practice
Solution
Step 1: Understand parallel execution in CI
Parallel execution means running multiple tests at the same time instead of one after another.Step 2: Identify the benefit of parallel execution
Running tests simultaneously saves time by reducing total test duration.Final Answer:
It reduces the total test execution time by running tests simultaneously. -> Option AQuick Check:
Parallel execution = faster testing [OK]
- Thinking parallel slows tests down
- Believing tests run only on one browser
- Confusing parallel with disabling tests
Solution
Step 1: Identify Python modules for parallel execution
Python offers several modules, but ThreadPoolExecutor from concurrent.futures is popular for parallel threads.Step 2: Match module to Selenium parallel testing
ThreadPoolExecutor allows running multiple Selenium tests in parallel threads efficiently.Final Answer:
ThreadPoolExecutor -> Option AQuick Check:
ThreadPoolExecutor = parallel threads [OK]
- Choosing multiprocessing which uses processes, not threads
- Selecting unittest which is a test framework, not parallel executor
- Picking time which is for delays, not parallelism
from concurrent.futures import ThreadPoolExecutor
import time
def test_func(name):
time.sleep(1)
return f"Test {name} done"
with ThreadPoolExecutor(max_workers=3) as executor:
results = list(executor.map(test_func, ['A', 'B', 'C', 'D']))
print(results)Solution
Step 1: Understand ThreadPoolExecutor with max_workers=3
It runs up to 3 threads at once, but can handle more tasks by queuing them.Step 2: Analyze executor.map behavior
executor.map runs test_func on all 4 inputs and returns results in order after all finish.Final Answer:
['Test A done', 'Test B done', 'Test C done', 'Test D done'] -> Option CQuick Check:
All tasks run, results list includes all [OK]
- Assuming only 3 results because max_workers=3
- Expecting error due to more tasks than workers
- Thinking results order changes
RuntimeError: cannot schedule new futures after shutdown. What is the likely cause?
from concurrent.futures import ThreadPoolExecutor executor = ThreadPoolExecutor(max_workers=2) executor.shutdown() executor.submit(test_function)
Solution
Step 1: Understand executor.shutdown() effect
shutdown() stops the executor from accepting new tasks and cleans up threads.Step 2: Analyze submit call after shutdown
Calling submit after shutdown causes RuntimeError because executor is closed.Final Answer:
Calling submit after shutdown closes the executor, so no new tasks allowed. -> Option DQuick Check:
submit after shutdown = error [OK]
- Thinking max_workers causes this error
- Assuming test_function undefined causes this error
- Believing context manager is required
Solution
Step 1: Understand parallel testing with multiple browsers
Each test must create its own browser driver instance to avoid conflicts.Step 2: Use ThreadPoolExecutor to run tests in parallel
Running test functions in parallel, each initializing its own browser, allows simultaneous testing on Chrome and Firefox.Final Answer:
Use ThreadPoolExecutor to run a test function twice, each time initializing a different browser driver inside the function. -> Option BQuick Check:
Separate drivers per thread = correct parallel setup [OK]
- Sharing one WebDriver instance across threads
- Switching browsers sequentially defeats parallelism
- Initializing browsers outside test functions causes conflicts
Ready to prove it?
Pass the test and this pattern moves unknown
