playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[BUG] run pytest + pytest-playwright (python)

Open Activisions opened this issue 1 year ago • 2 comments

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.

  1. It didn't solve the problem

  2. 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

  3. 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

Activisions avatar Nov 30 '24 18:11 Activisions

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!

Skn0tt avatar Dec 03 '24 22:12 Skn0tt

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.

mxschmitt avatar Dec 06 '24 17:12 mxschmitt

Closing per the response above, feel free to open a new issue if it doesn't work.

yury-s avatar Dec 09 '24 20:12 yury-s