Implementation:Getgauge Taiko Intercept Block
Overview
Intercept Block is the request blocking behavior of Taiko's intercept() function, which prevents matching HTTP requests from completing by failing them with a network error.
Description
When intercept() is called with only a URL pattern and no action parameter, it registers a blocking interceptor. Any matching request is immediately failed using the CDP Fetch.failRequest method with the error reason "Failed". The request never reaches the network — the browser receives an immediate failure response.
This is the default and simplest interception behavior. It is triggered when the action property of the registered interceptor is falsy (undefined, null, or empty).
Usage
Request blocking is used to prevent specific URLs from loading during tests. Common targets include analytics endpoints, advertisement networks, third-party tracking scripts, and heavy resources that are not relevant to the test scenario.
Code Reference
Source Location
- Blocking logic:
lib/handlers/fetchHandler.js:L86-90(falsy action branch inhandleInterceptor)
The relevant code path in handleInterceptor:
// When action is falsy (undefined), fail the request
if (!action) {
fetch.failRequest({ requestId: event.requestId, reason: 'Failed' });
return;
}
Signature
intercept(requestUrl)
Import
const { intercept } = require('taiko');
I/O Contract
Inputs
| Parameter | Type | Required | Description |
|---|---|---|---|
requestUrl |
string or RegExp |
Yes | URL pattern to match against outgoing requests. All matching requests will be blocked. |
Outputs
| Return Type | Description |
|---|---|
Promise<void> |
Resolves when the blocking interceptor has been registered. |
Side Effect: Matching requests receive a Fetch.failRequest with reason "Failed", causing the browser to treat the request as a network failure.
Usage Examples
Block analytics requests:
const { openBrowser, goto, intercept, closeBrowser } = require('taiko');
(async () => {
await openBrowser();
// Block analytics - no second parameter means block
await intercept('https://analytics.example.com/track');
await intercept('https://www.google-analytics.com/analytics.js');
await goto('https://example.com');
await closeBrowser();
})();
Block third-party trackers:
await intercept('https://tracker.example.com');
await intercept('https://pixel.facebook.com');
await intercept('https://ads.doubleclick.net');
await goto('https://example.com');
Block heavy resources to speed up tests:
// Block video and large image resources
await intercept('https://cdn.example.com/videos');
await intercept('https://cdn.example.com/hero-image.jpg');
await goto('https://example.com');
Block with regex pattern:
// Block all requests to any subdomain of ads.example.com
await intercept(/https:\/\/.*\.ads\.example\.com\/.*/);
await goto('https://example.com');