Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Debugger fails with pytest #2031

Open
ManiMozaffar opened this issue Aug 2, 2023 · 5 comments
Open

[BUG] Debugger fails with pytest #2031

ManiMozaffar opened this issue Aug 2, 2023 · 5 comments

Comments

@ManiMozaffar
Copy link

ManiMozaffar commented Aug 2, 2023

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 ManiMozaffar changed the title Debugger fails with pytest [BUG] Debugger fails with pytest Aug 2, 2023
@mxschmitt
Copy link
Member

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

@ManiMozaffar
Copy link
Author

ManiMozaffar commented Aug 5, 2023

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.

@mxschmitt
Copy link
Member

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

  1. pip install playwright pytest pytest-asyncio
  2. Put [BUG] Debugger fails with pytest #2031 (comment) 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.

@HermanBide
Copy link

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

@ManiMozaffar
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants