Skip to content

fix(tracing): resolve relative HAR and trace paths against current cwd - #3363

Open
Hossein Rahimi (HoosseinRahimi) wants to merge 1 commit into
microsoft:mainfrom
HoosseinRahimi:fix/resolve-relative-trace-har-paths
Open

Hossein Rahimi (HoosseinRahimi) wants to merge 1 commit into
microsoft:mainfrom
HoosseinRahimi:fix/resolve-relative-trace-har-paths

Conversation

@HoosseinRahimi

Copy link
Copy Markdown

Summary

Relative paths passed to tracing.stop(path=...), tracing.stop_chunk(path=...) and record_har_path=... were resolved by the driver process, not the Python client. Since the driver starts before the test/runner may change its working directory, the artifacts silently landed in the wrong place when the cwd changed after browser launch — the exact scenario in #3361.

This PR resolves such paths to absolute paths on the Python side before they reach the driver, mirroring what Artifact.save_as() already does for page screenshots/PDFs.

Changes

  • playwright/_impl/tracing.py
    • Add a shared _absolute_path() helper that normalizes a path with os.path.abspath(os.fspath(path)) (same approach as Artifact.save_as()).
    • _record_into_har(): normalize the HAR output path.
    • _save_chunk(): normalize the trace file path before it is sent to the driver or passed to local utils.

Test

Added test_TR_043_should_resolve_relative_trace_and_har_paths_against_current_directory, which:

  1. launches the browser from the original cwd,
  2. changes cwd (via monkeypatch.chdir) to a temp dir,
  3. records a HAR and stops tracing with relative paths,
  4. asserts both artifacts are created in the current (temp) directory and not in the original cwd.

Verified locally on Windows / Python 3.13:

  • Without the fix: the test fails — relative-trace.zip and relative.har are created in the driver's working directory (the repo root), not the temp dir.
  • With the fix: the test passes.
tests/sync/library/test_tracing.py::test_TR_043_should_resolve_relative_trace_and_har_paths_against_current_directory[chromium] PASSED

Fixes #3361

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

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 microsoft#3361
@HoosseinRahimi

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: tracing.stop(path=...) and record_har_path ignore os.chdir() for relative paths

1 participant