Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 #155

Open
snackattas opened this issue Mar 13, 2023 · 5 comments

Comments

@snackattas
Copy link

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
@ltsuda
Copy link

ltsuda commented Mar 15, 2023

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

@mxschmitt
Copy link
Member

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

@snackattas
Copy link
Author

snackattas commented Mar 15, 2023

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?

@aspenboy
Copy link

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?

@mxschmitt
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants