Skip to content

Speed up tests by creating a dedicated PlaywrightFixture #261

Closed
@Archmonger

Description

@Archmonger

Current Situation

The following PR #260 refactored tests to be more readable/debuggable, however, they have become a bit slow due to needing to startup/shutdown Playwright in-between each PlaywrightTestCase class.

It would be better to re-use Playwright in between test modules.

Proposed Actions

I attempted to implement a PlaywrightFixture, however, I was getting a variety of errors during integration that I didn't have time to resolve. Here's a copy-paste of the important stuff.

###############################
# tests/test_app/tests/utils.py
###############################
@pytest.mark.usefixtures("playwright_fixture")
class PlaywrightTestCase(ChannelsLiveServerTestCase):
    ...

class PlaywrightFixture:
    playwright: Playwright
    browser: Browser
    page: Page

    def __init__(self) -> None:
        # Open a Playwright browser window
        if sys.platform == "win32":
            asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
        self.playwright = sync_playwright().start()
        headless = strtobool(os.environ.get("PLAYWRIGHT_HEADLESS", GITHUB_ACTIONS))
        self.browser = self.playwright.chromium.launch(headless=bool(headless))
        self.page = self.browser.new_page()
        self.page.set_default_timeout(5000)

    def __enter__(self):
        yield self

    def __exit__(self, exc_type, exc_value, traceback):
        # Close the Playwright browser
        self.playwright.stop()
##################################
# tests/test_app/tests/conftest.py
##################################
@pytest.fixture(autouse=True, scope="session")
def playwright_fixture():
    with PlaywrightFixture() as playwright:
        yield playwright

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions