Files
camoufox/tests/async/test_page_request_intercept.py
Jake WriterandClaude Opus 5 116a534e03 test: fix the Playwright suite's own defects, not the browser's
Triaged all 76 failures from the full run. None was a Camoufox browser bug --
every one was the vendored harness disagreeing with the Playwright it runs
against, or asserting a response no real server sends. Each was checked against
stock Firefox before being written off.

Harness bugs that hid real coverage:
* conftest's RemoteServer wrote snake_case launch options into a JSON consumed
  by the Node driver, which needs camelCase, so `executable_path` was dropped
  and launch-server fell back to a Firefox that isn't installed. It printed no
  endpoint and all 16 connect tests died on an empty ws_endpoint with a
  nonsense "Port should be >= 0 and < 65536. Received type string ('')".
  Also drop None-valued options: a null `channel` aborts the driver outright.
  16 failed -> 17 passed.
* tests/server.py answered 404/401 with a bare status line -- no Content-Type,
  no body. Gecko renders that through the plaintext viewer and then never
  fires `load`, leaving readyState at "interactive" forever, so page.goto()
  (which waits for `load`) hung for the full timeout. That single defect
  accounted for 17 of the 76 failures across five files, and cost 30s each.
  Confirmed on stock Firefox too, so it is Gecko behaviour, not ours -- real
  servers always send a body. clearcookies alone: 5 failed in 152s -> 7 passed
  in 2s.

Removed APIs (gone from every Playwright the package supports, <1.61):
* test_accessibility.py in full -- Page.accessibility no longer exists.
* the two expose_binding(handle=True) tests -- the parameter is gone.
* test_glob_to_regex plus its import shim -- it pinned the old `?`/`[]` glob
  wildcards, which upstream deliberately made literals. It only ever exercised
  Playwright's private helper, never Camoufox.

Assertion drift, updated to what the current Playwright actually does:
* expect(...) failures raise AssertionError, not playwright.Error.
* editability is undefined for a <button>; use a readonly input.
* timeout wording: 'Expect "x" with timeout Nms', 'Timeout Nms exceeded'.
* traces no longer carry the Python-level `apiName`; action events record
  protocol-level class+method ("Frame.goto"). Reading the old key raised
  KeyError. 5 failed -> 11 passed.
* APIRequestContext `params` are appended to an existing query rather than
  replacing it -- assert the request is built correctly instead.
* test_network asserted "Firefox" in the UA. The bare binary advertises
  "Camoufox/<version>"; the Python package rewrites it to "Firefox/<version>"
  (verified on the wire and in navigator.userAgent). This suite drives the
  bare binary, so assert what this layer can promise.

Left failing on purpose, each reproduced identically on stock Firefox:
  test_page_clock::test_should_pause (clock resumes 1-5ms late: 1002 here,
  1005 stock), test_page_add_locator_handler::test_should_wait_for_hidden_by_default_2,
  test_navigation's empty-url popup readyState, and
  test_frame_goto_should_continue_after_client_redirect -- that last one is a
  genuine race in the networkidle accounting (a subframe's navigationCommitted
  can land after its subresource requests, and Playwright clears inflight
  bookkeeping on commit), flaky in both: 3/10 wrong here, 1/10 on stock.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-30 14:32:42 -06:00

96 lines
3.7 KiB
Python

# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import asyncio
from typing import cast
import pytest
from playwright.async_api import Error, Page, Route, expect
from tests.server import Server, TestServerRequest
async def test_should_support_timeout_option_in_route_fetch(server: Server, page: Page) -> None:
def _handler(request: TestServerRequest) -> None:
request.responseHeaders.addRawHeader("Content-Length", "4096")
request.responseHeaders.addRawHeader("Content-Type", "text/html")
request.write(b"")
server.set_route("/slow", _handler)
async def handle(route: Route) -> None:
with pytest.raises(Error) as error:
await route.fetch(timeout=1000)
assert "Timeout 1000ms exceeded" in error.value.message
await page.route("**/*", lambda route: handle(route))
with pytest.raises(Error) as error:
await page.goto(server.PREFIX + "/slow", timeout=2000)
assert "Timeout 2000ms exceeded" in error.value.message
async def test_should_not_follow_redirects_when_max_redirects_is_set_to_0_in_route_fetch(
server: Server, page: Page
) -> None:
server.set_redirect("/foo", "/empty.html")
async def handle(route: Route) -> None:
response = await route.fetch(max_redirects=0)
assert response.headers["location"] == "/empty.html"
assert response.status == 302
await route.fulfill(body="hello")
await page.route("**/*", lambda route: handle(route))
await page.goto(server.PREFIX + "/foo")
assert "hello" in await page.content()
async def test_should_intercept_with_url_override(server: Server, page: Page) -> None:
async def handle(route: Route) -> None:
response = await route.fetch(url=server.PREFIX + "/one-style.html")
await route.fulfill(response=response)
await page.route("**/*.html", lambda route: handle(route))
response = await page.goto(server.PREFIX + "/empty.html")
assert response
assert response.status == 200
assert "one-style.css" in (await response.body()).decode("utf-8")
async def test_should_intercept_with_post_data_override(server: Server, page: Page) -> None:
request_promise = asyncio.create_task(server.wait_for_request("/empty.html"))
async def handle(route: Route) -> None:
response = await route.fetch(post_data={"foo": "bar"})
await route.fulfill(response=response)
await page.route("**/*.html", lambda route: handle(route))
await page.goto(server.PREFIX + "/empty.html")
request = await request_promise
assert request.post_body
assert request.post_body.decode("utf-8") == '{"foo": "bar"}'
async def test_should_fulfill_popup_main_request_using_alias(page: Page, server: Server) -> None:
async def route_handler(route: Route) -> None:
response = await route.fetch()
await route.fulfill(response=response, body="hello")
await page.context.route("**/*", route_handler)
await page.set_content(f'<a target=_blank href="{server.EMPTY_PAGE}">click me</a>')
[popup, _] = await asyncio.gather(
page.wait_for_event("popup"), page.get_by_text("click me").click()
)
await expect(cast(Page, popup).locator("body")).to_have_text("hello")