Bridge browser WebSockets
Set webSocketUrls to route the page's own sockets to Decoy (Playwright 1.48 or later). The page
needs no changes:
playwright.config.ts (use excerpt)
- Each matching page socket is bridged to Decoy: the fixture opens its own socket to Decoy at the
same path and query (
ws://, orwss://for anhttpsurl) withx-mock-sessionset to the test's Session. Text and binary frames pass both ways; when either side closes, the other is closed too, with the same code and reason where the protocol allows it. When the test ends, the fixture closes its sockets to Decoy that are still open. - The real server is never contacted. If Decoy can't be reached, the page socket is closed.
- A socket keeps the Session it opened with; see WebSocket for Routes and Behaviors.
To push a message from the test, wait for the page's socket and send to it:
tests/live.spec.ts (excerpt)
decoy.connection(route, { timeout })resolves once this test's Session has a live connection on the Route (Control APIwait). It rejects on timeout (Decoy's default whentimeoutis unset), for an unknown Route, or for a Route whose transport has no connections (HTTP, unary gRPC)..send(message)pushesmessageto every live connection of the Route in this Session, skipping Cases (Control APIsend). Other tests' sockets never get it.- It resolves while any of the Session's sockets on the Route is live, including one that is
closing. After a reload, wait for a page signal that the new socket is up before you
send.