Skip to content

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

Open

Description

Version

1.65.2 (same on 1.64.0)

Steps to reproduce

Relative paths for traces and HARs are resolved against the directory the driver was started in, not the current one. Screenshots use the current one, so in one script the files end up in two places:

import os, tempfile
from playwright.sync_api import sync_playwright

start = os.getcwd()
with sync_playwright() as p:
    target = tempfile.mkdtemp()
    os.chdir(target)
    ctx = p.chromium.launch().new_context(record_har_path="rel.har")
    ctx.tracing.start()
    page = ctx.new_page()
    page.goto("data:text/html,x")
    ctx.tracing.stop(path="trace.zip")
    page.screenshot(path="shot.png")
    ctx.close()
    for f in ("trace.zip", "rel.har", "shot.png"):
        print(f, os.path.exists(os.path.join(target, f)), os.path.exists(os.path.join(start, f)))

Expected behavior

All three files in target.

Actual behavior

trace.zip False True
rel.har False True
shot.png True False

Hits anyone using monkeypatch.chdir() with a session-scoped playwright. Artifact.save_as already makes the path absolute before sending it; tracing and HAR don't.

Additional context

No response

Environment

- Operating System: macOS 27.2
- CPU: arm64
- Browser: Chromium
- Python Version: 3.14
- Other info:

Activity

  1. microsoft-playwright-automation commented on Nov 23, 2026

    @microsoft-playwright-automation
  2. microsoft-playwright-automation commented on Nov 23, 2026

    @microsoft-playwright-automation
    Contributor

    Hi, I'm the Playwright bot. I reproduced this on macOS.

    Reproduced on current main in Chromium, Firefox and WebKit. The Python process changes directory, but the driver keeps its startup directory. Trace and HAR paths reach the driver without being made absolute. The proposed fix resolves them in Python, before tracing stops or HAR recording starts.

    The source change in #3363 also passes the independent regression. Its test fails the test-registry check because TR-043 already identifies another test. This proposal extends the existing Python tracing test instead.

    Condensed repro

    With the session-scoped browser fixture already started:

    monkeypatch.chdir(tmp_path)
    context = browser.new_context(record_har_path="log.har")
    context.tracing.start()
    page = context.new_page()
    page.goto(server.EMPTY_PAGE)
    context.tracing.stop(path=Path("trace.zip"))
    context.close()
    assert (tmp_path / "trace.zip").is_file()
    assert (tmp_path / "log.har").is_file()

    Without the fix, both files are written in the driver's startup directory. With the fix, both assertions pass.

    What I ran

    macOS 26.7.2, Python 3.14.10. Python checkout de03c865f5738c2291a6a58c8b5c20346d8e70f2 (1.65.1.dev24+gde03c865f), built with driver a018f24a4fc76e2120d3b35d2415f1e5bd4b073e (1.66.0-next). The reported published versions were not retested.

    Check Chromium Firefox WebKit
    Independent regression on main, headless Fails Fails Fails
    Independent regression on #3363, a790c72ca8ea4a4f76ca444926bb6ff8ae9d0014 Passes Passes Passes
    #3363 test alone on main Fails Fails Fails
    #3363 test on its head Passes Passes Passes
    Independent regression with the proposed fix Passes Passes Passes
    Upstream Node client: relative trace, HAR and screenshot paths after changing directory Passes Passes Passes

    The independent regression extends tests/sync/python/test_tracing.py::test_should_collect_sources, using a relative Path for the trace and a relative string for the HAR. Its failure without the source fix and success with it were checked again before preparing the proposal.

    Neighboring tracing, HAR, browser-context HAR and HAR WebSocket files passed in separate sync and async runs across all three browsers: 467 sync tests and 482 async tests passed, with 7 existing skips in each run. The Python-only tracing files and async HAR file were included.

    Full scripts/lint.sh and mypy playwright passed. Lint used Python 3.13 because the pinned Pyright version cannot parse Python 3.14's standard-library t-strings.

    Draft PR: #3367

    Triaged on macOS by the Playwright bot - agent run

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions