From 456f8286f09f132d2e21f6bf71f27465e71ba17a Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Wed, 16 Mar 2022 22:14:16 +0100 Subject: [PATCH] chore: launch the browser headed by default if debugger is attached (#107) --- pytest_playwright/pytest_playwright.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pytest_playwright/pytest_playwright.py b/pytest_playwright/pytest_playwright.py index 6c9c834..b6c661a 100644 --- a/pytest_playwright/pytest_playwright.py +++ b/pytest_playwright/pytest_playwright.py @@ -14,6 +14,7 @@ import shutil import os +import sys import warnings from typing import Any, Callable, Dict, Generator, List, Optional @@ -105,12 +106,18 @@ def pytest_runtest_setup(item: Any) -> None: pytest.skip("skipped for this browser: {}".format(browser_name)) +VSCODE_PYTHON_EXTENSION_ID = "ms-python.python" + + @pytest.fixture(scope="session") def browser_type_launch_args(pytestconfig: Any) -> Dict: launch_options = {} headed_option = pytestconfig.getoption("--headed") if headed_option: launch_options["headless"] = False + elif VSCODE_PYTHON_EXTENSION_ID in sys.argv[0] and _is_debugger_attached(): + # When the VSCode debugger is attached, then launch the browser headed by default + launch_options["headless"] = False browser_channel_option = pytestconfig.getoption("--browser-channel") if browser_channel_option: launch_options["channel"] = browser_channel_option @@ -120,6 +127,16 @@ def browser_type_launch_args(pytestconfig: Any) -> Dict: return launch_options +def _is_debugger_attached() -> bool: + pydevd = sys.modules.get("pydevd") + if not pydevd or not hasattr(pydevd, "get_global_debugger"): + return False + debugger = pydevd.get_global_debugger() + if not debugger or not hasattr(debugger, "is_attached"): + return False + return debugger.is_attached() + + def _build_artifact_test_folder( pytestconfig: Any, request: pytest.FixtureRequest, folder_or_file_name: str ) -> str: