[Python] Slow Python Async Playwright tests when running with with async_playwright() as p:

Discussão em 'Python' iniciado por Stack, Abril 15, 2025.

  1. Stack

    Stack Membro Participativo

    New to writing tests in playwright with Python, I am currently attempting to uplift our current TypeScript playwright tests into Python Playwright.

    To begin with I have been experimenting with the example tests from playwright.

    Running three very basic async tests that simply go to playwright and print the title of the webpage takes ~50 seconds. The same time is true for setting up my tests in a similar sync_playwright manner.

    Async Implementation:

    from playwright.async_api import async_playwright


    def test_has_title():
    with async_playwright() as p:
    browser = p.chromium.launch()
    context = browser.new_context()
    page = context.new_page()

    # Go to the Playwright documentation page
    page.goto("https://playwright.dev/")
    print(page.title())


    By using the Page object itself I am able to speed up the time to about 30 seconds for the 3 tests when running it in sync:

    from playwright.sync_api import Page

    async def test_has_title(page: Page):
    page.goto("https://playwright.dev/")
    print(page.title())


    From what I think I understand the time difference it mostly a result of creating new browsers and pages, however I'd like to do my tests as quickly as possible in async. Using a page object asynchronously in a similar way as the last example does not work.

    This is the code I was attempting to run:

    import pytest
    from playwright.async_api import Page

    @pytest.mark.asyncio
    async def test_has_title(page: Page):
    await page.goto("https://playwright.dev/")
    print(await page.title())


    Package versions:

    playwright 1.51.0
    pytest 8.3.5
    pytest-asyncio 0.26.0
    pytest-base-url 2.1.0
    pytest-dotenv 0.5.2
    pytest-playwright 0.7.0


    How can I share the page object among multiple asynchronous tests in order to speed up test execution.

    Thanks in advance

    Continue reading...

Compartilhe esta Página