Parallel Execution in Playwright with Python
Automation test suites can become very large as the project grows. Running tests sequentially can take a lot of time. Playwright supports parallel execution which allows multiple tests to run simultaneously.
Parallel execution helps reduce total test execution time and provides faster feedback during CI/CD pipelines.
What is Parallel Execution?
Parallel execution means running multiple tests at the same time instead of executing them one by one.
- Test 1 running on Chrome
- Test 2 running on Firefox
- Test 3 running on WebKit
All tests execute together which significantly reduces overall test execution time.
Install Parallel Execution Plugin
Playwright Python projects commonly use Pytest. To enable parallel execution we install the pytest-xdist plugin.
pip install pytest-xdist
Run Tests in Parallel
You can run tests in parallel using the -n option.
pytest -n 4
This command will run tests using 4 parallel workers.
You can also automatically use all CPU cores.
pytest -n auto
Run Single Test in Multiple Browsers
Sometimes we need to verify a single test case across multiple browsers. Playwright allows this using pytest parameterization.
import pytest
@pytest.mark.parametrize("browser_name", ["chromium", "firefox", "webkit"])
def test_google(playwright, browser_name):
browser = getattr(playwright, browser_name).launch()
page = browser.new_page()
page.goto("https://google.com")
print(page.title())
browser.close()
This single test will run on:
- Chromium (Chrome)
- Firefox
- WebKit (Safari engine)
Run Multiple Tests in Multiple Browsers
In real automation projects we usually have multiple test cases. These tests can run across multiple browsers in parallel.
import pytest
@pytest.mark.parametrize("browser_name", ["chromium", "firefox"])
def test_google(playwright, browser_name):
browser = getattr(playwright, browser_name).launch()
page = browser.new_page()
page.goto("https://google.com")
assert "Google" in page.title()
browser.close()
@pytest.mark.parametrize("browser_name", ["chromium", "firefox"])
def test_bing(playwright, browser_name):
browser = getattr(playwright, browser_name).launch()
page = browser.new_page()
page.goto("https://bing.com")
assert "Bing" in page.title()
browser.close()
Run the tests using:
pytest -n 4
The framework will run multiple tests across multiple browsers in parallel.
Benefits of Parallel Execution
- Reduces test execution time
- Faster CI/CD pipeline execution
- Supports cross-browser testing
- Efficient use of CPU resources
- Faster feedback for developers
Parallel Execution Summary
| Feature | Command / Method | Description |
|---|---|---|
| Install Plugin | pip install pytest-xdist | Enables parallel execution |
| Run Parallel Tests | pytest -n 4 | Runs tests using 4 workers |
| Auto Workers | pytest -n auto | Uses all available CPU cores |
| Single Test Multiple Browser | pytest.mark.parametrize | Run one test across browsers |
| Multiple Tests Multiple Browser | pytest + parametrize | Run many tests across browsers |
Conclusion
Parallel execution is an important feature in modern automation frameworks. By combining Playwright, Pytest, and pytest-xdist, testers can run multiple tests across browsers simultaneously.
This approach significantly reduces execution time and improves CI/CD performance.
No comments:
Post a Comment