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 EmulateNetwork

From Leeroopedia

Template:Implementation

Overview

EmulateNetwork is the Taiko API function for simulating various network conditions such as slow connections, offline mode, and bandwidth throttling during browser automation.

Description

The emulateNetwork() function configures the browser's network emulation using the CDP Network.emulateNetworkConditions API. It accepts either a string preset name for common network types or a custom configuration object specifying exact throughput and latency values.

When network emulation is active, all network traffic from the browser is throttled according to the specified conditions. This affects page loads, API requests, resource fetching, and all other network activity.

Available presets provide realistic configurations for common network types:

  • GPRS — Very slow mobile data (6.4 KB/s down, 2.56 KB/s up, 500ms latency)
  • Regular2G — Basic 2G connection (31.25 KB/s down, 6.4 KB/s up, 300ms latency)
  • Good2G — Improved 2G connection (56.25 KB/s down, 18.75 KB/s up, 150ms latency)
  • Regular3G — Standard 3G connection (96 KB/s down, 31.25 KB/s up, 100ms latency)
  • Good3G — Fast 3G connection (192 KB/s down, 96 KB/s up, 150ms latency)
  • Regular4G — Standard 4G/LTE connection (512 KB/s down, 512 KB/s up, 170ms latency)
  • DSL — Wired DSL connection (256 KB/s down, 128 KB/s up, 5ms latency)
  • WiFi — Standard WiFi connection (3.84 MB/s down, 1.92 MB/s up, 2ms latency)
  • Offline — Complete network disconnection

Usage

Network emulation is used to test application behavior under various network conditions. It should be set before navigating to the page or triggering the network activity that should be affected. To restore normal network conditions, call emulateNetwork with a fast preset like 'WiFi' or re-open the browser.

Code Reference

Source Location

  • Public API: lib/taiko.js:L314-321
  • Network emulation logic: lib/handlers/networkHandler.js:L60-82 (setNetworkEmulation)

The relevant code paths:

// Public API in taiko.js
module.exports.emulateNetwork = async (networkType) => {
    await networkHandler.setNetworkEmulation(networkType);
};

// setNetworkEmulation in networkHandler.js
const networkPresets = {
    'GPRS':       { offline: false, downloadThroughput: 6400,    uploadThroughput: 2560,    latency: 500 },
    'Regular2G':  { offline: false, downloadThroughput: 31250,   uploadThroughput: 6400,    latency: 300 },
    'Good2G':     { offline: false, downloadThroughput: 56250,   uploadThroughput: 18750,   latency: 150 },
    'Regular3G':  { offline: false, downloadThroughput: 96000,   uploadThroughput: 31250,   latency: 100 },
    'Good3G':     { offline: false, downloadThroughput: 196608,  uploadThroughput: 96000,   latency: 150 },
    'Regular4G':  { offline: false, downloadThroughput: 524288,  uploadThroughput: 524288,  latency: 170 },
    'DSL':        { offline: false, downloadThroughput: 262144,  uploadThroughput: 131072,  latency: 5   },
    'WiFi':       { offline: false, downloadThroughput: 3932160, uploadThroughput: 1966080, latency: 2   },
    'Offline':    { offline: true,  downloadThroughput: 0,       uploadThroughput: 0,       latency: 0   }
};

async function setNetworkEmulation(networkType) {
    const conditions = typeof networkType === 'string'
        ? networkPresets[networkType]
        : networkType;
    await network.emulateNetworkConditions(conditions);
}

Signature

emulateNetwork(networkType)

Import

const { emulateNetwork } = require('taiko');

I/O Contract

Inputs

Parameter Type Required Description
networkType string or Object Yes Either a preset name string or a custom configuration object.

When string: Must be one of the preset names: 'GPRS', 'Regular2G', 'Good2G', 'Regular3G', 'Good3G', 'Regular4G', 'DSL', 'WiFi', 'Offline'.

When object: Custom network conditions with the following properties:

Property Type Description
offline boolean Whether to simulate offline mode. When true, all requests fail with a network error.
downloadThroughput number Maximum download speed in bytes per second. Use -1 for no throttling.
uploadThroughput number Maximum upload speed in bytes per second. Use -1 for no throttling.
latency number Additional latency in milliseconds added to each request.

Outputs

Return Type Description
Promise<void> Resolves when the network emulation conditions have been applied to the browser.

Side Effect: All subsequent network traffic from the browser is throttled according to the specified conditions until new conditions are applied or the browser is closed.

Usage Examples

Simulate offline mode:

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

(async () => {
    await openBrowser();
    await goto('https://example.com');

    // Go offline to test service worker / offline functionality
    await emulateNetwork('Offline');

    // Trigger actions that should work offline
    // Assert offline UI is displayed

    await closeBrowser();
})();

Test with slow 3G connection:

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

(async () => {
    await openBrowser();

    // Set slow 3G before navigating
    await emulateNetwork('Good3G');

    await goto('https://example.com');
    // Assert loading indicators are displayed
    // Verify page loads within acceptable timeframe

    await closeBrowser();
})();

Use custom network conditions:

// Simulate a very specific network condition
await emulateNetwork({
    offline: false,
    downloadThroughput: 6400,     // ~6.4 KB/s download
    uploadThroughput: 2560,       // ~2.5 KB/s upload
    latency: 500                  // 500ms added latency
});
await goto('https://example.com');

Test network transition (online to offline and back):

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

(async () => {
    await openBrowser();

    // Start with normal connection
    await emulateNetwork('WiFi');
    await goto('https://example.com');

    // Go offline
    await emulateNetwork('Offline');
    // Assert offline behavior...

    // Come back online
    await emulateNetwork('WiFi');
    // Assert recovery behavior...

    await closeBrowser();
})();

Simulate GPRS for extreme slow network testing:

await emulateNetwork('GPRS');
await goto('https://example.com');
// Verify that the application handles extremely slow loading
// Check that timeout handling works correctly
// Ensure progressive loading or skeleton screens appear

Related Pages

Implements Principle

Requires Environment

Page Connections

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