Introduction
Playwright is an open-source automation library developed by Microsoft for testing web applications. It enables reliable end-to-end testing across modern browsers with a single API. The Python version supports Chromium, Firefox, and WebKit, making it a robust alternative to Selenium or other legacy tools.
Key Features
- Cross-Browser Support: Chromium, Firefox, and WebKit.
- Headless & Headed Modes: Run tests in both modes.
- Auto-Waiting: Built-in waiting mechanisms before element actions.
- Network Interception: Mock or modify network traffic.
- Async and Sync APIs: Choose between synchronous or asynchronous coding styles.
Installation
pip install playwright
playwright install
Basic Test Example
from playwright.sync_api import sync_playwright
def test_homepage():
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.goto("https://playwright.dev/")
assert "Playwright" in page.title()
page.click("text=Get started")
assert "intro" in page.url
browser.close()
test_homepage()
Pytest
For pytest integration:
pip install pytest-playwright
pytest
Debugging
- Use
page.pause()
to launch the inspector:
page.pause()
- Run tests with debugging enabled:
PWDEBUG=1 pytest
CI Integration
Playwright works well in CI environments. Example GitHub Actions snippet:
- name: Install dependencies
run: |
pip install playwright
playwright install
- name: Run Playwright tests
run: pytest
Conclusion
Playwright for Python brings the power and reliability of modern browser automation to Python developers. Its flexibility, ease of use, and cross-browser support make it ideal for E2E testing.