You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
By default, Playwright only has access to the network traffic made by the browser.
562
+
To mock and intercept traffic made by the application server, use Playwright's **experimental** mocking proxy. Note this feature is **experimental** and subject to change.
563
+
564
+
The mocking proxy is a HTTP proxy server that's connected to the currently running test.
565
+
If you send it a request, it will apply the network routes configured via `page.route` and `context.route`, reusing your existing browser routes.
566
+
567
+
To get started, enable the `mockingProxy` option in your Playwright config:
568
+
569
+
```ts
570
+
exportdefaultdefineConfig({
571
+
use: { mockingProxy: true }
572
+
});
573
+
```
574
+
575
+
Playwright will now inject the proxy URL into all browser requests under the `x-playwright-proxy` header.
576
+
On your server, read the URL in this header and prepend it to all outgoing traffic you want to intercept:
577
+
578
+
```js
579
+
constheaders=getCurrentRequestHeaders(); // this looks different for each application
Prepending the URL will direct the request through the proxy. You can now intercept it with `context.route` and `page.route`, just like browser requests:
setGlobalDispatcher(proxyingDispatcher); // this will also apply to global fetch
638
+
```
639
+
640
+
:::note
641
+
Note that this style of proxying, where the proxy URL is prended to the request URL, does *not* use [`CONNECT`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT), which is the common way of establishing a proxy connection.
642
+
This is because for HTTPS requests, a `CONNECT` proxy does not have access to the proxied traffic. That's great behaviour for a production proxy, but counteracts network interception!
643
+
:::
644
+
645
+
646
+
### Recipes
647
+
* langs: js
648
+
649
+
#### Next.js
650
+
* langs: js
651
+
652
+
Monkey-patch `globalThis.fetch` in your `instrumentation.ts` file:
0 commit comments