Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions playwright/_impl/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"""

import asyncio
import os
import re
from pathlib import Path
from typing import (
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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:
Expand Down
28 changes: 28 additions & 0 deletions tests/sync/library/test_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()