Puppeteer:有没有办法访问 DevTools 网络 API?
Puppeteer: is there a way to access the DevTools Network API?
我正在尝试使用 Puppeteer 进行端到端测试。这些测试需要访问 DevTools 的网络模拟功能(例如模拟离线浏览)。
到目前为止,我正在使用 chrome-remote-interface,但它对我来说太低级了。
据我所知,Puppeteer 不公开网络 DevTools 功能(emulateNetworkConditions
在 DevTools 协议中)。
Puppeteer 中是否有逃生通道来访问这些功能,例如在 DevTools API 可访问的上下文中执行 Javascript 片段的方法?
谢谢
编辑:
好的,看来我可以使用如下方法解决缺少 API 的问题:
const client = page._client;
const res = await client.send('Network.emulateNetworkConditions',
{ offline: true, latency: 40, downloadThroughput: 40*1024*1024,
uploadThroughput: 40*1024*1024 });
但我想它是 Bad Form,随时可能从我脚下滑落?
更新:headless Chrome 现在支持网络节流!
在 Puppeteer 中,您可以模拟设备 (https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pageemulateoptions) 但不能模拟网络状况。这是我们正在考虑的事情,但 headless Chrome 需要首先支持网络节流。
为了模拟设备,我会使用在 DeviceDescriptors
:
中找到的预定义设备
const puppeteer = require('puppeteer');
const devices = require('puppeteer/DeviceDescriptors');
const iPhone = devices['iPhone 6'];
puppeteer.launch().then(async browser => {
const page = await browser.newPage();
await page.emulate(iPhone);
await page.goto('https://www.google.com');
// other actions...
browser.close();
});
我正在尝试使用 Puppeteer 进行端到端测试。这些测试需要访问 DevTools 的网络模拟功能(例如模拟离线浏览)。
到目前为止,我正在使用 chrome-remote-interface,但它对我来说太低级了。
据我所知,Puppeteer 不公开网络 DevTools 功能(emulateNetworkConditions
在 DevTools 协议中)。
Puppeteer 中是否有逃生通道来访问这些功能,例如在 DevTools API 可访问的上下文中执行 Javascript 片段的方法?
谢谢
编辑: 好的,看来我可以使用如下方法解决缺少 API 的问题:
const client = page._client;
const res = await client.send('Network.emulateNetworkConditions',
{ offline: true, latency: 40, downloadThroughput: 40*1024*1024,
uploadThroughput: 40*1024*1024 });
但我想它是 Bad Form,随时可能从我脚下滑落?
更新:headless Chrome 现在支持网络节流!
在 Puppeteer 中,您可以模拟设备 (https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pageemulateoptions) 但不能模拟网络状况。这是我们正在考虑的事情,但 headless Chrome 需要首先支持网络节流。
为了模拟设备,我会使用在 DeviceDescriptors
:
const puppeteer = require('puppeteer');
const devices = require('puppeteer/DeviceDescriptors');
const iPhone = devices['iPhone 6'];
puppeteer.launch().then(async browser => {
const page = await browser.newPage();
await page.emulate(iPhone);
await page.goto('https://www.google.com');
// other actions...
browser.close();
});