最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Puppeteer: is there a way to access the DevTools Network API? - Stack Overflow

programmeradmin5浏览0评论

I am trying to use Puppeteer for end-to-end tests. These tests require accessing the network emulation capabilities of DevTools (e.g. to simulate offline browsing).

So far I am using chrome-remote-interface, but it is too low-level for my taste.

As far as I know, Puppeteer does not expose the network DevTools features (emulateNetworkConditions in the DevTools protocol).

Is there an escape hatch in Puppeteer to access those features, e.g. a way to execute a Javascript snippet in a context in which the DevTools API is accessible?

Thanks

Edit: OK, so it seems that I can work around the lack of an API using something like this:

    const client = page._client;
    const res = await client.send('Network.emulateNetworkConditions',
      { offline: true, latency: 40, downloadThroughput: 40*1024*1024, 
      uploadThroughput: 40*1024*1024 });

But I suppose it is Bad Form and may slip under my feet at any time?

I am trying to use Puppeteer for end-to-end tests. These tests require accessing the network emulation capabilities of DevTools (e.g. to simulate offline browsing).

So far I am using chrome-remote-interface, but it is too low-level for my taste.

As far as I know, Puppeteer does not expose the network DevTools features (emulateNetworkConditions in the DevTools protocol).

Is there an escape hatch in Puppeteer to access those features, e.g. a way to execute a Javascript snippet in a context in which the DevTools API is accessible?

Thanks

Edit: OK, so it seems that I can work around the lack of an API using something like this:

    const client = page._client;
    const res = await client.send('Network.emulateNetworkConditions',
      { offline: true, latency: 40, downloadThroughput: 40*1024*1024, 
      uploadThroughput: 40*1024*1024 });

But I suppose it is Bad Form and may slip under my feet at any time?

Share Improve this question edited Sep 6, 2017 at 14:22 Rom1 asked Sep 6, 2017 at 9:57 Rom1Rom1 3,2072 gold badges24 silver badges39 bronze badges 1
  • Not any time, I guess, but only when the underlying API is changed in an unexpected way. – woxxom Commented Sep 6, 2017 at 16:27
Add a ment  | 

1 Answer 1

Reset to default 9

Update: headless Chrome now supports network throttling!

In Puppeteer, you can emulate devices (https://github./GoogleChrome/puppeteer/blob/master/docs/api.md#pageemulateoptions) but not network conditions. It's something we're considering, but headless Chrome needs to support network throttling first.

To emulate a device, I'd use the predefined devices found in 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.');
  // other actions...
  browser.close();
});
发布评论

评论列表(0)

  1. 暂无评论