pytestplaywright-python

playwright default timeout in pytest


In node.js we have https://playwright.dev/docs/test-timeouts, but page does not exist for python, I can use the timeout parameter, but then I have to set it all over:

page.goto(url, timeout=6000.0)
page.get_by_label(re.compile(r"(Language select|Språkvalg)")).click(timeout=6000.0)

How can I set this globally, in pytest.ini or a pytest.fixture?


Solution

  • To change navigational timeouts, one way you could accomplish this is to overwrite the default page fixture used in your Playwright tests. For example, you could add the following code to the top of your Playwright test file:

    @pytest.fixture
    def page(context: BrowserContext) -> Generator[Page, None, None]:
        page = context.new_page()
        page.set_default_navigation_timeout(6000) # the timeout is in milliseconds
        yield page
    

    For expect based timeouts, you can modify the default timeout in your conftest.py file (see documentation here). To set the timeout to 6 seconds, include the following code:

    from playwright.sync_api import expect
    
    expect.set_options(timeout=6000) # the timeout is in milliseconds