microsoft/playwright-pytest

If you use a fixture that runs for a while, and use the pytest-playwright fixture, the fixture opens an empty browser early, and just sits there

snackattas opened this issue · 5 comments

import pytest
import time
from playwright.sync_api import Page

@pytest.fixture
def i_am_a_fixture():
    time.sleep(60)

def test_page_has_extra_open_time(i_am_a_fixture, page: Page):
    page.goto("https://www.google.com")
    time.sleep(2)
    1 / 0

# run this test with this command
pytest <test file name> --tracing=retain-on-failure --video=retain-on-failure --output=./playwright-recordings

That's probably expected as the page is created by the function fixture context which is created by the session scoped fixture Browser. Because of that, the page (browser window) opens before your function scoped fixtures i_am_a_fixture.

@pytest.fixture(scope="session")

To add more information, even fixtures of the same scope, pytest probably wouldn't guarantee their order. But in this case, playwright fixture probably are instantiated at a higher priority than user defined fixtures.

https://docs.pytest.org/en/7.1.x/how-to/fixtures.html#yield-fixtures-recommended

Yup sounds expected, closing by that. Don't have long run fixtures, make them fast like Playwright is!

Yup sounds expected, closing by that. Don't have long run fixtures, make them fast like Playwright is!

Software is complicated, sometimes, I have to do A TON of API setup to drop the browser right in place. It is a reality that people are dealing with legacy software, and it pays to have a long running fixture vs refactoring TONS of code to make it more testable/easier to test...that may be the long term plan, but in the meantime, this is the world we live in, with long fixtures...

I thought page and context were session scoped fixtures...so, is there a way to make those fixtures run AFTER the i_am_a_fixture fixture?

I think it would be possible to do this if I defined a custom page fixture (maybe copying how the existing fixture works), and put the i_am_a_fixture as an arg to that custom page fixture, as a requirement...do you think that would work?

Yup sounds expected, closing by that. Don't have long run fixtures, make them fast like Playwright is!

Software is complicated, sometimes, I have to do A TON of API setup to drop the browser right in place. It is a reality that people are dealing with legacy software, and it pays to have a long running fixture vs refactoring TONS of code to make it more testable/easier to test...that may be the long term plan, but in the meantime, this is the world we live in, with long fixtures...

I feel you bro, I have similar setup - loads of test data goes through API calls before actual page navigation starts, and the browser still opens first - hanging around and waiting until all the setup is done. Maybe some of the pytest hooks can be used for that?

I will reopen for now, since the following prints page1 first and the page2.

import pytest


@pytest.fixture
def page1():
    print("page1")


@pytest.fixture
def page2():
    print("page2")


async def test_foo(page1, page2):
    pass