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

Playwright does not play right with asyncio #240

Open
Xowap opened this issue Aug 16, 2024 · 1 comment
Open

Playwright does not play right with asyncio #240

Xowap opened this issue Aug 16, 2024 · 1 comment

Comments

@Xowap
Copy link

Xowap commented Aug 16, 2024

Summary

This issue has been previously reported in issues #167, #46, and #29, but has not been fully resolved as the underlying root cause remains unaddressed.

Problem Description

I have observed that the playwright fixture installs a running event loop in the current thread. This behavior leads to subsequent calls to asyncio's run or similar functions becoming unsuccessful. Due to the abstract nature of the Playwright code, I have not yet pinpointed the exact source of the problem.

Additionally, I am unable to reproduce this issue by directly invoking the asyncio API within basic Python scripts not involving Playwright, so I don't really understand the mechanics at play here.

Reproduction

I have created a minimal reproduction repository to demonstrate the issue. Here is a sample test file that highlights the problem:

import asyncio
import pytest


def get_true():
    return True


async def a_get_true():
    return get_true()


@pytest.mark.playwright
def test_pw_true(playwright):
    pass


def test_true():
    assert get_true()


def test_a_true():
    assert asyncio.run(a_get_true())


@pytest.mark.asyncio
async def test_pa_true():
    assert await a_get_true()

In this script, any asynchronous test that follows a test using the playwright fixture results in an error. Changing the test order or skipping the test_pw_true test, which is currently empty, prevents the issue from occurring.

Suggested Solution

One potential solution could be to initialize Playwright in a separate thread or process, with the Pytest API offering a wrapper to handle inter-thread communication. However, this approach might be overly simplistic and may need further refinement.

Temporary Workaround

While awaiting a more permanent and clean solution, I will continue using the hacks mentioned in the above issues. Nonetheless, a robust and canonical fix would be greatly appreciated.

Thank you!

@mxschmitt mxschmitt transferred this issue from microsoft/playwright-pytest Aug 20, 2024
@mxschmitt mxschmitt transferred this issue from microsoft/playwright-python Aug 20, 2024
@Skn0tt Skn0tt self-assigned this Aug 20, 2024
@Skn0tt Skn0tt removed their assignment Oct 1, 2024
@bartfeenstra
Copy link

bartfeenstra commented Nov 2, 2024

I have run into this as well. Please add a warning to the documentation, as this means the package is outright incompatible with anything that requires an async test method, because the system under test is asynchronous. It appears that this is the sole reason I cannot use this package and will have to resort to using the JS version of Playwright instead (which comes with the additional overhead of not being able to reuse existing Python fixtures) (not a rant, just an explanation of why this has a bigger impact than it may seem at first).

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

No branches or pull requests

3 participants