playwright-python icon indicating copy to clipboard operation
playwright-python copied to clipboard

[BUG] Debugger fails with pytest

Open ManiMozaffar opened this issue 2 years ago • 5 comments

System info

  • Playwright Version: [v1.35.0]
  • Operating System: [macOS]
  • Browser: [Firefox]
  • Other info:

Source code

Make a checkpoint in VSCode with pytest debugger, and when running the file, it runs file. but if you want to execute another function (like going to another url) inside the debugger terminal, it'll fail. the debugger will freeze.

import pytest
from playwright.sync_api import async_playwright, Page

@pytest.fixture(scope="function")
def browser():
    with async_playwright() as playwright:
        # Use Firefox as the browser
        browser = playwright.firefox.launch()
        yield browser
        browser.close()

@pytest.mark.asyncio
async def test_playwright_with_firefox(browser):
    page: Page = await browser.new_page()
    await page.goto("https://www.example.com")
    assert "Example Domain" in await page.title()
    await page.close()

Steps

  • Check point somewhere in test
  • Control the page to go to somewhere else manually from command line.

Expected

  • It gets to that URL

[Describe expected behavior]

Actual

[Describe actual behavior]

ManiMozaffar avatar Aug 02 '23 13:08 ManiMozaffar

Your attached code was not working, you can do this instead:

import pytest
from playwright.async_api import async_playwright, Browser
import pytest_asyncio


@pytest_asyncio.fixture(scope="function")
async def browser():
    async with async_playwright() as playwright:
        # Use Firefox as the browser
        browser = await playwright.firefox.launch(headless=False)
        yield browser
        await browser.close()

@pytest.mark.asyncio
async def test_playwright_with_firefox(browser: Browser):
    page = await browser.new_page()
    await page.goto("https://www.example.com")
    assert "Example Domain" in await page.title()
    await page.close()

We discourage doing so tho, since we have an official pytest-playwright plugin which launches the browsers and contexts for you automatically: https://playwright.dev/python/docs/intro

mxschmitt avatar Aug 03 '23 14:08 mxschmitt

Your attached code was not working, you can do this instead:

import pytest
from playwright.async_api import async_playwright, Browser
import pytest_asyncio


@pytest_asyncio.fixture(scope="function")
async def browser():
    async with async_playwright() as playwright:
        # Use Firefox as the browser
        browser = await playwright.firefox.launch(headless=False)
        yield browser
        await browser.close()

@pytest.mark.asyncio
async def test_playwright_with_firefox(browser: Browser):
    page = await browser.new_page()
    await page.goto("https://www.example.com")
    assert "Example Domain" in await page.title()
    await page.close()

We discourage doing so tho, since we have an official pytest-playwright plugin which launches the browsers and contexts for you automatically: https://playwright.dev/python/docs/intro

Yes sorry I tried to simpilify my code from my project, In my case, I cannot use pytest-playwright, because I'm using dev tools and events that is only supported by a specific browser, with a custom written adaptor. Furthermore, the problem I have is not running the code, as suggested in title and description, when I try to execute a code in debugger, it would only work if it's not an action to be done by browser. if it's an action that needs to be done by browser (like a click event), then my debug console will freeze and it'll never work. I'm using vscode pytest debugger.

but with PDB debugger it's working fine, however PDB debugger only supports sync, so async api cannot be used with any debugger.

ManiMozaffar avatar Aug 05 '23 12:08 ManiMozaffar

I was able to reproduce it, with the following repro steps:

  1. pip install playwright pytest pytest-asyncio
  2. Put https://github.com/microsoft/playwright-python/issues/2031#issuecomment-1664059467 into a test_foo.py
  3. Set a breakpoint at the await page.goto("https://www.example.com") line
  4. Debug the test from VSCode
  5. Evaluate await page.goto("https://example.com")

Expected: no deadlock Actual: deadlock

I recommend filing it against https://github.com/microsoft/vscode-python instead, since this seems most likely not caused by us and they can debug it quicker.

mxschmitt avatar Aug 09 '23 20:08 mxschmitt

consider using PDB for debugging synchronous parts of your code and manually inserting print statements or logging for async code until a resolution is found for the freezing issue with the VSCode pytest debugger. Maybe consider testing outside of vscode

HermanBide avatar Feb 09 '24 23:02 HermanBide

consider using PDB for debugging synchronous parts of your code and manually inserting print statements or logging for async code until a resolution is found for the freezing issue with the VSCode pytest debugger. Maybe consider testing outside of vscode

I don't understand your point, you can literally write print statement to debug your code in VSCODE given current status but that's not what actual debugging means. You'd want to execute code as you're changing your breakpoint to figure out the problem. What the actual bug is that you can't execute async code that involves playwright, however you can execute other async code. with PDB, you can't even debug async code which is worst.

ManiMozaffar avatar Feb 10 '24 09:02 ManiMozaffar