[Python] Playwright-Pytest Error: It looks like you are using Playwright Sync API inside the...

Discussão em 'Python' iniciado por Stack, Setembro 10, 2025.

  1. Stack

    Stack Membro Participativo

    I am able to run testcases individually using pytest and the class, function fixture: pytest test_fixtures_class.py.

    But when I try to run all classes testcases using the pytest command, I get the following error:

    test_fixtures_class.py::Test_titleclass::test_portfolio - playwright._impl._errors.Error: It looks like you are using Playwright Sync API inside the asyncio loop.

    [​IMG]
    pytest test_fixtures_class.py

    import pytest
    from playwright.sync_api import sync_playwright

    @pytest.fixture(scope='class')
    def browser(request):
    with sync_playwright() as p:
    browser=p.chromium.launch(headless=False)
    # Attach the browser instance to the test class
    request.cls.browser=browser
    yield browser
    browser.close()

    # Define a fixture with function-level scope
    @pytest.fixture(scope='function')
    def page(browser):
    page=browser.new_page()
    yield page
    page.close()


    # Use the 'browser' fixture for the following test class
    @pytest.mark.usefixtures('browser')
    class Test_titleclass:
    def test_google(self,page):
    page.goto('https://google.com')
    assert 'Google' == page.title()

    def test_portfolio(self,page):
    page.goto('https://github-portfolio-pst99.vercel.app/')
    assert 'My Portfolio' == page.title()




    Terminal Error

    PS C:\Users\Desktop\playwright-python\pytest> pytest
    ======================================================== test session starts ========================================================
    platform win32 -- Python 3.12.2, pytest-8.2.2, pluggy-1.5.0
    baseurl: https://www.saucedemo.com/
    rootdir: C:\Users\Desktop\playwright-python\pytest
    configfile: pytest.ini
    plugins: asyncio-0.23.7, base-url-2.1.0, html-4.1.1, metadata-3.1.1, playwright-0.5.0
    asyncio: mode=Mode.STRICT
    collected 9 items

    test_OrangeHRM.py . [ 11%]
    test_demo.py .. [ 33%]
    test_fixtures_class.py EE [ 55%]
    test_fixtures_module.py EE [ 77%]
    test_parameter.py EE [100%]

    ============================================================== ERRORS ===============================================================
    ___________________________________________ ERROR at setup of Test_titleclass.test_google ___________________________________________

    request = <SubRequest 'browser' for <Function test_google>>

    @pytest.fixture(scope='class')
    def browser(request):
    > with sync_playwright() as p:

    test_fixtures_class.py:6:
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    self = <playwright.sync_api._context_manager.PlaywrightContextManager object at 0x000001BF7E4FFB30>

    def __enter__(self) -> SyncPlaywright:
    try:
    self._loop = asyncio.get_running_loop()
    except RuntimeError:
    self._loop = asyncio.new_event_loop()
    self._own_loop = True
    if self._loop.is_running():
    > raise Error(
    """It looks like you are using Playwright Sync API inside the asyncio loop.
    Please use the Async API instead."""
    )
    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.

    ..\..\..\AppData\Local\Programs\Python\Python312\Lib\site-packages\playwright\sync_api\_context_manager.py:47: Error
    _________________________________________ ERROR at setup of Test_titleclass.test_portfolio __________________________________________

    request = <SubRequest 'browser' for <Function test_google>>

    @pytest.fixture(scope='class')
    def browser(request):
    > with sync_playwright() as p:

    test_fixtures_class.py:6:
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    self = <playwright.sync_api._context_manager.PlaywrightContextManager object at 0x000001BF7E4FFB30>

    def __enter__(self) -> SyncPlaywright:
    try:
    self._loop = asyncio.get_running_loop()
    except RuntimeError:
    self._loop = asyncio.new_event_loop()
    self._own_loop = True
    if self._loop.is_running():
    > raise Error(
    """It looks like you are using Playwright Sync API inside the asyncio loop.
    Please use the Async API instead."""
    )
    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.

    ..\..\..\AppData\Local\Programs\Python\Python312\Lib\site-packages\playwright\sync_api\_context_manager.py:47: Error
    ___________________________________________________ ERROR at setup of test_google ___________________________________________________

    @pytest.fixture(scope="module")
    def browser():
    > with sync_playwright() as p:

    test_fixtures_module.py:7:
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    self = <playwright.sync_api._context_manager.PlaywrightContextManager object at 0x000001BF7E599C70>

    def __enter__(self) -> SyncPlaywright:
    try:
    self._loop = asyncio.get_running_loop()
    except RuntimeError:
    self._loop = asyncio.new_event_loop()
    self._own_loop = True
    if self._loop.is_running():
    > raise Error(
    """It looks like you are using Playwright Sync API inside the asyncio loop.
    Please use the Async API instead."""
    )
    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.

    ..\..\..\AppData\Local\Programs\Python\Python312\Lib\site-packages\playwright\sync_api\_context_manager.py:47: Error
    _________________________________________________ ERROR at setup of test_portfolio __________________________________________________

    @pytest.fixture(scope="module")
    def browser():
    > with sync_playwright() as p:

    test_fixtures_module.py:7:
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    self = <playwright.sync_api._context_manager.PlaywrightContextManager object at 0x000001BF7E599C70>

    def __enter__(self) -> SyncPlaywright:
    try:
    self._loop = asyncio.get_running_loop()
    except RuntimeError:
    self._loop = asyncio.new_event_loop()
    self._own_loop = True
    if self._loop.is_running():
    > raise Error(
    """It looks like you are using Playwright Sync API inside the asyncio loop.
    Please use the Async API instead."""
    )
    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.

    ..\..\..\AppData\Local\Programs\Python\Python312\Lib\site-packages\playwright\sync_api\_context_manager.py:47: Error
    ___________________________________________ ERROR at setup of test_parameter[admin-admin] ___________________________________________

    @pytest.fixture(scope="module")
    def browser():
    > with sync_playwright() as p:

    test_parameter.py:6:
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    self = <playwright.sync_api._context_manager.PlaywrightContextManager object at 0x000001BF7E599520>

    def __enter__(self) -> SyncPlaywright:
    try:
    self._loop = asyncio.get_running_loop()
    except RuntimeError:
    self._loop = asyncio.new_event_loop()
    self._own_loop = True
    if self._loop.is_running():
    > raise Error(
    """It looks like you are using Playwright Sync API inside the asyncio loop.
    Please use the Async API instead."""
    )
    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.

    ..\..\..\AppData\Local\Programs\Python\Python312\Lib\site-packages\playwright\sync_api\_context_manager.py:47: Error
    ____________________________________________ ERROR at setup of test_parameter[test-test] ____________________________________________

    @pytest.fixture(scope="module")
    def browser():
    > with sync_playwright() as p:

    test_parameter.py:6:
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    self = <playwright.sync_api._context_manager.PlaywrightContextManager object at 0x000001BF7E599520>

    def __enter__(self) -> SyncPlaywright:
    try:
    self._loop = asyncio.get_running_loop()
    except RuntimeError:
    self._loop = asyncio.new_event_loop()
    self._own_loop = True
    if self._loop.is_running():
    > raise Error(
    """It looks like you are using Playwright Sync API inside the asyncio loop.
    Please use the Async API instead."""
    )
    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.

    ..\..\..\AppData\Local\Programs\Python\Python312\Lib\site-packages\playwright\sync_api\_context_manager.py:47: Error
    ====================================================== short test summary info ======================================================
    ERROR test_fixtures_class.py::Test_titleclass::test_google - playwright._impl._errors.Error: It looks like you are using Playwright Sync API inside the asyncio loop.
    ERROR test_fixtures_class.py::Test_titleclass::test_portfolio - playwright._impl._errors.Error: It looks like you are using Playwright Sync API inside the asyncio loop.
    ERROR test_fixtures_module.py::test_google - playwright._impl._errors.Error: It looks like you are using Playwright Sync API inside the asyncio loop.
    ERROR test_fixtures_module.py::test_portfolio - playwright._impl._errors.Error: It looks like you are using Playwright Sync API inside the asyncio loop.
    ERROR test_parameter.py::test_parameter[admin-admin] - playwright._impl._errors.Error: It looks like you are using Playwright Sync API inside the asyncio loop.
    ERROR test_parameter.py::test_parameter[test-test] - playwright._impl._errors.Error: It looks like you are using Playwright Sync API inside the asyncio loop.
    =================================================== 3 passed, 6 errors in 11.83s ==================================================== ```

    Continue reading...

Compartilhe esta Página