Files
playwright/tests/_fake_stock_playwright.py
feder-cr 3366497f7d tests: the twins open their page before they ask Marionette for a session
The wrapper launches the engine with -silent, so there is no window at start-up, and
Marionette's WebDriver:NewSession is answered only when the browser's start-up ends, which
only a window ends. run_retail_twin and _on_ours created Chrome() before browser.new_page(),
so the session never got its answer and the socket timed out after 300 s (every retail twin
and every stock twin of the engine candidate).

Chrome now takes the page of the launch as a required argument, so the wrong order cannot be
written, and both twins open the page first. scales() reads that page.

Regression test (red on the old files, 4 of 9 fail): an ast scan of every test and script
refuses a function that asks for a session before its first new_page; the scan is itself
proved on the old order; a fake Marionette that answers NewSession only once a window exists
shows _on_ours and run_retail_twin get their answer. In the two unit rows: 4196 -> 4205 on
Linux, 4208 -> 4217 on Windows (9 tests, not OS-specific).
2026-11-29 08:59:28 +01:00

59 lines
1.9 KiB
Python

"""A stock Playwright client that launches nothing and records what ``p.firefox.launch`` was called with, for the unit tests of the stock twins (no browser, no display)."""
from __future__ import annotations
from typing import Any, Dict, List
import _stock_twin
class _Page:
pass
class _Context:
def new_page(self) -> _Page:
return _Page()
class _Browser:
def __init__(self, events: List[str]) -> None:
self._events = events
def new_context(self, **kwargs: Any) -> _Context:
return _Context()
def close(self) -> None:
self._events.append("browser closed")
class _Firefox:
def __init__(self, launches: List[Dict[str, Any]], events: List[str], fail: bool) -> None:
self._launches, self._events, self._fail = launches, events, fail
def launch(self, **kwargs: Any) -> _Browser:
self._events.append("launch")
self._launches.append(kwargs)
if self._fail:
raise RuntimeError("the launch fails")
return _Browser(self._events)
class _Playwright:
def __init__(self, launches: List[Dict[str, Any]], events: List[str], fail: bool) -> None:
self.firefox = _Firefox(launches, events, fail)
def __enter__(self) -> "_Playwright":
return self
def __exit__(self, *exc: Any) -> bool:
return False
def install(monkeypatch, events: List[str] = None, fail: bool = False) -> List[Dict[str, Any]]:
"""``_stock_twin._on_stock`` on a client that records its launches (the keywords of each) in the list returned, and its steps in ``events``."""
launches: List[Dict[str, Any]] = []
events = [] if events is None else events
monkeypatch.setattr(_stock_twin, "stock_client", lambda: (lambda: _Playwright(launches, events, fail)))
monkeypatch.setattr(_stock_twin, "Chrome", type("FakeChrome", (), {"__init__": lambda self, page: None, "close": lambda self: None}))
return launches