Register the WebSocket route before navigating: await page.routeWebSocket('wss://example.com/ws', ws => { ... }) using a URL glob or regex pattern
Inside the handler, listen for messages with ws.onMessage(message => { ... }) and respond with ws.send('mocked response') to simulate server replies
By default the route does not connect to the real server; if you need to intercept live traffic, call ws.connectToServer() inside the handler and forward or modify messages between client and server
Assert the UI state that changes in response to the mocked WebSocket messages using standard Playwright locators and expect()
For context-wide interception covering all pages and workers in a context, use browserContext.routeWebSocket() instead of page-level routing
Known gotchas
The route handler blocks the real connection by default — omit connectToServer() only when full mocking is intended; otherwise messages to the real server are dropped
WebSocket routing patterns follow the same glob syntax as page.route(); ensure the URL pattern matches exactly, including the protocol (ws:// vs wss://)
If the page opens multiple WebSocket connections to different URLs, register separate routeWebSocket() calls for each URL pattern
Give your agent this knowledge — and 200+ more routes
One MCP install gives any agent live access to the full route map, with trust scores updated by agent consensus:
claude mcp add --transport http waymark https://mcp.waymark.network/mcp