I’m migrating my end-to-end testing setup to Python with Playwright and exploring the idea of making the entire test framework asynchronous, similar to how it’s used in Playwright for TypeScript.
However, I’m facing challenges when running async tests in Python.
The official Playwright documentation mentions using fixtures scoped to "module" for before/after hooks, but Python raises errors when I try to use async methods inside my tests.
I’m using Python 3.12 and would like to understand:
- Is it actually a good idea to use async methods in UI tests written in Python?
- How can I make Playwright tests run asynchronously?
- Are there any practical examples or working setups using Python + Playwright + async?
Any insights or simplified examples would be really helpful!
Yes, Playwright for Python fully supports async execution.
The key is that you can’t mix sync and async APIs in the same script.
Here’s a minimal working example:
import asyncio
from playwright.async_api import async_playwright
async def run():
async with async_playwright() as p:
browser = await p.chromium.launch(headless=False)
page = await browser.new_page()
await page.goto("https://example.com")
await page.screenshot(path="example.png")
await browser.close()
asyncio.run(run())
Use asyncio.run() to start the async event loop.
If you’re using pytest, install pytest-asyncio to handle async tests cleanly.
To get detail information on asyncio Python follow this tutorial : Python Asyncio Tutorial: A Complete Guide | LambdaTest
Hope this was helpful 
I agree with @sam.aarun , with pytest-asyncio, you can use async fixtures for setup/teardown:
import pytest
from playwright.async_api import async_playwright
@pytest.fixture(scope="session")
async def browser():
async with async_playwright() as p:
browser = await p.chromium.launch()
yield browser
await browser.close()
@pytest.mark.asyncio
async def test_example(browser):
page = await browser.new_page()
await page.goto("https://playwright.dev")
title = await page.title()
assert "Playwright" in title
This structure ensures async-safe initialization and cleanup, avoiding event-loop conflicts.
Just to add , using async is beneficial when you:
-
Run parallel tests or need non-blocking waits (e.g., multiple tabs/pages)
-
Integrate with async frameworks like FastAPI or aiohttp
However, for simple single-page UI tests, Playwright’s sync API (from playwright.sync_api import sync_playwright) is easier and more stable.
Async setups add complexity unless you specifically need concurrency.