Repository navigation
feat: record traces of playwright.request API contexts - #333
Merged
Merged
Conversation
Pavel Feldman (pavelfeldman)
added this pull request to stack #335
October 7, 2026 23:05
Pavel Feldman (pavelfeldman)
force-pushed
the
feat-137-api-request-tracing
branch
from
October 8, 2026 01:32
56ce3c6 to
cf5d5ac
Compare
Pavel Feldman (pavelfeldman)
force-pushed
the
feat-121-report-artifacts
branch
from
October 8, 2026 01:32
46acebe to
3d413a2
Compare
Pavel Feldman (pavelfeldman)
force-pushed
the
feat-137-api-request-tracing
branch
4 times, most recently
from
October 8, 2026 02:10
13763a2 to
6ffab8c
Compare
Pavel Feldman (pavelfeldman)
force-pushed
the
feat-137-api-request-tracing
branch
from
October 8, 2026 02:12
6ffab8c to
1317e02
Compare
Pavel Feldman (pavelfeldman)
removed this pull request from stack #335
October 8, 2026 02:19
Copilot started reviewing on behalf of
Pavel Feldman (pavelfeldman)
October 8, 2026 17:47
View session
There was a problem hiding this comment.
🟡 Changes recommended
It breaks declared pytest 6 compatibility and can skip API-context disposal when trace finalization fails.
4 open findings
What changed in this PR
Adds per-test tracing for Playwright API request contexts in both plugin variants.
Changes:
- Instruments API context creation and disposal for trace collection.
- Integrates API traces with existing artifact retention.
- Adds synchronous and asynchronous integration coverage.
| File | Description |
|---|---|
tests/test_sync.py |
Tests synchronous API tracing scenarios. |
tests/test_asyncio.py |
Tests asynchronous API tracing scenarios. |
pytest-playwright/pytest_playwright/pytest_playwright.py |
Implements synchronous API trace recording. |
pytest-playwright-asyncio/pytest_playwright_asyncio/pytest_playwright.py |
Implements asynchronous API trace recording. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| # Set on a test item by _artifacts_recorder, picked up for the teardown report. | ||
| _ARTIFACTS_RECORDER_ATTR = "_playwright_artifacts_recorder" | ||
| # Node id of the test being set up, run or torn down, see _APIRequestContexts. | ||
| _CURRENT_TEST_NODEID_KEY = pytest.StashKey[str]() |
| # Set on a test item by _artifacts_recorder, picked up for the teardown report. | ||
| _ARTIFACTS_RECORDER_ATTR = "_playwright_artifacts_recorder" | ||
| # Node id of the test being set up, run or torn down, see _APIRequestContexts. | ||
| _CURRENT_TEST_NODEID_KEY = pytest.StashKey[str]() |
Comment on lines
+1013
to
+1019
| async def _dispose_wrapper(*args: Any, **kwargs: Any) -> None: | ||
| if context in self.live: | ||
| self.live.remove(context) | ||
| if self.recorder is not None: | ||
| await self.recorder.on_will_dispose_api_request_context(context) | ||
| self.tracing_chunk.discard(context) | ||
| await original_dispose(*args, **kwargs) |
Comment on lines
+1006
to
+1012
| def _dispose_wrapper(*args: Any, **kwargs: Any) -> None: | ||
| if context in self.live: | ||
| self.live.remove(context) | ||
| if self.recorder is not None: | ||
| self.recorder.on_will_dispose_api_request_context(context) | ||
| self.tracing_chunk.discard(context) | ||
| original_dispose(*args, **kwargs) |
--tracing only covered browser contexts, so API tests using playwright.request.new_context() produced no trace. With APIRequestContext.tracing public since playwright 1.60, contexts created through playwright.request.new_context() are now traced per test: - tracing starts when the context is created; - each test records its own chunk, so a session-scoped context shared by many tests yields one trace per test, and chunks without any action (a long-lived context the test did not use) are dropped; - the traces go through the same retain-on-failure handling, file naming and report attachments as browser context traces. Tests that use Playwright but no page or context now get the artifacts recorder through a small autouse fixture when tracing is enabled. Fixes #137
…traces of pre-existing contexts, let stop_chunk errors surface, stream the trace check; test mixed page+API traces and --tracing off
Pavel Feldman (pavelfeldman)
force-pushed
the
feat-137-api-request-tracing
branch
from
October 8, 2026 18:36
1317e02 to
1e0724e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


--tracingonly covered browser contexts, so API tests usingplaywright.request.new_context()produced no trace.APIRequestContext.tracingis public since playwright 1.60, so contexts created throughplaywright.request.new_context()are now traced per test:pytest_runtest_setupstores the current node id inconfig.stashfor that.stop_chunksurface like those from browser contexts instead of silently losing the trace; the context is still disposed (finally) when that happens.pytest-asyncio>=0.24already requires).pytest.Parser, used since before this PR, andpytest.StashKeyboth need pytest 7; the plugin did not import on pytest 6 before either.trace-N.zipnaming and report attachments as browser traces.Tests that use Playwright but no page or context now get the artifacts recorder through a small autouse fixture. It only does so when tracing is enabled and the test transitively uses the
playwrightfixture. Reviewers should know that this also sets the recorder up for browser tests before their other function-scoped fixtures, rather than as a dependency ofnew_context; no behavior change was found. On playwright < 1.60 nothing changes.Tests: session- and function-scoped API contexts with
--tracing on(each trace contains only its test's requests and is titled after it; an unused self-created context keeps its trace; a test with a page and an API context getstrace-1.zipandtrace-2.zip),--tracing retain-on-failure, and--tracing offleavingplaywright.request.new_contextuntouched (sync + async).Fixes #137
Stack created with GitHub Stacks CLI • Give Feedback 💬