Block unwanted requests
Abort images, fonts, and third-party trackers so a run loads only what the test needs.
Analytics beacons, ad scripts, and large images slow a run and introduce failures unrelated to the behavior under test. Abort them at the route layer so the browser never fetches them.
Abort by resource type
Register a route, inspect the request, and abort the types you do not need. abort() fails the request as if the network dropped it.
use Playwright\Network\RouteInterface;
$page->route('**/*', static function (RouteInterface $route): void {
$type = $route->request()->resourceType();
if (in_array($type, ['image', 'font', 'media'], true)) {
$route->abort();
return;
}
$route->continue();
});
$page->goto('https://app.example.test/dashboard');
A catch-all **/* route must call continue() for everything it does not abort. Miss that and the whole page hangs, because every request waits for a handler that never answers.
Abort by host
To drop a known tracker, match its domain instead of every request:
use Playwright\Network\RouteInterface;
$page->route('**/*.png', static function (RouteInterface $route): void {
$route->abort();
});
$page->route('https://analytics.example.com/**', static function (RouteInterface $route): void {
$route->abort();
});
A narrow pattern only fires for matching URLs, so it needs no continue() branch. Prefer several narrow routes over one broad handler with a filter.
Register before you navigate
Set every block route up before goto(). A route added after navigation starts can miss the first wave of requests the page already sent.
Expected result
The page should still reach the user-visible state your test needs, while blocked requests appear as aborted in network diagnostics or traces.
If the page hangs, the route is probably too broad and did not call continue() for allowed requests.
Keep it honest
Blocking is for noise, not for the feature under test. If the page genuinely depends on an image or a script to work, aborting it tests a state your users never see.
Go next
- Run the example: a narrow PNG-only route you can execute locally
- Network and API testing: abort, continue, and modify requests
- Mock an API response: fulfill a request instead of dropping it