Skip to content

Instantly share code, notes, and snippets.

@aslushnikov
Created June 3, 2019 03:48
Show Gist options
  • Save aslushnikov/8fc02205222e3dcf367cfd3f470ec554 to your computer and use it in GitHub Desktop.
Save aslushnikov/8fc02205222e3dcf367cfd3f470ec554 to your computer and use it in GitHub Desktop.
Using Puppeteer to emulate Network Information API
const puppeteer = require('puppeteeer');
const networkConditions = {
'2g': {
downloadThroughput: 500 * 1024 / 8 * .8,
uploadThroughput: 500 * 1024 / 8 * .8,
latency: 400 * 5,
offline: false,
},
'3g': {
downloadThroughput: 1.6 * 1024 * 1024 / 8 * .9,
uploadThroughput: 750 * 1024 / 8 * .9,
latency: 150 * 3.75,
offline: false,
},
'offline': {
downloadThroughput: 0,
uploadThroughput: 0,
latency: 0,
offline: true,
},
'no throttling': {
downloadThroughput: -1,
uploadThroughput: -1,
latency: 0,
offline: false,
},
};
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Use raw protocol to configure network conditions.
const session = await page.target().createCDPSession();
await session.send('Network.emulateNetworkConditions', networkConditions['3g']);
await session.detach();
// Keep using Puppeteer API to drive automation.
await page.goto('https://example.com');
// Fetch connection effective type.
console.log(await page.evaluate(() => navigator.connection.effectiveType));
await browser.close();
})();
@ishafiul
Copy link

i think its not working!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment