Created
June 3, 2019 03:48
-
-
Save aslushnikov/8fc02205222e3dcf367cfd3f470ec554 to your computer and use it in GitHub Desktop.
Using Puppeteer to emulate Network Information API
This file contains hidden or 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('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(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i think its not working!