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

Speed up tests by creating a dedicated PlaywrightFixture #261

Closed
Archmonger opened this issue Dec 1, 2024 · 0 comments · Fixed by #273
Closed

Speed up tests by creating a dedicated PlaywrightFixture #261

Archmonger opened this issue Dec 1, 2024 · 0 comments · Fixed by #273

Comments

@Archmonger
Copy link
Contributor

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant