From Playwright Routing to Browser Protocols: How Request Interception Works
How does Playwright intercept requests? Follow page.route() down to CDP and WebDriver BiDi, and see how to intercept requests with no framework at all.
The article explores Playwright's request interception capabilities, focusing on how it works with different browsers and the underlying protocols. In Chromium, Playwright leverages the Chrome DevTools Protocol (CDP) for request interception. CDP is a protocol used for debugging, inspection, and browser automation, with domains like Page, Runtime, Network, Performance, and Fetch.
To intercept requests in Chromium, Playwright sends a Fetch.enable command over CDP, specifying the domain to intercept and the request stage. Chromium then pauses the request and emits a Fetch.requestPaused event, including the request details. Playwright listens to this event and handles the request accordingly, either continuing, failing, or providing a custom response.
This process is specific to Chromium, as Firefox and WebKit do not use CDP. Instead, Playwright ships its own patched builds for these browsers, allowing consistent automation features across platforms. The article also demonstrates a code example for directly communicating with Chromium without using Playwright, involving starting Chromium with remote debugging enabled, requesting available debugging targets, and connecting to the target's WebSocket.
Written by urgent.news from HackerNoon's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.