-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Deflake test_memo: gate on hydration and capture page diagnostics #6641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+97
−10
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| """Conftest for Playwright integration tests. | ||
|
|
||
| Records browser-side activity (console messages, page errors, websocket | ||
| frames) for each test's page and attaches it to the test report on failure, | ||
| so CI logs contain enough context to diagnose flaky frontend behavior | ||
| post-mortem. | ||
| """ | ||
|
|
||
| import time | ||
|
|
||
| import pytest | ||
|
|
||
| # Cap recorded entries per test so a chatty page can't bloat memory or logs. | ||
| _MAX_DIAGNOSTIC_ENTRIES = 500 | ||
| _MAX_ENTRY_LENGTH = 300 | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def _page_diagnostics(request): | ||
| """Capture console/pageerror/websocket activity from the ``page`` fixture. | ||
|
|
||
| Args: | ||
| request: The pytest fixture request object. | ||
|
|
||
| Yields: | ||
| Control to the test function. | ||
| """ | ||
| if "page" not in request.fixturenames: | ||
| yield | ||
| return | ||
| page = request.getfixturevalue("page") | ||
| log: list[str] = [] | ||
| t0 = time.monotonic() | ||
|
|
||
| def stamp(message: str) -> None: | ||
| if len(log) < _MAX_DIAGNOSTIC_ENTRIES: | ||
| log.append(f"+{time.monotonic() - t0:.3f}s {message[:_MAX_ENTRY_LENGTH]}") | ||
|
|
||
| page.on("console", lambda msg: stamp(f"console.{msg.type}: {msg.text}")) | ||
| page.on("pageerror", lambda exc: stamp(f"pageerror: {exc}")) | ||
|
|
||
| def _on_websocket(ws) -> None: | ||
| stamp(f"ws open: {ws.url}") | ||
| ws.on("framesent", lambda frame: stamp(f"ws sent: {frame}")) | ||
| ws.on("framereceived", lambda frame: stamp(f"ws recv: {frame}")) | ||
| ws.on("close", lambda _ws: stamp("ws close")) | ||
|
|
||
| page.on("websocket", _on_websocket) | ||
| request.node._page_diagnostics = log | ||
| yield | ||
|
|
||
|
|
||
| @pytest.hookimpl(hookwrapper=True) | ||
| def pytest_runtest_makereport(item, call): | ||
| """Attach recorded page diagnostics to failed (or rerun) call reports. | ||
|
|
||
| Args: | ||
| item: The test item being reported on. | ||
| call: The call info for the current test phase. | ||
|
|
||
| Yields: | ||
| Control to other report hooks. | ||
| """ | ||
| outcome = yield | ||
| report = outcome.get_result() | ||
| if report.when != "call" or not (report.failed or report.outcome == "rerun"): | ||
| return | ||
| log = getattr(item, "_page_diagnostics", None) | ||
| if log: | ||
| report.sections.append((f"page diagnostics ({report.outcome})", "\n".join(log))) | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
frameargument passed toframesent/framereceivedcallbacks is a PlaywrightWebSocketFrameobject. It does not override__repr__or__str__, so interpolating it directly with an f-string logs an opaque object address (e.g.<WebSocketFrame object at 0x7f…>) rather than the actual payload. Useframe.payloadto capture the frame contents that make these diagnostics useful.