DevTools Network, HAR, throttling and offline
The Network tab breaks a request into phases and shows which layer is slow, while a HAR is a JSON record of all the page's network activity. In Playwright, slow network is emulated for Chromium through CDP, and offline through context.setOffline(), which works in every browser. An exported HAR is checked before sharing: response bodies and tokens in URLs remain. A HAR used as a stable backend ages with the API.
Networking · Testing
On the developer’s desk everything works, but on a train the "no connection" banner never appears. How do you reproduce such a network?
A test that runs in a fast office network checks the most favourable case. Real users have slow 3G, drops, offline and long responses. The Network tab in DevTools shows where exactly the time goes, and network emulation lets you reproduce bad conditions at home and lock them into a test. This lesson covers reading Network, what a HAR is, and how to slow down, cut and record the network in Playwright.
How it works and why. Every request on the Network tab passes through phases, and a long phase points at a specific layer. So you read the breakdown first and only then decide whether the cause is in the network, the server or the page itself.
Request phases on the Timing tab
What else is in the tab.
- Filters (Fetch/XHR, WS, Doc) and search across response bodies.
- Preserve log keeps the list across redirects and navigation, Disable cache turns the cache off while DevTools is open.
- Columns Status, Protocol (
h2,h3), Initiator (who created the request), Time. - For WebSocket a separate Messages tab with frames in both directions.
- The request context menu: Copy as cURL, to repeat it in a terminal.
HAR. HTTP Archive is a JSON record of all the page’s network activity: requests, responses, timings. It is exported from DevTools ("Export HAR (sanitized)" or "Save all as HAR (sanitized)") and attached to a bug instead of a screenshot. By default Chrome exports a "sanitized" HAR without the Cookie, Set-Cookie and Authorization headers, but response bodies and tokens in URLs remain, so check the file before sharing it. Playwright can record and replay the same format.
Example: a slow network and offline in Playwright. Playwright has no built-in network throttling. For Chromium it is done through a CDP session. In the CDP protocol the Network.emulateNetworkConditions command is marked deprecated in favour of the experimental Network.emulateNetworkConditionsByRule and Network.overrideNetworkState, but it is still available. Offline is emulated by context.setOffline(), which works in every browser.
const cdp = await context.newCDPSession(page);
await cdp.send('Network.enable');
await cdp.send('Network.emulateNetworkConditions', {
offline: false,
latency: 400, // ms
downloadThroughput: (500 * 1024) / 8, // bytes/s
uploadThroughput: (250 * 1024) / 8,
});
await page.goto('/dashboard');
await expect(page.getByTestId('spinner')).toBeVisible();
// offline: the banner, the queue of pending actions, recovery
await context.setOffline(true);
await page.getByRole('button', { name: 'Save' }).click();
await expect(page.getByText('No connection')).toBeVisible();
await context.setOffline(false);
await expect(page.getByText('Saved')).toBeVisible();
Two ways to slow down
page.route with a delay
Slows one request, works in every browser, but it is not the network: the handler simply waits.
CDP throttling
Slows the whole page like DevTools, but only in Chromium and through a protocol that changes.
Individual requests are slowed in page.route: a delay before route.continue(). More realistic is at the OS or proxy level: Network Link Conditioner (macOS), tc netem (Linux), Toxiproxy as a proxy with latency and drop "toxics". On such a network you check not just "it works" but the spinner, timeouts, the error message, retry and recovery without duplicated actions.
Example: a HAR as a stable backend for a UI test. The file is recorded once on a real environment, then UI tests replay the answers from the file. Tests are fast and independent of data, but the file ages along with the API.
// recording: the HAR is written when the context closes
const context = await browser.newContext({
recordHar: { path: 'hars/orders.har', urlFilter: '**/api/**' },
});
// ... the scenario ...
await context.close();
// replay without the real backend
await page.routeFromHAR('hars/orders.har', {
url: '**/api/**',
notFound: 'abort', // an unknown request fails at once
});
Limits and alternatives. Browser throttling changes speed but not the behaviour of a real network: no packet loss, no Wi-Fi to mobile switch, no heavy jitter. A HAR holds one snapshot, so it will not show a change in the API schema: you still need a few e2e tests without mocks. For mobile apps and non-browser clients these tools do not fit: use a proxy or a device network emulator.
Typical mistakes.
- Measuring speed with DevTools open and Disable cache on, then drawing conclusions about users.
- Forgetting
await context.close()so the HAR is not written. - Handing a HAR to a colleague or vendor without checking response bodies and tokens in URLs.
- Leaving CDP throttling in a test for Firefox or WebKit:
newCDPSessiondoes not work there. - Checking only that the banner appeared and not recovery after the network returns.
Check yourself
On the Timing tab the Waiting (TTFB) phase is long. What does it mean and where do you look next?
Answer
The client sent the request and waits long for the first byte: a slow server, backend or database. Next look at server logs and traces and check whether the delay is stable. A long DNS or SSL would point to the network and a new connection.
How does context.setOffline(true) differ from CDP throttling?
Answer
setOffline switches the context’s network off entirely and works in every browser. CDP throttling keeps the network but limits latency and throughput, and works only in Chromium.
Is it safe to hand a HAR from a bug report to outsiders?
Answer
Not without a check. The DevTools export strips Cookie, Set-Cookie and Authorization by default, but response bodies and tokens in URLs remain, so the file is reviewed and cleaned.