Playwright does not play right with asyncio
Summary
This issue has been previously reported in issues microsoft/playwright-pytest#167, microsoft/playwright-pytest#46, and microsoft/playwright-pytest#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!