Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Getgauge Taiko Intercept

From Leeroopedia

Template:Implementation

Overview

Intercept is the primary API function for registering HTTP request interceptors in Taiko. It configures browser-level interception that can block, redirect, mock, or dynamically handle network requests matching a specified URL pattern.

Description

The intercept() function is the unified entry point for all request interception capabilities in Taiko. It registers an interceptor for a given URL pattern, and the behavior is determined by the type of the second parameter:

  • No parameter (undefined) — Block the request (fail with network error)
  • String parameter — Redirect the request to the specified URL
  • Object parameter — Return a mock response with the specified data
  • Function parameter — Invoke a handler function with full control over the request

On first invocation, intercept() enables the CDP Fetch domain with a wildcard pattern to begin intercepting all network traffic. Subsequent calls register additional interceptors without re-enabling the Fetch domain.

Usage

The intercept() function is called before navigating to a page or triggering the network activity that should be intercepted. Interceptors are persistent — once registered, they remain active until explicitly cleared with clearIntercept().

Code Reference

Source Location

  • Public API: lib/taiko.js:L284-291
  • Interceptor registration: lib/handlers/fetchHandler.js:L248-254 (addInterceptor)
  • Fetch domain enablement: lib/handlers/fetchHandler.js:L26-31 (enableFetchIntercept)

Signature

intercept(requestUrl, option, count)

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. Supports exact string matching and regular expressions.
option undefined, string, Object, or Function No Determines the interception behavior. Type controls action: undefined=block, string=redirect URL, object=mock response, function=custom handler.
count number No Number of times to intercept matching requests. After the count is reached, the interceptor is automatically removed. If omitted, the interceptor persists indefinitely.

Outputs

Return Type Description
Promise<void> Resolves when the interceptor has been registered and the Fetch domain is enabled.

Usage Examples

Basic request blocking (no action parameter):

const { openBrowser, goto, intercept, closeBrowser } = require('taiko');

(async () => {
    await openBrowser();
    await intercept('https://analytics.example.com/track');
    await goto('https://example.com');
    await closeBrowser();
})();

Request redirect (string action):

await intercept('https://api.production.com/v1/users',
                'https://api.staging.com/v1/users');
await goto('https://example.com');

Response mocking (object action):

await intercept('https://api.example.com/data', {
    status: 200,
    headers: { 'x-custom': 'value' },
    contentType: 'application/json',
    body: { items: [{ id: 1, name: 'Test Item' }] }
});
await goto('https://example.com');

Custom handler (function action):

await intercept('https://api.example.com', (req) => {
    if (req.request.method === 'POST') {
        req.respond({ status: 201, body: '{"created": true}' });
    } else {
        req.continue();
    }
});
await goto('https://example.com');

Limited interception (count parameter):

// Only intercept the first 3 matching requests
await intercept('https://api.example.com/data', {
    status: 200,
    body: '{"cached": true}'
}, 3);

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment