Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(benchmark): harden CDP navigation outcomes

ldm067e2ff6a1872
diff --git a/moli-benchmark/moli_benchmark/chrome_dcl.py b/moli-benchmark/moli_benchmark/chrome_dcl.py
index 860747b6d..c74f6a6df 100644
--- a/moli-benchmark/moli_benchmark/chrome_dcl.py
+++ b/moli-benchmark/moli_benchmark/chrome_dcl.py
@@ -255,6 +255,16 @@ def _main_document_response_from_message(
and main_document_request_ids is not None
and request_id in main_document_request_ids
)
+ # Download navigations are allowed to omit loaderId. In that case a
+ # Document-typed response is not enough to establish ownership: an old
+ # loader on the same frame may still publish a late response. Require the
+ # request identity observed after Page.navigate was sent.
+ if (
+ expected_loader_id is None
+ and main_document_request_ids is not None
+ and not request_was_document
+ ):
+ return None
if (
isinstance(request_id, str)
and main_document_request_ids
@@ -336,6 +346,11 @@ async def _recv_until_dcl_or_binary_main_resource(
response_mime_type: str | None = None
response_url: str | None = None
main_document_request_ids: set[str] = set()
+ awaited = (
+ "main-document response headers"
+ if download_navigation
+ else "DOMContentLoaded"
+ )
for message in seen:
response = _main_document_response_from_message(
message,
@@ -371,8 +386,11 @@ async def _recv_until_dcl_or_binary_main_resource(
while True:
remaining = deadline - time.perf_counter()
if remaining <= 0:
- raise TimeoutError("timed out waiting for Page.domContentEventFired")
- message = await asyncio.wait_for(client.recv(), timeout=remaining)
+ raise TimeoutError(f"timed out waiting for {awaited}")
+ try:
+ message = await asyncio.wait_for(client.recv(), timeout=remaining)
+ except TimeoutError as error:
+ raise TimeoutError(f"timed out waiting for {awaited}") from error
response = _main_document_response_from_message(
message,
session_id=session_id,
@@ -498,7 +516,8 @@ async def _dump_dcl_html(
download_navigation=navigation.is_download,
)
except TimeoutError as error:
- raise CdpDclDumpTimeoutError("DCL", error) from error
+ stage = "response headers" if navigation.is_download else "DCL"
+ raise CdpDclDumpTimeoutError(stage, error) from error
if main_document.headers_only:
return CdpDclDumpResult(
body="",
diff --git a/moli-benchmark/tests/test_chrome_dcl.py b/moli-benchmark/tests/test_chrome_dcl.py
index 83a464afa..b62373c34 100644
--- a/moli-benchmark/tests/test_chrome_dcl.py
+++ b/moli-benchmark/tests/test_chrome_dcl.py
@@ -93,6 +93,7 @@ def _document_response_message(
request_id: str = "REQUEST-1",
status: int = 200,
loader_id: str = "LOADER-1",
+ url: str = "https://example.test/final",
) -> dict[str, object]:
return {
"sessionId": "SID-1",
@@ -105,7 +106,7 @@ def _document_response_message(
"response": {
"mimeType": mime_type,
"status": status,
- "url": "https://example.test/final",
+ "url": url,
},
},
}
@@ -115,6 +116,7 @@ def _document_request_message(
*,
request_id: str = "REQUEST-1",
loader_id: str = "LOADER-1",
+ url: str = "https://example.test/",
) -> dict[str, object]:
return {
"sessionId": "SID-1",
@@ -124,7 +126,7 @@ def _document_request_message(
"frameId": "FRAME-1",
"loaderId": loader_id,
"requestId": request_id,
- "request": {"url": "https://example.test/"},
+ "request": {"url": url},
},
}
@@ -252,13 +254,46 @@ class ChromeDclTests(unittest.TestCase):
self.assertEqual(navigation.loader_id, "LOADER-1")
self.assertFalse(navigation.is_download)
+ def test_navigation_result_only_treats_literal_true_as_download(self) -> None:
+ navigation = _parse_navigation_result(
+ {
+ "result": {
+ "frameId": "FRAME-1",
+ "errorText": "net::ERR_ABORTED",
+ "isDownload": True,
+ }
+ },
+ "https://example.test/archive.zip",
+ )
+ self.assertTrue(navigation.is_download)
+
+ with self.assertRaises(RawCdpError):
+ _parse_navigation_result(
+ {
+ "result": {
+ "frameId": "FRAME-1",
+ "errorText": "net::ERR_ABORTED",
+ "isDownload": "true",
+ }
+ },
+ "https://example.test/no-content",
+ )
+
+ def test_download_navigation_does_not_require_error_text_or_loader_id(self) -> None:
+ navigation = _parse_navigation_result(
+ {"result": {"frameId": "FRAME-1", "isDownload": True}},
+ "https://example.test/archive.zip",
+ )
+
+ self.assertTrue(navigation.is_download)
+ self.assertIsNone(navigation.loader_id)
+
def test_download_navigation_keeps_abort_as_headers_only_evidence(self) -> None:
async def run() -> None:
navigation = _parse_navigation_result(
{
"result": {
"frameId": "FRAME-1",
- "loaderId": "LOADER-1",
"errorText": "net::ERR_ABORTED",
"isDownload": True,
}
@@ -272,7 +307,10 @@ class ChromeDclTests(unittest.TestCase):
frame_id=navigation.frame_id,
expected_loader_id=navigation.loader_id,
deadline=time.perf_counter() + 1.0,
- seen=[_document_response_message("application/zip")],
+ seen=[
+ _document_request_message(),
+ _document_response_message("application/zip"),
+ ],
download_navigation=navigation.is_download,
)
@@ -283,6 +321,94 @@ class ChromeDclTests(unittest.TestCase):
asyncio.run(run())
+ def test_download_navigation_is_authoritative_over_mime_and_status(self) -> None:
+ async def run() -> None:
+ observation = await _recv_until_dcl_or_binary_main_resource(
+ mock.Mock(),
+ session_id="SID-1",
+ frame_id="FRAME-1",
+ expected_loader_id="LOADER-1",
+ deadline=time.perf_counter() + 1.0,
+ seen=[
+ _document_response_message(
+ "text/html; charset=utf-8",
+ status=404,
+ )
+ ],
+ download_navigation=True,
+ )
+
+ self.assertTrue(observation.headers_only)
+ self.assertEqual(observation.response_status, 404)
+ self.assertEqual(observation.response_mime_type, "text/html; charset=utf-8")
+
+ asyncio.run(run())
+
+ def test_download_without_loader_ignores_stale_response_until_current_request(self) -> None:
+ async def run() -> None:
+ current_url = "https://example.test/current.zip"
+ client = _QueuedMessageClient(
+ [
+ _document_request_message(
+ request_id="CURRENT",
+ loader_id="LOADER-CURRENT",
+ url=current_url,
+ ),
+ _document_response_message(
+ "application/zip",
+ request_id="CURRENT",
+ loader_id="LOADER-CURRENT",
+ url=current_url,
+ ),
+ ]
+ )
+ observation = await _recv_until_dcl_or_binary_main_resource( # type: ignore[arg-type]
+ client,
+ session_id="SID-1",
+ frame_id="FRAME-1",
+ expected_loader_id=None,
+ deadline=time.perf_counter() + 1.0,
+ seen=[
+ _document_response_message(
+ "application/zip",
+ request_id="STALE",
+ loader_id="LOADER-STALE",
+ status=206,
+ url="https://example.test/stale.zip",
+ ),
+ _dcl_lifecycle_message(loader_id="LOADER-STALE"),
+ ],
+ download_navigation=True,
+ )
+
+ self.assertTrue(observation.headers_only)
+ self.assertEqual(observation.response_status, 200)
+ self.assertEqual(observation.final_url, current_url)
+ self.assertEqual(client.messages, [])
+
+ asyncio.run(run())
+
+ def test_wait_timeout_names_download_and_document_stages(self) -> None:
+ async def run() -> None:
+ for is_download, expected in (
+ (True, "main-document response headers"),
+ (False, "DOMContentLoaded"),
+ ):
+ with self.subTest(download=is_download):
+ with self.assertRaises(TimeoutError) as raised:
+ await _recv_until_dcl_or_binary_main_resource(
+ mock.Mock(),
+ session_id="SID-1",
+ frame_id="FRAME-1",
+ expected_loader_id=None,
+ deadline=time.perf_counter() - 1.0,
+ seen=[],
+ download_navigation=is_download,
+ )
+ self.assertIn(expected, str(raised.exception))
+
+ asyncio.run(run())
+
def test_binary_main_document_response_returns_mime_evidence(self) -> None:
mime_type = _binary_main_resource_mime_type_from_message(
_document_response_message("application/pdf"),
diff --git a/moli-cdp-smoke/README.md b/moli-cdp-smoke/README.md
index b570004d8..73565ff69 100644
--- a/moli-cdp-smoke/README.md
+++ b/moli-cdp-smoke/README.md
@@ -80,6 +80,24 @@ both sessions. The identical probe timed out at the replacement evaluation after
10,000 ms when Moli's parent-session renderer-inspector detach fix was removed,
and passed after the fix was restored.
+The `navigation-outcomes` group was calibrated on 2026-08-23 against Debian
+`/usr/bin/chromium` 145.0.7632.116 and then run unchanged against Moli. It
+directly drives `Page.navigate` and correlates the result with the matching
+Document request/response identity. The matrix covers text, renderable HTML,
+empty, redirected, and HTTP-error attachments; unsupported binary MIME; an
+ordinary HTML 502 document; 204 No Content; and a reset before response
+headers. Chromium reports successful attachments as `isDownload=true` plus
+`net::ERR_ABORTED` while retaining the old Document, but a 204 reports the same
+error with `isDownload=false`; the smoke therefore treats `isDownload`, not the
+error string, as authoritative. Moli currently commits binary responses and
+204 responses as external Documents where Chromium retains the old Document;
+those two cases assert internally coherent response/lifecycle evidence and
+record the engine-selected outcome rather than claiming parity. For a 404
+attachment, both engines preserve `isDownload` and the HTTP response evidence,
+while Chromium commits an error Document with `net::ERR_INVALID_RESPONSE` and
+Moli retains the prior Document with `net::ERR_ABORTED`; that difference is
+also recorded rather than hidden behind an exact error-string assertion.
+
## Current Coverage
The current suite is a strong core smoke gate, not a complete Playwright compatibility suite.
@@ -213,6 +231,10 @@ Covered well:
- Locator/input workflows for fill/clear, Playwright user-facing `get_by_test_id()` / `get_by_text()` / `get_by_label()` / `get_by_placeholder()` / `get_by_alt_text()` / `get_by_title()` / `get_by_role()` plus role selector selected/checked/pressed/expanded/disabled/level/name/include-hidden filters, Playwright `expect(locator)` text/count/value/attribute/class/visible/hidden/enabled/disabled/checked matchers, `has_text` / `has` / `filter()` locator composition, upstream-derived `first()` / `last()` / `nth()` / `and_()` / `or_()` / Locator-argument composition and `FrameLocator.locator()` workflows, `$eval` / `$$eval` selector evaluation, `page.type()` selection/focus behavior, `fill()` input/change events, `locator.clear()` input event, input type/error/auto-wait behavior, `check()` / `uncheck()` / `setChecked()` state, aria-role, trial, error, and label-retarget behavior, type, press, keyboard modifiers, basic contenteditable editing, hover, checkbox, radio, richer Playwright `selectOption()` value/label/index/handle/multiple/wait behavior, click-triggered navigation, and drag/drop. Raw CDP smoke checks invalid-parameter priority plus explicit layout hit-testing errors for mouse, touch, emulated touch, tap, and drag, and verifies that none dispatch DOM events. Playwright `page.mouse` click/dblclick/buttons/move/wheel workflows enforce the same boundary.
- DOM/handle workflows for `locator.evaluate()`, `ElementHandle` queries, `ElementHandle.content_frame()`, `ElementHandle.wait_for_element_state()` visible/hidden/enabled/editable/timeout/detached behavior, `JSHandle.as_element()`, `JSHandle` property reads, nested/same-handle `evaluate()` arguments, console events with JSHandle args, bounding boxes, owner frames, child-frame evaluation, detached-handle behavior after navigation, and isolated-world DOM resolution through Playwright's injected scripts.
- Download event, downloaded artifact, and download cancellation.
+- Direct `Page.navigate` outcome coverage for attachments, empty and redirected
+ downloads, download HTTP errors, binary MIME responses, ordinary HTTP error
+ Documents, 204 responses, and transport failures, including response/request
+ correlation and retained-Document checks.
- Viewport resize, the explicit Playwright-generated screenshot clip boundary, and Chromium-calibrated geolocation
position/unavailable/clear behavior across navigation and auxiliary CDP sessions.
- localStorage, sessionStorage, basic IndexedDB, multi-context cookie isolation,
@@ -269,6 +291,7 @@ Runner layout:
- `groups/chromium_cdp.py`: Chromium inspector-protocol derived samples for Page, Runtime, Input, IO Blob streams, Performance, Profiler, and DOM contracts.
- `groups/document_content.py`: Playwright and raw-CDP `Page.setDocumentContent` replacement identity, parser pause/resume, child-frame, and error-atomicity workflows.
- `groups/error_document.py`: failed main-document transport error Document identity, lifecycle, realm replacement, recovery, and multi-target isolation.
+- `groups/navigation_outcomes.py`: direct Page.navigate download/no-document/error outcomes and matching Network evidence.
- `groups/dom_parser_mutations.py`: cross-engine raw-CDP parser-tail mutation publication and commit/DCL DOM binding barriers.
- `groups/layout_screenshot.py`: raw current-viewport PNG, DevTools parameter compatibility, paint/layout mutation freshness, open/closed Shadow Root and iframe TreeScope stability, generation-gated 1 FPS JPEG screencast/ACK behavior, and Moli default-Mock restart boundary.
- `groups/action_window.py`: raw wheel admission/deadline batching, screenshot
@@ -346,6 +369,7 @@ uv run moli-cdp-smoke --group dom-snapshot
uv run moli-cdp-smoke --group dom-whitespace
uv run moli-cdp-smoke --group computed-style
uv run moli-cdp-smoke --group error-document
+uv run moli-cdp-smoke --group navigation-outcomes
uv run moli-cdp-smoke --group url-policy
uv run moli-cdp-smoke --group inspector-routing
MOLI_SMOKE_GROUPS=protocol,websocket uv run moli-cdp-smoke
diff --git a/moli-cdp-smoke/moli_cdp_smoke/fixture.py b/moli-cdp-smoke/moli_cdp_smoke/fixture.py
index bdf900314..c43af2c60 100644
--- a/moli-cdp-smoke/moli_cdp_smoke/fixture.py
+++ b/moli-cdp-smoke/moli_cdp_smoke/fixture.py
@@ -606,6 +606,12 @@ class FixtureServer:
self.send_header("Content-Length", "0")
self.end_headers()
return
+ if route == "/navigation-redirect-download":
+ self.send_response(HTTPStatus.FOUND)
+ self.send_header("Location", "/navigation-download-html")
+ self.send_header("Content-Length", "0")
+ self.end_headers()
+ return
if route == "/chromium-network-reset-before-response":
outer._increment_request_count(route)
self.connection.setsockopt(
@@ -1584,6 +1590,51 @@ class FixtureServer:
)
elif route == "/download":
self._send_download("smoke-download.txt", b"download contents")
+ elif route == "/navigation-download-html":
+ self._send_common(
+ HTTPStatus.OK,
+ "text/html; charset=utf-8",
+ b"<!doctype html><title>must download</title>",
+ {
+ "Content-Disposition": (
+ 'attachment; filename="navigation-download.html"'
+ )
+ },
+ )
+ elif route == "/navigation-download-empty":
+ self._send_common(
+ HTTPStatus.OK,
+ "application/zip",
+ b"",
+ {
+ "Content-Disposition": (
+ 'attachment; filename="navigation-empty.zip"'
+ )
+ },
+ )
+ elif route == "/navigation-download-http-error":
+ self._send_common(
+ HTTPStatus.NOT_FOUND,
+ "text/plain; charset=utf-8",
+ b"download not found",
+ {
+ "Content-Disposition": (
+ 'attachment; filename="navigation-missing.txt"'
+ )
+ },
+ )
+ elif route == "/navigation-http-error":
+ self._send_common(
+ HTTPStatus.BAD_GATEWAY,
+ "text/html; charset=utf-8",
+ b"<!doctype html><title>gateway error</title><main>gateway error</main>",
+ )
+ elif route == "/navigation-no-content":
+ self._send_common(
+ HTTPStatus.NO_CONTENT,
+ "text/plain; charset=utf-8",
+ b"",
+ )
elif route == "/slow-download":
self._send_slow_download()
else:
diff --git a/moli-cdp-smoke/moli_cdp_smoke/groups/navigation_outcomes.py b/moli-cdp-smoke/moli_cdp_smoke/groups/navigation_outcomes.py
new file mode 100644
index 000000000..b49e50e22
--- /dev/null
+++ b/moli-cdp-smoke/moli_cdp_smoke/groups/navigation_outcomes.py
@@ -0,0 +1,553 @@
+from __future__ import annotations
+
+from contextlib import asynccontextmanager, suppress
+from dataclasses import dataclass
+from typing import Any, AsyncIterator, Awaitable, Callable
+
+from ..assertions import SmokeError, assert_equal, record_contract, wait_until
+from ..helpers import attach_cdp_event_collector
+from ..state import SmokeState
+
+
+_OLD_DOCUMENT_MARKER = "navigation-outcome-old-document"
+
+NavigationScenario = Callable[[SmokeState], Awaitable[dict[str, Any]]]
+
+
+@dataclass(frozen=True)
+class NavigationOutcomeContract:
+ name: str
+ contract: str
+ scenario: NavigationScenario
+
+
+@dataclass
+class NavigationProbe:
+ page: Any
+ cdp: Any
+ events: list[dict[str, Any]]
+
+
+async def run_navigation_outcomes_group(state: SmokeState) -> None:
+ source = (
+ "raw Page.navigate and Network observation calibrated against Debian "
+ "Chromium 145.0.7632.116"
+ )
+ commands = [
+ "Page.enable",
+ "Page.setLifecycleEventsEnabled",
+ "Runtime.enable",
+ "Network.enable",
+ "Page.navigate",
+ "Runtime.evaluate",
+ ]
+ for item in _contracts():
+ try:
+ observed = await item.scenario(state)
+ except Exception as error:
+ state.results.append(
+ {
+ "name": item.name,
+ "ok": False,
+ "contract": item.contract,
+ "source": source,
+ "commands": commands,
+ "errorType": type(error).__name__,
+ "error": str(error),
+ }
+ )
+ else:
+ record_contract(
+ state.results,
+ item.name,
+ contract=item.contract,
+ source=source,
+ commands=commands,
+ observed=observed,
+ )
+
+
+def _contracts() -> tuple[NavigationOutcomeContract, ...]:
+ return (
+ NavigationOutcomeContract(
+ "navigate_attachment_retains_active_document",
+ "A text attachment reports isDownload with response metadata and leaves the active Document and realm untouched.",
+ _text_attachment,
+ ),
+ NavigationOutcomeContract(
+ "navigate_html_attachment_overrides_renderable_mime",
+ "Content-Disposition attachment remains a download even when the response MIME is renderable HTML.",
+ _html_attachment,
+ ),
+ NavigationOutcomeContract(
+ "navigate_empty_attachment_is_download",
+ "A zero-byte attachment still reports a download and retains the active Document.",
+ _empty_attachment,
+ ),
+ NavigationOutcomeContract(
+ "navigate_redirect_to_attachment_uses_final_response",
+ "A redirect into an attachment keeps one Document request identity and exposes the final response as a download.",
+ _redirect_attachment,
+ ),
+ NavigationOutcomeContract(
+ "navigate_error_status_attachment_keeps_http_evidence",
+ "isDownload remains authoritative when an attachment has an HTTP error status, while Network preserves that status and MIME for classification.",
+ _error_status_attachment,
+ ),
+ NavigationOutcomeContract(
+ "navigate_binary_mime_outcome_is_self_consistent",
+ "A binary main response always exposes headers and either becomes a download or commits a lifecycle-bearing external Document without mixing the two outcomes.",
+ _binary_mime,
+ ),
+ NavigationOutcomeContract(
+ "navigate_http_error_commits_document",
+ "An ordinary HTML HTTP error is a successful navigation with status evidence and a DOMContentLoaded Document, not a Page.navigate network error.",
+ _http_error_document,
+ ),
+ NavigationOutcomeContract(
+ "navigate_no_content_is_not_download",
+ "A 204 response is never inferred to be a download merely because Chromium reports net::ERR_ABORTED.",
+ _no_content,
+ ),
+ NavigationOutcomeContract(
+ "navigate_transport_failure_is_not_download",
+ "A reset before response metadata reports a non-download navigation error and a matching Network.loadingFailed terminal.",
+ _transport_failure,
+ ),
+ )
+
+
+@asynccontextmanager
+async def _new_probe(state: SmokeState) -> AsyncIterator[NavigationProbe]:
+ page = await state.context.new_page()
+ cdp = await state.context.new_cdp_session(page)
+ events = attach_cdp_event_collector(
+ cdp,
+ [
+ "Network.requestWillBeSent",
+ "Network.responseReceived",
+ "Network.loadingFailed",
+ "Page.lifecycleEvent",
+ ],
+ )
+ try:
+ await cdp.send("Page.enable")
+ await cdp.send("Network.enable")
+ await cdp.send("Runtime.enable")
+ await cdp.send("Page.setLifecycleEventsEnabled", {"enabled": True})
+ marker = await _evaluate_value(
+ cdp,
+ f"globalThis.__navigationOutcomeMarker = {_OLD_DOCUMENT_MARKER!r}",
+ )
+ assert_equal(marker, _OLD_DOCUMENT_MARKER, "initial Document marker")
+ events.clear()
+ yield NavigationProbe(page=page, cdp=cdp, events=events)
+ finally:
+ with suppress(Exception):
+ await cdp.detach()
+ with suppress(Exception):
+ await page.close()
+
+
+async def _text_attachment(state: SmokeState) -> dict[str, Any]:
+ return await _successful_download(
+ state,
+ route="/download",
+ expected_mime="text/plain",
+ )
+
+
+async def _html_attachment(state: SmokeState) -> dict[str, Any]:
+ return await _successful_download(
+ state,
+ route="/navigation-download-html",
+ expected_mime="text/html",
+ )
+
+
+async def _empty_attachment(state: SmokeState) -> dict[str, Any]:
+ return await _successful_download(
+ state,
+ route="/navigation-download-empty",
+ expected_mime="application/zip",
+ )
+
+
+async def _successful_download(
+ state: SmokeState,
+ *,
+ route: str,
+ expected_mime: str,
+) -> dict[str, Any]:
+ async with _new_probe(state) as probe:
+ url = f"{state.fixture}{route}"
+ result = await probe.cdp.send("Page.navigate", {"url": url})
+ _assert_download_result(result, "net::ERR_ABORTED")
+ response_event = await _wait_for_document_response(probe, url)
+ _assert_response(response_event, status=200, mime=expected_mime, url=url)
+ _assert_request_response_identity(probe.events, response_event, url)
+ snapshot = await _active_document_snapshot(probe.cdp)
+ _assert_retained_document(snapshot)
+ _assert_no_navigation_dcl(probe.events, response_event)
+ return _compact_observation(result, response_event, snapshot)
+
+
+async def _redirect_attachment(state: SmokeState) -> dict[str, Any]:
+ async with _new_probe(state) as probe:
+ requested_url = f"{state.fixture}/navigation-redirect-download"
+ final_url = f"{state.fixture}/navigation-download-html"
+ result = await probe.cdp.send("Page.navigate", {"url": requested_url})
+ _assert_download_result(result, "net::ERR_ABORTED")
+ response_event = await _wait_for_document_response(probe, final_url)
+ _assert_response(response_event, status=200, mime="text/html", url=final_url)
+
+ requests = _document_request_events(probe.events)
+ request_urls = [_request_url(event) for event in requests]
+ assert_equal(
+ request_urls,
+ [requested_url, final_url],
+ "redirect download request chain",
+ )
+ request_ids = {_required_string(event["params"], "requestId") for event in requests}
+ if len(request_ids) != 1:
+ raise SmokeError(
+ f"redirect download changed Network request identity: {request_ids}"
+ )
+ redirect_response = requests[-1]["params"].get("redirectResponse")
+ if not isinstance(redirect_response, dict):
+ raise SmokeError("redirect download omitted redirectResponse")
+ assert_equal(int(redirect_response.get("status", 0)), 302, "redirect status")
+
+ snapshot = await _active_document_snapshot(probe.cdp)
+ _assert_retained_document(snapshot)
+ _assert_no_navigation_dcl(probe.events, response_event)
+ observed = _compact_observation(result, response_event, snapshot)
+ observed["requestUrls"] = request_urls
+ observed["requestId"] = next(iter(request_ids))
+ return observed
+
+
+async def _error_status_attachment(state: SmokeState) -> dict[str, Any]:
+ async with _new_probe(state) as probe:
+ url = f"{state.fixture}/navigation-download-http-error"
+ result = await probe.cdp.send("Page.navigate", {"url": url})
+ if result.get("isDownload") is not True:
+ raise SmokeError(f"HTTP-error attachment was not marked as download: {result}")
+ error_text = _required_string(result, "errorText")
+ if not error_text.startswith("net::ERR_"):
+ raise SmokeError(f"unexpected attachment navigation errorText: {error_text}")
+ response_event = await _wait_for_document_response(probe, url)
+ _assert_response(response_event, status=404, mime="text/plain", url=url)
+ _assert_request_response_identity(probe.events, response_event, url)
+ snapshot = await _active_document_snapshot(probe.cdp)
+ return _compact_observation(result, response_event, snapshot)
+
+
+async def _binary_mime(state: SmokeState) -> dict[str, Any]:
+ async with _new_probe(state) as probe:
+ url = f"{state.fixture}/chromium-resource-xhr.bin"
+ result = await probe.cdp.send("Page.navigate", {"url": url})
+ response_event = await _wait_for_document_response(probe, url)
+ _assert_response(
+ response_event,
+ status=200,
+ mime="application/octet-stream",
+ url=url,
+ )
+ snapshot = await _active_document_snapshot(probe.cdp)
+ if result.get("isDownload") is True:
+ _assert_retained_document(snapshot)
+ _assert_no_navigation_dcl(probe.events, response_event)
+ mode = "download"
+ else:
+ if result.get("errorText"):
+ raise SmokeError(f"binary external Document reported an error: {result}")
+ await _wait_for_navigation_dcl(probe, response_event)
+ assert_equal(snapshot.get("href"), url, "binary external Document URL")
+ mode = "external-document"
+ observed = _compact_observation(result, response_event, snapshot)
+ observed["outcome"] = mode
+ return observed
+
+
+async def _http_error_document(state: SmokeState) -> dict[str, Any]:
+ async with _new_probe(state) as probe:
+ url = f"{state.fixture}/navigation-http-error"
+ result = await probe.cdp.send("Page.navigate", {"url": url})
+ _assert_not_download_or_navigation_error(result)
+ response_event = await _wait_for_document_response(probe, url)
+ _assert_response(response_event, status=502, mime="text/html", url=url)
+ await _wait_for_navigation_dcl(probe, response_event)
+ snapshot = await _active_document_snapshot(probe.cdp)
+ assert_equal(snapshot.get("href"), url, "HTTP error Document URL")
+ if "gateway error" not in str(snapshot.get("text", "")):
+ raise SmokeError(f"HTTP error body was not committed: {snapshot}")
+ return _compact_observation(result, response_event, snapshot)
+
+
+async def _no_content(state: SmokeState) -> dict[str, Any]:
+ async with _new_probe(state) as probe:
+ url = f"{state.fixture}/navigation-no-content"
+ result = await probe.cdp.send("Page.navigate", {"url": url})
+ if result.get("isDownload") is True:
+ raise SmokeError(f"204 response was misreported as a download: {result}")
+ response_event = await _wait_for_document_response(probe, url)
+ _assert_response(response_event, status=204, mime="text/plain", url=url)
+ snapshot = await _active_document_snapshot(probe.cdp)
+ error_text = result.get("errorText")
+ if error_text:
+ assert_equal(error_text, "net::ERR_ABORTED", "204 navigation errorText")
+ _assert_retained_document(snapshot)
+ outcome = "retained-document"
+ else:
+ assert_equal(snapshot.get("href"), url, "204 committed Document URL")
+ outcome = "committed-document"
+ observed = _compact_observation(result, response_event, snapshot)
+ observed["outcome"] = outcome
+ return observed
+
+
+async def _transport_failure(state: SmokeState) -> dict[str, Any]:
+ async with _new_probe(state) as probe:
+ url = f"{state.fixture}/chromium-network-reset-before-response?navigation-outcome=1"
+ result = await probe.cdp.send("Page.navigate", {"url": url})
+ if result.get("isDownload") is True:
+ raise SmokeError(f"transport failure was misreported as download: {result}")
+ error_text = _required_string(result, "errorText")
+ await wait_until(
+ lambda: _loading_failure_for_url(probe.events, url) is not None,
+ "main-document Network.loadingFailed",
+ )
+ failure = _loading_failure_for_url(probe.events, url)
+ if failure is None:
+ raise SmokeError("missing main-document Network.loadingFailed")
+ assert_equal(
+ failure["params"].get("errorText"),
+ error_text,
+ "Page.navigate and Network.loadingFailed error",
+ )
+ if _document_response_for_url(probe.events, url) is not None:
+ raise SmokeError("reset-before-response unexpectedly exposed response metadata")
+ snapshot = await _active_document_snapshot(probe.cdp)
+ if snapshot.get("href") == url:
+ raise SmokeError(f"transport failure committed the requested URL: {snapshot}")
+ return {
+ "frameId": result.get("frameId"),
+ "loaderId": result.get("loaderId"),
+ "isDownload": result.get("isDownload"),
+ "errorText": error_text,
+ "activeUrl": snapshot.get("href"),
+ "requestId": failure["params"].get("requestId"),
+ }
+
+
+async def _wait_for_document_response(
+ probe: NavigationProbe,
+ url: str,
+) -> dict[str, Any]:
+ await wait_until(
+ lambda: _document_response_for_url(probe.events, url) is not None,
+ f"main-document response for {url}",
+ )
+ response = _document_response_for_url(probe.events, url)
+ if response is None:
+ raise SmokeError(f"missing main-document response for {url}")
+ return response
+
+
+async def _wait_for_navigation_dcl(
+ probe: NavigationProbe,
+ response_event: dict[str, Any],
+) -> None:
+ loader_id = _required_string(response_event["params"], "loaderId")
+ await wait_until(
+ lambda: any(
+ event.get("method") == "Page.lifecycleEvent"
+ and event.get("params", {}).get("loaderId") == loader_id
+ and event.get("params", {}).get("name")
+ in {"DOMContentLoaded", "domContentLoaded"}
+ for event in probe.events
+ ),
+ f"DOMContentLoaded for loader {loader_id}",
+ )
+
+
+def _assert_download_result(result: dict[str, Any], error_text: str) -> None:
+ if result.get("isDownload") is not True:
+ raise SmokeError(f"Page.navigate did not report isDownload=true: {result}")
+ assert_equal(result.get("errorText"), error_text, "download navigation errorText")
+ _required_string(result, "frameId")
+
+
+def _assert_not_download_or_navigation_error(result: dict[str, Any]) -> None:
+ if result.get("isDownload") is True:
+ raise SmokeError(f"ordinary Document was misreported as download: {result}")
+ if result.get("errorText"):
+ raise SmokeError(f"ordinary Document reported navigation error: {result}")
+ _required_string(result, "frameId")
+ _required_string(result, "loaderId")
+
+
+def _assert_response(
+ event: dict[str, Any],
+ *,
+ status: int,
+ mime: str,
+ url: str,
+) -> None:
+ response = event.get("params", {}).get("response")
+ if not isinstance(response, dict):
+ raise SmokeError(f"Network.responseReceived omitted response: {event}")
+ assert_equal(int(response.get("status", 0)), status, "main-document status")
+ observed_mime = str(response.get("mimeType", "")).split(";", 1)[0].lower()
+ assert_equal(observed_mime, mime, "main-document MIME")
+ assert_equal(response.get("url"), url, "main-document final URL")
+
+
+def _assert_request_response_identity(
+ events: list[dict[str, Any]],
+ response_event: dict[str, Any],
+ url: str,
+) -> None:
+ request = next(
+ (event for event in _document_request_events(events) if _request_url(event) == url),
+ None,
+ )
+ if request is None:
+ raise SmokeError(f"missing Document request for {url}")
+ assert_equal(
+ response_event["params"].get("requestId"),
+ request["params"].get("requestId"),
+ "main-document request/response identity",
+ )
+
+
+def _assert_retained_document(snapshot: dict[str, Any]) -> None:
+ assert_equal(snapshot.get("href"), "about:blank", "retained active Document URL")
+ assert_equal(
+ snapshot.get("marker"),
+ _OLD_DOCUMENT_MARKER,
+ "retained active Document realm marker",
+ )
+
+
+def _assert_no_navigation_dcl(
+ events: list[dict[str, Any]],
+ response_event: dict[str, Any],
+) -> None:
+ loader_id = response_event.get("params", {}).get("loaderId")
+ matching = [
+ event
+ for event in events
+ if event.get("method") == "Page.lifecycleEvent"
+ and event.get("params", {}).get("loaderId") == loader_id
+ and event.get("params", {}).get("name")
+ in {"DOMContentLoaded", "domContentLoaded"}
+ ]
+ if matching:
+ raise SmokeError(f"download navigation emitted DOMContentLoaded: {matching}")
+
+
+def _document_request_events(events: list[dict[str, Any]]) -> list[dict[str, Any]]:
+ return [
+ event
+ for event in events
+ if event.get("method") == "Network.requestWillBeSent"
+ and event.get("params", {}).get("type") == "Document"
+ ]
+
+
+def _document_response_for_url(
+ events: list[dict[str, Any]],
+ url: str,
+) -> dict[str, Any] | None:
+ for event in events:
+ if event.get("method") != "Network.responseReceived":
+ continue
+ params = event.get("params", {})
+ response = params.get("response", {})
+ if params.get("type") == "Document" and response.get("url") == url:
+ return event
+ return None
+
+
+def _loading_failure_for_url(
+ events: list[dict[str, Any]],
+ url: str,
+) -> dict[str, Any] | None:
+ request_ids = {
+ event.get("params", {}).get("requestId")
+ for event in _document_request_events(events)
+ if _request_url(event) == url
+ }
+ for event in events:
+ if (
+ event.get("method") == "Network.loadingFailed"
+ and event.get("params", {}).get("requestId") in request_ids
+ ):
+ return event
+ return None
+
+
+def _request_url(event: dict[str, Any]) -> str | None:
+ request = event.get("params", {}).get("request")
+ return request.get("url") if isinstance(request, dict) else None
+
+
+async def _active_document_snapshot(cdp: Any) -> dict[str, Any]:
+ value = await _evaluate_value(
+ cdp,
+ """
+ ({
+ href: location.href,
+ marker: globalThis.__navigationOutcomeMarker ?? null,
+ text: document.body?.textContent ?? '',
+ })
+ """,
+ )
+ if not isinstance(value, dict):
+ raise SmokeError(f"unexpected active Document snapshot: {value!r}")
+ return value
+
+
+async def _evaluate_value(cdp: Any, expression: str) -> Any:
+ response = await cdp.send(
+ "Runtime.evaluate",
+ {
+ "expression": expression,
+ "returnByValue": True,
+ "awaitPromise": True,
+ },
+ )
+ if response.get("exceptionDetails") is not None:
+ raise SmokeError(f"Runtime.evaluate failed: {response['exceptionDetails']}")
+ return response.get("result", {}).get("value")
+
+
+def _compact_observation(
+ result: dict[str, Any],
+ response_event: dict[str, Any],
+ snapshot: dict[str, Any],
+) -> dict[str, Any]:
+ params = response_event["params"]
+ response = params["response"]
+ return {
+ "frameId": result.get("frameId"),
+ "loaderId": result.get("loaderId"),
+ "networkLoaderId": params.get("loaderId"),
+ "requestId": params.get("requestId"),
+ "isDownload": result.get("isDownload"),
+ "errorText": result.get("errorText"),
+ "status": int(response.get("status", 0)),
+ "mimeType": response.get("mimeType"),
+ "finalUrl": response.get("url"),
+ "activeUrl": snapshot.get("href"),
+ "oldRealmRetained": snapshot.get("marker") == _OLD_DOCUMENT_MARKER,
+ }
+
+
+def _required_string(value: dict[str, Any], key: str) -> str:
+ field = value.get(key)
+ if not isinstance(field, str) or not field:
+ raise SmokeError(f"missing {key}: {value}")
+ return field
diff --git a/moli-cdp-smoke/moli_cdp_smoke/runner.py b/moli-cdp-smoke/moli_cdp_smoke/runner.py
index f3ed7f948..0a046f09b 100644
--- a/moli-cdp-smoke/moli_cdp_smoke/runner.py
+++ b/moli-cdp-smoke/moli_cdp_smoke/runner.py
@@ -47,6 +47,7 @@ from .groups.network import (
run_page_network_group,
run_websocket_group,
)
+from .groups.navigation_outcomes import run_navigation_outcomes_group
from .groups.pdf import run_pdf_group
from .groups.playwright_compat import run_playwright_compat_group
from .groups.protocol import run_raw_protocol_group
@@ -211,6 +212,12 @@ PAGE_GROUPS: tuple[SmokeGroup, ...] = (
"page",
run_error_document_group,
),
+ SmokeGroup(
+ "navigation-outcomes",
+ "Chromium-calibrated direct Page.navigate downloads, redirects, binary/no-content responses, HTTP errors, and transport failures.",
+ "page",
+ run_navigation_outcomes_group,
+ ),
SmokeGroup(
"computed-style",
"Cross-engine CSSOM and CSS.getComputedStyleForNode breadth, stability, and mutation contracts.",
diff --git a/moli-cdp-smoke/tests/test_group_selection.py b/moli-cdp-smoke/tests/test_group_selection.py
index 38e05f906..994221e9b 100644
--- a/moli-cdp-smoke/tests/test_group_selection.py
+++ b/moli-cdp-smoke/tests/test_group_selection.py
@@ -27,6 +27,7 @@ class GroupSelectionTests(unittest.TestCase):
)
self.assertEqual(DEFAULT_GROUP_NAMES, tuple(group.name for group in DEFAULT_GROUPS))
self.assertIn("inspector-routing", DEFAULT_GROUP_NAMES)
+ self.assertIn("navigation-outcomes", DEFAULT_GROUP_NAMES)
self.assertIn("puppeteer", DEFAULT_GROUP_NAMES)
self.assertEqual(
tuple(group.name for group in MANAGED_EXTERNAL_GROUPS),