Emulation
CPU Performance
This feature only works with browsers supporting Chrome DevTools Protocol (CDP).
The CPU speed on mobile devices is significantly slower than on computers. Therefore, to emulate CPU speed in puppeteer, there is a method called emulateCPUThrottling.
Example: Slowing Down CPU Speed by 8x
Let's use this method to slow down CPU speed by 8 times:
it("should open yandex.ru with emulation 8x slower CPU", async function ({ browser }) {
// Get puppeteer instance
const puppeteer = await browser.getPuppeteer();
// Get the first open page (considering it to be currently active)
const [page] = await puppeteer.pages();
// Slow down the CPU speed by 8 times
await page.emulateCPUThrottling(8);
await browser.url("https://yandex.ru");
});
Network Bandwidth
This feature only works with browsers supporting Chrome DevTools Protocol (CDP).
A large number of users access services from mobile devices where internet speed can be quite slow or may even drop out intermittently. In webdriverio, we can limit network bandwidth using the throttle method, thereby testing the website's behavior under various network conditions.
Besides custom settings, the throttle method supports the following ready-made presets:
- offline | online
- GPRS
- Regular2G | Good2G
- Regular3G | Good3G
- Regular4G
- DSL
- WiFi
Example 1: Emulating a 2G Connection
Let's emulate a 2G connection and open yandex.ru in Chrome with phone emulation:
it("should open yandex.ru with emulation of 2G-connection", async function ({ browser }) {
// Emulate a 2G connection
await browser.throttle("Good2G");
await browser.url("https://yandex.ru");
});
Example 2: Emulating a Network with Given Characteristics
We can also emulate a connection with specific characteristics:
it("should open yandex.ru with emulation of custom connection", async function ({ browser }) {
// Emulate a network connection with specified characteristics
await browser.throttle({
offline: false, // emulate offline state
downloadThroughput: (10 * 1024) / 8, // max download bandwidth (byte/sec)
uploadThroughput: (10 * 1024) / 8, // max upload bandwidth (byte/sec)
latency: 10, // min latency from sending the request to receiving the response headers
});
await browser.url("https://yandex.ru");
});