From a790c72ca8ea4a4f76ca444926bb6ff8ae9d0014 Mon Sep 17 00:00:00 2001 From: HoosseinRahimi Date: Sat, 21 Nov 2026 17:04:49 +0330 Subject: [PATCH] fix(tracing): resolve relative HAR and trace paths against current cwd The Playwright driver runs as a separate process, so relative paths passed to tracing.stop() / stop_chunk() and to record_har_path were resolved against the driver process's working directory instead of the Python client's current directory. If the cwd changes after the browser is launched (common in tests and job runners), trace and HAR files silently land in the wrong place. Resolve such paths to absolute paths on the Python side before they reach the driver, mirroring what Artifact.save_as() already does. Fixes #3361 --- playwright/_impl/tracing.py | 14 ++++++++++++-- tests/sync/library/test_tracing.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/playwright/_impl/tracing.py b/playwright/_impl/tracing.py index 8453bedf9..2b7d99a7b 100644 --- a/playwright/_impl/tracing.py +++ b/playwright/_impl/tracing.py @@ -22,6 +22,7 @@ """ import asyncio +import os import re from pathlib import Path from typing import ( @@ -64,6 +65,12 @@ class _HarRecorder(TypedDict): path: str +def _absolute_path(path: Union[str, Path]) -> str: + # Python: the driver is another process, so a relative path must be resolved + # here against the current directory, as Artifact.save_as() does. + return os.path.abspath(os.fspath(path)) + + class Tracing(ChannelOwner): _channel: "TracingChannel" @@ -220,8 +227,9 @@ async def _record_into_har( update_mode: Optional[Literal["minimal", "full"]], ) -> str: # Not ported: the resourcesDir option; it is JavaScript only. - # Python: the path can be a pathlib.Path. - har = str(har) + # Python: the path can be a pathlib.Path and is resolved against the + # current directory before it is sent to the driver. + har = _absolute_path(har) is_zip = bool(re.search(r"\.zip$", har, re.IGNORECASE)) options: Dict[str, Any] = { "content": update_content if update_content is not None else "attach", @@ -344,6 +352,8 @@ async def _save_chunk( if not local_utils: raise Error("Cannot save trace in thin clients") + # Python: resolve relative paths here; the driver is another process. + file_path = _absolute_path(file_path) is_local = not self._connection.is_remote() if is_local: diff --git a/tests/sync/library/test_tracing.py b/tests/sync/library/test_tracing.py index eee2d8346..e5a609fa3 100644 --- a/tests/sync/library/test_tracing.py +++ b/tests/sync/library/test_tracing.py @@ -1442,3 +1442,31 @@ def stop(*args: Any) -> None: web_socket_lines.append({"path": path, "lines": lines}) assert web_socket_lines[0]["path"] != web_socket_lines[1]["path"] assert web_socket_lines[1]["lines"] == web_socket_lines[1]["lines"] + + +def test_TR_043_should_resolve_relative_trace_and_har_paths_against_current_directory( + browser: Browser, server: Server, tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + # Regression test for python#3361: the driver process is started before a + # test changes the current directory, so relative artifact paths must be + # resolved by the Python client. + original_cwd = Path.cwd() + har_context = None + try: + monkeypatch.chdir(tmp_path) + har_context = browser.new_context(record_har_path="relative.har") + context = har_context + context.tracing.start() + page = context.new_page() + page.goto(server.EMPTY_PAGE) + context.tracing.stop(path="relative-trace.zip") + context.close() + har_context = None + + assert (tmp_path / "relative-trace.zip").is_file() + assert (tmp_path / "relative.har").is_file() + assert not (original_cwd / "relative-trace.zip").exists() + assert not (original_cwd / "relative.har").exists() + finally: + if har_context: + har_context.close()