Last active
October 24, 2022 14:59
-
-
Save trungpv1601/2ccd3cc998149a84ba80ed7a4c9ef562 to your computer and use it in GitHub Desktop.
Network throttling in Puppeteer. Read more: https://fdalvi.github.io/blog/2018-02-05-puppeteer-network-throttle/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let NETWORK_PRESETS = { | |
'GPRS': { | |
'offline': false, | |
'downloadThroughput': 50 * 1024 / 8, | |
'uploadThroughput': 20 * 1024 / 8, | |
'latency': 500 | |
}, | |
'Regular2G': { | |
'offline': false, | |
'downloadThroughput': 250 * 1024 / 8, | |
'uploadThroughput': 50 * 1024 / 8, | |
'latency': 300 | |
}, | |
'Good2G': { | |
'offline': false, | |
'downloadThroughput': 450 * 1024 / 8, | |
'uploadThroughput': 150 * 1024 / 8, | |
'latency': 150 | |
}, | |
'Regular3G': { | |
'offline': false, | |
'downloadThroughput': 750 * 1024 / 8, | |
'uploadThroughput': 250 * 1024 / 8, | |
'latency': 100 | |
}, | |
'Good3G': { | |
'offline': false, | |
'downloadThroughput': 1.5 * 1024 * 1024 / 8, | |
'uploadThroughput': 750 * 1024 / 8, | |
'latency': 40 | |
}, | |
'Regular4G': { | |
'offline': false, | |
'downloadThroughput': 4 * 1024 * 1024 / 8, | |
'uploadThroughput': 3 * 1024 * 1024 / 8, | |
'latency': 20 | |
}, | |
'DSL': { | |
'offline': false, | |
'downloadThroughput': 2 * 1024 * 1024 / 8, | |
'uploadThroughput': 1 * 1024 * 1024 / 8, | |
'latency': 5 | |
}, | |
'WiFi': { | |
'offline': false, | |
'downloadThroughput': 30 * 1024 * 1024 / 8, | |
'uploadThroughput': 15 * 1024 * 1024 / 8, | |
'latency': 2 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const puppeteer = require('puppeteer') | |
puppeteer.launch().then(async browser => { | |
// Create a new tab | |
const page = await browser.newPage() | |
// Connect to Chrome DevTools | |
const client = await page.target().createCDPSession() | |
// Set throttling property | |
await client.send('Network.emulateNetworkConditions', { | |
'offline': false, | |
'downloadThroughput': 200 * 1024 / 8, | |
'uploadThroughput': 200 * 1024 / 8, | |
'latency': 20 | |
}) | |
// Navigate and take a screenshot | |
await page.goto('https://fdalvi.github.io') | |
await page.screenshot({path: 'screenshot.png'}) | |
await browser.close() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is rad.