Simulate a slow network and packet loss in Playwright tests
Playwright can take a page offline, and slow Chromium through DevTools. The beannetworktester package on npm slows the real network instead, below the browser: latency, packet loss, a speed limit or a whole outage, for the server your test talks to. Chromium, Firefox and WebKit are slowed the same way, WebSockets included. The package carries Bean Network Tester's own program and runs it for the length of a test, on Windows and Linux. It is published with every stable release from 1.1.0 on.
Install it
npm install -D @playwright/test beannetworktester
npx playwright install chromium
It needs Windows x64 or Linux x64 (glibc 2.39 or newer, such as Ubuntu 24.04) and Node 20 or newer. npm installs the program for your system as an optional dependency, so do not leave optional dependencies out.
Changing the network needs administrator rights on Windows: open the terminal with Run as administrator. On Linux it needs root, or a user who may run sudo without a password, and then only the program runs under sudo. GitHub's hosted Windows runners run elevated and its Ubuntu runners allow sudo, so a workflow needs no extra step.
A test on a slow network
Save this as tests/slow.spec.js and run npx playwright test:
import http from 'node:http';
import { test, expect } from 'beannetworktester/playwright';
let server;
test.beforeAll(async () => {
server = http.createServer((request, response) => response.end('<h1>Hello</h1>'));
await new Promise((done) => server.listen(8000, done));
});
test.afterAll(() => server.close());
test('a page takes longer on a slow network', async ({ page, badNetwork }) => {
await badNetwork({ server: 'http://localhost:8000', latency: 500 });
const started = Date.now();
await page.goto('http://localhost:8000');
await expect(page.locator('h1')).toHaveText('Hello');
expect(Date.now() - started).toBeGreaterThanOrEqual(500);
});
The test passes because the page really arrives later: every packet to and from that server waits 500 ms. When the test ends, the network is back to normal. Measured on Windows with latency: 200: a request to the server in the aim went from 0.8 ms to 404 ms, because the request and the answer both wait, and a page in Chromium from 13 ms to 416 ms. Another local server, outside the aim, answered in 1.6 ms meanwhile.
No rights at hand? badNetwork({ simulate: true, latency: 500 }) runs the program on made-up traffic and changes nothing on the network. It checks that the package is set up, and slows nothing.
The badNetwork fixture
Import test and expect from beannetworktester/playwright instead of @playwright/test. badNetwork starts a session when you call it and resolves once the network is slow. The session stops when the test ends, whether it passed, failed or crashed, and the program's summary is attached to the test's report as bad-network. To change the conditions halfway, stop the first session before you start the next:
test('the app recovers when the API comes back', async ({ page, badNetwork }) => {
const outage = await badNetwork({ server: 'http://localhost:8000', loss: 100 });
await page.goto('http://localhost:3000');
await expect(page.getByText('Offline')).toBeVisible();
await outage.stop();
await expect(page.getByText('Offline')).toBeHidden();
});
Outside Playwright Test, badNetwork from require('beannetworktester') starts the same session: const session = await badNetwork({ ... }), and await session.stop() ends it and returns the summary.
What a session aims at
Every session aims at something, and only that traffic changes. A session without an aim would slow every connection of the machine, the test runner's own included, so it does not start.
server: 'http://localhost:3000'slows every connection to that address and port, from the browser, from your API server or from any other program.'db.local:5432'and a list of servers work too. This is the first choice, and it is exact on Windows and Linux.process: 'self'slows the test worker and every program it started, such as its browser. A process your test started itself is given as itsChildProcess.dstPort,dstIpandtargetare the program's own aims, written in its syntax, for an API on a known port or address. They reach everything that matches:dstPort: 443is every HTTPS connection of the machine, your editor's included, andtarget: 'node'every program with "node" in its name.
Aims given together narrow each other: server with process: 'self' slows only this worker's connections to that server. On Linux, process and target reach a new connection up to about 0.2 seconds after it opens, so aim at the server when the first request counts.
What a session can do
Every setting is one of the program's command-line options. latency: 300 adds 300 ms to every packet, jitter: 100 varies that delay, loss: 5 drops 5 % of the packets, down and up limit the speed in KB/s, and loss: 100 cuts the server off. preset: 'presets.3g' is a whole link profile, and seed: 42 makes the same random choices on every run. A scenario changes the conditions as the seconds pass: scenario: 'scenarios/cafe-wifi.json'. A scenario step may change how the network behaves, but not what the session aims at.
Parallel workers and limits
- One at a time. Two sessions over the same traffic would add up, so parallel workers take turns, each waiting up to
waitseconds (600 unless you give another). A second session in the same test is refused at once. - It ends with the test. If the test worker dies, the program sees it and stops at once.
duration(300 seconds unless you give another) is the most a session may run in any case. - It says when it did nothing. A session that slowed nothing, because nothing reached its aim, ends with a
BadNetworkWarning. The usual cause is an app that talks to another address than the aim, such as a container. - macOS. The program runs on Windows and Linux. On a Mac, run the tests in a Linux x86_64 container or virtual machine, or on a CI runner.
The package's README on GitHub lists every option and what each message means.