Skip to content

Commit 456f828

Browse files
authored
chore: launch the browser headed by default if debugger is attached (#107)
1 parent 676a301 commit 456f828

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

pytest_playwright/pytest_playwright.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import shutil
1616
import os
17+
import sys
1718
import warnings
1819
from typing import Any, Callable, Dict, Generator, List, Optional
1920

@@ -105,12 +106,18 @@ def pytest_runtest_setup(item: Any) -> None:
105106
pytest.skip("skipped for this browser: {}".format(browser_name))
106107

107108

109+
VSCODE_PYTHON_EXTENSION_ID = "ms-python.python"
110+
111+
108112
@pytest.fixture(scope="session")
109113
def browser_type_launch_args(pytestconfig: Any) -> Dict:
110114
launch_options = {}
111115
headed_option = pytestconfig.getoption("--headed")
112116
if headed_option:
113117
launch_options["headless"] = False
118+
elif VSCODE_PYTHON_EXTENSION_ID in sys.argv[0] and _is_debugger_attached():
119+
# When the VSCode debugger is attached, then launch the browser headed by default
120+
launch_options["headless"] = False
114121
browser_channel_option = pytestconfig.getoption("--browser-channel")
115122
if browser_channel_option:
116123
launch_options["channel"] = browser_channel_option
@@ -120,6 +127,16 @@ def browser_type_launch_args(pytestconfig: Any) -> Dict:
120127
return launch_options
121128

122129

130+
def _is_debugger_attached() -> bool:
131+
pydevd = sys.modules.get("pydevd")
132+
if not pydevd or not hasattr(pydevd, "get_global_debugger"):
133+
return False
134+
debugger = pydevd.get_global_debugger()
135+
if not debugger or not hasattr(debugger, "is_attached"):
136+
return False
137+
return debugger.is_attached()
138+
139+
123140
def _build_artifact_test_folder(
124141
pytestconfig: Any, request: pytest.FixtureRequest, folder_or_file_name: str
125142
) -> str:

0 commit comments

Comments
 (0)