Home  Puppeteer   How to simu ...

How to simulate slow network speed in puppeteer

To simulate network throttling in Puppeteer, you can use the setOfflineMode and setRequestInterception methods provided by the Puppeteer Page class. Here’s how you can simulate different network conditions like slow 3G:

  1. Simulating Network Throttling (Slow 3G):
const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  // Enable request interception
  await page.setRequestInterception(true);

  // Intercept requests and simulate slow 3G
  page.on('request', (request) => {
    const headers = request.headers();
    headers['Connection'] = 'close'; // Close connection to simulate slow network
    request.continue({ headers });
  });

  // Navigate to a website
  await page.goto('https://example.com', { waitUntil: 'domcontentloaded' });

  // Your Puppeteer script continues here

  await browser.close();
})();

Explanation:

Adjusting Network Conditions:

To simulate different network conditions, you can modify the headers in the page.on('request', ...) handler:

Published on: Jun 28, 2024, 12:26 AM  
 

Comments

Add your comment