microsoft / playwright-pytest

Pytest plugin to write end-to-end browser tests with Playwright.
https://playwright.dev/python/docs/test-runners
Apache License 2.0
443 stars 69 forks source link

AttributeError: 'PlaywrightContextManager' object has no attribute '_playwright' #235

Closed prashant111188 closed 4 months ago

prashant111188 commented 4 months ago

Hi,

I am using playwright-pytest in a debian 11 (bullseye) arm64 container. Following are the versions for all the dependencies:

Python: 3.12.2 pytest = 8.2.1 playwright = 1.45.0 pytest-playwright = 0.5.1.

I'm running the container on macOS Sonoma 14.5 (M1)

My sample test script is as follows:

import pytest from playwright.sync_api import Page, expect

def test_login(page: Page):
    page.goto("https:www.google.com")

    # Perform login actions
    expect(page.get_by_text("Google offered in")).to_be_visible()

This gives me the following error:



    @pytest.fixture(scope="session")
    def playwright() -> Generator[Playwright, None, None]:
>       pw = sync_playwright().start()

myTestEnv/lib/python3.12/site-packages/pytest_playwright/pytest_playwright.py:241: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
myTestEnv/lib/python3.12/site-packages/playwright/sync_api/_context_manager.py:84: in start
    return self.__enter__()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

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

        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."""
                )

            # Create a new fiber for the protocol dispatcher. It will be pumping events
            # until the end of times. We will pass control to that fiber every time we
            # block while waiting for a response.
            def greenlet_main() -> None:
                self._loop.run_until_complete(self._connection.run_as_sync())

            dispatcher_fiber = MainGreenlet(greenlet_main)

            self._connection = Connection(
                dispatcher_fiber,
                create_remote_object,
                PipeTransport(self._loop),
                self._loop,
            )

            g_self = greenlet.getcurrent()

            def callback_wrapper(channel_owner: ChannelOwner) -> None:
                playwright_impl = cast(Playwright, channel_owner)
                self._playwright = SyncPlaywright(playwright_impl)
                g_self.switch()

            # Switch control to the dispatcher, it'll fire an event and pass control to
            # the calling greenlet.
            self._connection.call_on_object_with_known_name("Playwright", callback_wrapper)
            dispatcher_fiber.switch()

>           playwright = self._playwright
E           AttributeError: 'PlaywrightContextManager' object has no attribute '_playwright'

myTestEnv/lib/python3.12/site-packages/playwright/sync_api/_context_manager.py:79: AttributeError
---------------------------------------------------------------------------------------------------------------------- Captured stderr setup -----------------------------------------------------------------------------------------------------------------------
qemu-x86_64-static: QEMU internal SIGSEGV {code=MAPERR, addr=0x20}
===================================================================================================================== short test summary info ======================================================================================================================
ERROR test_script.py::test_login[chromium] - AttributeError: 'PlaywrightContextManager' object has no attribute '_playwright'
========================================================================================================================= 1 error in 0.67s =========================================================================================================================
Task was destroyed but it is pending!
task: <Task pending name='Task-3' coro=<Connection.run.<locals>.init() running at /genai_platform_prompt_playground/playground/tests/myTestEnv/lib/python3.12/site-packages/playwright/_impl/_connection.py:270> wait_for=<Future finished result=None> cb=[ProtocolCallback.__init__.<locals>.cb() at /genai_platform_prompt_playground/playground/tests/myTestEnv/lib/python3.12/site-packages/playwright/_impl/_connection.py:191]>
Future exception was never retrieved
future: <Future finished exception=Exception('Connection closed while reading from the driver')>
Exception: Connection closed while reading from the driver```

I've seen a few threads on this but unable to resolve this issue. Is there something I'm missing here?
If I run this directly on my mac, works without issues.
mxschmitt commented 4 months ago

Did you install the dependencies via playwright install --with-deps?

qemu-x86_64-static: QEMU internal SIGSEGV {code=MAPERR, addr=0x20}

This looks like that still something gets emulated, could you execute arch inside your container to see which architecture it actually runs with?

prashant111188 commented 4 months ago

When I try to execute playwright install --with-deps, I still get the SIGSEGV error. The result of arch command is: x86_64

mxschmitt commented 4 months ago

You are emulating x86_64 via QEMU, it should be aarch64. Emulating a different architecture will prevent browsers from starting / is not supported. You can e.g. be explicit about it when running the container:

docker run -it --platform linux/arm64 debian:11 /bin/bash
prashant111188 commented 4 months ago

Oh ok. I'll try that out. Thanks very much for the response. .

prashant111188 commented 4 months ago

Well I was able to run this with an arm64 version of the image. But, interestingly, I could run on the amd64 version as well. What changed this time? I reduced the number of cores for podman desktop to 1 (and enabled rosetta in podman desktop 5.1.0). This fixed the issue and I can now run playwright on amd64 as well. Hopefully people facing a similar issue will find this helpful.