Skip to content

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

Done

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

  3. dgozman commented on Nov 25, 2026

    @dgozman
    Collaborator

    Agent, let's check whether #3373 fixed this one.

  4. added and removed
    agent-triage-done
    on Nov 25, 2026
  5. microsoft-playwright-automation commented on Nov 25, 2026

    @microsoft-playwright-automation
  6. microsoft-playwright-automation commented on Nov 25, 2026

    @microsoft-playwright-automation
    Contributor

    Hi, I'm the Playwright bot. I checked #3373 against the same regression before and after the roll.

    Yes, #3373 fixes this on main. On macOS, relative trace, HAR and screenshot paths all use the Python process's current directory in Chromium, Firefox and WebKit. Before the roll, only the screenshot does.

    The roll ports microsoft/playwright#44319: the client makes HAR paths absolute when recording starts and trace paths absolute before export. This is verified on main, not in a tagged release.

    Regression and results

    The existing tests/sync/python/test_tracing.py::test_should_collect_sources was temporarily extended with this case. The session-scoped browser starts before the directory change:

    monkeypatch.chdir(tmp_path)
    context = context_factory(record_har_path="relative-3361.har")
    context.tracing.start(sources=True)
    page = context.new_page()
    page.goto(server.EMPTY_PAGE)
    context.tracing.stop(path=Path("relative-3361-trace.zip"))
    page.screenshot(path="relative-3361-shot.png")
    context.close()
    assert (tmp_path / "relative-3361-trace.zip").is_file()
    assert (tmp_path / "relative-3361.har").is_file()
    assert (tmp_path / "relative-3361-shot.png").is_file()

    Runner: macOS 26.7.2 arm64, Python 3.14.10. Each checkout was built with its own pinned driver, both identified as 1.66.0-next.

    Checkout Mode Chromium Firefox WebKit
    Before #3373: 8b452510884008b70c099a32089d4501c90c721f Headless Fails Fails Fails
    After #3373: f31e7025d36bebc4b55c2e94f97f26ac92e13067 Headless Passes Passes Passes
    After #3373: f31e7025d36bebc4b55c2e94f97f26ac92e13067 Headed Passes Passes Passes

    Before: Python 1.65.1.dev26+g8b4525108.d20261125, driver 049226728d2da8054db18b04782a9848b755855e. After: Python 1.65.1.dev27+gf31e7025d, driver b0e0b7b555e637cc8c0ce94dd98c3be153861333.

    Before the roll, both the trace and HAR appear in the driver's startup directory, not tmp_path. After it, all three artifacts appear in tmp_path, and the existing trace-source viewer assertions also pass. No browser-specific difference.

    Ran python -m pytest tests/sync/python/test_tracing.py -k test_should_collect_sources --browser chromium --browser firefox --browser webkit, and repeated the current-main run with --headed: three expected failures before the roll and six passes after it. The reported published versions were not retested in this pass.

    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

    agent-triage-done

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions