Skip to main content

Network

Playwright provides APIs to monitor and modify network traffic, both HTTP and HTTPS. Any requests that page does, including XHRs and fetch requests, can be tracked, modified and handled.


HTTP Authentication

BrowserContext context = browser.newContext(new Browser.NewContextOptions()
.setHttpCredentials("bill", "pa55w0rd"));
Page page = context.newPage();
page.navigate("https://example.com");

API reference

HTTP Proxy

You can configure pages to load over the HTTP(S) proxy or SOCKSv5. Proxy can be either set globally for the entire browser, or for each browser context individually.

You can optionally specify username and password for HTTP(S) proxy, you can also specify hosts to bypass proxy for.

Here is an example of a global proxy:

Browser browser = chromium.launch(new BrowserType.LaunchOptions()
.setProxy(new Proxy("http://myproxy.com:3128")
.setUsername('usr')
.setPassword('pwd'));

When specifying proxy for each context individually, Chromium on Windows needs a hint that proxy will be set. This is done via passing a non-empty proxy server to the browser itself. Here is an example of a context-specific proxy:

Browser browser = chromium.launch(new BrowserType.LaunchOptions()
// Browser proxy option is required for Chromium on Windows.
.setProxy(new Proxy("per-context"));
BrowserContext context = chromium.launch(new Browser.NewContextOptions()
.setProxy(new Proxy("http://myproxy.com:3128"));

Network events

You can monitor all the requests and responses:

import com.microsoft.playwright.*;

public class Example {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
BrowserType chromium = playwright.chromium();
Browser browser = chromium.launch();
Page page = browser.newPage();
page.onRequest(request -> System.out.println(">> " + request.method() + " " + request.url()));
page.onResponse(response -> System.out.println("<<" + response.status() + " " + response.url()));
page.navigate("https://example.com");
browser.close();
}
}
}

Or wait for a network response after the button click:

// Use a glob URL pattern
Response response = page.waitForResponse("**/api/fetch_data", () -> {
page.locator("button#update").click();
});

Variations

// Use a RegExp
Response response = page.waitForResponse(Pattern.compile("\\.jpeg$"), () -> {
page.locator("button#update").click();
});

// Use a predicate taking a Response object
Response response = page.waitForResponse(r -> r.url().contains(token), () -> {
page.locator("button#update").click();
});

API reference


Handle requests

page.route("**/api/fetch_data", route -> route.fulfill(new Route.FulfillOptions()
.setStatus(200)
.setBody(testData)));
page.navigate("https://example.com");

You can mock API endpoints via handling the network requests in your Playwright script.

Variations

browserContext.route("**/api/login", route -> route.fulfill(new Route.FulfillOptions()
.setStatus(200)
.setBody("accept")));
page.navigate("https://example.com");

API reference


Modify requests

// Delete header
page.route("**/*", route -> {
Map<String, String> headers = new HashMap<>(route.request().headers());
headers.remove("X-Secret");
route.resume(new Route.ResumeOptions().setHeaders(headers));
});

// Continue requests as POST.
page.route("**/*", route -> route.resume(new Route.ResumeOptions().setMethod("POST")));

You can continue requests with modifications. Example above removes an HTTP header from the outgoing requests.

Abort requests

page.route("**/*.{png,jpg,jpeg}", route -> route.abort());

// Abort based on the request type
page.route("**/*", route -> {
if ("image".equals(route.request().resourceType()))
route.abort();
else
route.resume();
});

API reference


Modify responses

To modify a response use APIRequestContext to get original response and then pass the response to Route.fulfill([options]). You can override individual fields on the response via options:

page.route("**/title.html", route -> {
// Fetch original response.
APIResponse response = page.request().fetch(route.request());
// Add a prefix to the title.
String body = response.text();
body = body.replace("<title>", "<title>My prefix:");
Map<String, String> headers = response.headers();
headers.put("content-type": "text/html");
route.fulfill(new Route.FulfillOptions()
// Pass all fields from the response.
.setResponse(response)
// Override response body.
.setBody(body)
// Force content type to be html.
.setHeaders(headers));
});

API reference


Record and replay requests

You can record network activity as an HTTP Archive file (HAR). Later on, this archive can be used to mock responses to the network requests. You'll need to:

  1. Record a HAR file.
  2. Commit the HAR file alongside the tests.
  3. Route requests using the saved HAR files in the tests.

Recording HAR with CLI

Open the browser with Playwright CLI and pass --save-har option to produce a HAR file. Optionally, use --save-har-glob to only save requests you are interested in, for example API endpoints. If the har file name ends with .zip, artifacts are written as separate files and are all compressed into a single zip.

# Save API requests from example.com as "example.har" archive.
mvn exec:java -e -Dexec.mainClass=com.microsoft.playwright.CLI -Dexec.args="open --save-har=example.har --save-har-glob='**/api/**' https://example.com"

Recording HAR with a script

Alternatively, instead of using the CLI, you can record HAR programmatically. Pass har option when creating a BrowserContext with Browser.newContext([options]) to create an archive. If the har file name ends with .zip, artifacts are written as separate files and are all compressed into a single zip.

BrowserContext context = browser.newContext(new Browser.NewContextOptions()
.setRecordHarPath(Paths.get("example.har"))
.setRecordHarUrlFilter("**/api/**"));

// ... Perform actions ...

// Close context to ensure HAR is saved to disk.
context.close();

Replaying from HAR

Use Page.routeFromHAR(har[, options]) or BrowserContext.routeFromHAR(har[, options]) to serve matching responses from the HAR file.

// Either use a matching response from the HAR,
// or abort the request if nothing matches.
page.routeFromHAR(Paths.get("example.har"));

HAR replay matches URL and HTTP method strictly. For POST requests, it also matches POST payloads strictly. If multiple recordings match a request, the one with the most matching headers is picked. An entry resulting in a redirect will be followed automatically.

Similar to when recording, if given HAR file name ends with .zip, it is considered an archive containing the HAR file along with network payloads stored as separate entries. You can also extract this archive, edit payloads or HAR log manually and point to the extracted har file. All the payloads will be resolved relative to the extracted har file on the file system.


WebSockets

Playwright supports WebSockets inspection out of the box. Every time WebSocket is created, Page.onWebSocket(handler) event is fired. This event contains the WebSocket instance for further web socket frames inspection:

page.onWebSocket(ws -> {
log("WebSocket opened: " + ws.url());
ws.onFrameSent(frameData -> log(frameData.text()));
ws.onFrameReceived(frameData -> log(frameData.text()));
ws.onClose(ws1 -> log("WebSocket closed"));
});

API reference


Missing Network Events and Service Workers

Playwright's built-in BrowserContext.route(url, handler[, options]) and Page.route(url, handler[, options]) allow your tests to natively route requests and perform mocking and interception.

  1. If you're using Playwright's native BrowserContext.route(url, handler[, options]) and Page.route(url, handler[, options]), and it appears network events are missing, disable Service Workers by setting Browser.newContext.serviceWorkers to 'block'.
  2. It might be that you are using a mock tool such as Mock Service Worker (MSW). While this tool works out of the box for mocking responses, it adds its own Service Worker that takes over the network requests, hence making them invisible to BrowserContext.route(url, handler[, options]) and Page.route(url, handler[, options]). If you are interested in both network testing and mocking, consider using built-in BrowserContext.route(url, handler[, options]) and Page.route(url, handler[, options]) for response mocking.
  3. If you're interested in not solely using Service Workers for testing and network mocking, but in routing and listening for requests made by Service Workers themselves, please see this experimental feature.