[BUG] run pytest + pytest-playwright (python)
hello
when I try run the old pytest function in same running with pytest-playwright. I get error:
E playwright._impl._errors.Error: It looks like you are using Playwright Sync API inside the asyncio loop. E Please use the Async API instead.
try yourself run this 2 tests together
import pytest
from playwright.sync_api import sync_playwright
from playwright.sync_api import Page
class Test_example:
def test_api_request(self):
with sync_playwright() as playwright:
request_context = playwright.request.new_context()
request_context.get("https://www.microsoft.com/")
def test_pytest_playwright(self, page):
Test_example().test_api_request()
page.goto("https://google.com")
now. for check it I try use the Async API instead.
-
It didn't solve the problem
-
We have hundreds of tests written in the old pytest method, and we don't want to rewrite them to async just run them with new tests togheter
-
We want to continue working sync, because our tests are not written asyncio either
Hope for a solution to the problem, thank you very much for everything
Hi! Could you provide a self-contained reproduction, which we can run locally to reproduce your issue? That'd be great for us to understand what's happening. Thanks for your understanding!
When using pytest-playwright we already have an existing Playwright object as the playwright fixture you can use, see the following:
import pytest
from playwright.sync_api import sync_playwright
from playwright.sync_api import Page
class Test_example:
def my_helper(self, playwright):
request_context = playwright.request.new_context()
request_context.get("https://www.microsoft.com/")
def test_pytest_playwright(self, playwright, page):
self.my_helper(playwright)
page.goto("https://google.com")
Note: I renamed test_api_request to my_helper since otherwise Pytest would run two tests while you seem to want to call the my_helper from the test_pytest_playwright test.
Closing per the response above, feel free to open a new issue if it doesn't work.