【问题标题】:Puppeteer: is there a way to access the DevTools Network API?Puppeteer:有没有办法访问 DevTools Network API?
【发布时间】:2017-09-06 09:57:07
【问题描述】:

我正在尝试使用Puppeteer 进行端到端测试。这些测试需要访问 DevTools 的网络仿真功能(例如模拟离线浏览)。

到目前为止,我使用的是 chrome-remote-interface,但它对我的口味来说太低级了。

据我所知,Puppeteer 没有公开网络 DevTools 功能(DevTools 协议中的emulateNetworkConditions)。

Puppeteer 中是否有一个逃生舱来访问这些功能,例如一种在可以访问 DevTools API 的上下文中执行 Javascript sn-p 的方法?

谢谢

编辑: 好的,看来我可以使用这样的方法来解决缺少 API 的问题:

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

但我想这是糟糕的形式,可能随时从我脚下滑落?

【问题讨论】:

  • 我猜不是任何时候,但只有当底层 API 以意想不到的方式发生变化时。

标签: javascript google-chrome-devtools puppeteer


【解决方案1】:

更新:无头 Chrome 现在支持网络节流!

在 Puppeteer 中,您可以模拟设备 (https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pageemulateoptions),但不能模拟网络条件。这是我们正在考虑的事情,但无头 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();
});

【讨论】:

  • 好的,我不知道无头 Chrome 不能被限制。至于向页面发送原始协议消息的能力,它会在某个时候暴露吗?或者它会保持原样,还是隐藏在封闭或其他东西中?基本上,我可以依靠它吗?
  • @ebdel 因为它被添加到铬,我们如何使用它? chromium.googlesource.com/chromium/src/+/…
  • 现在应该可以开箱即用了。你在看什么?
  • 我不知道这是否是一个有效的问题,但我已经准备好 Headless chrome 不支持网络节流,但我还没有阅读任何关于在 NON-headless 上支持网络节流的信息傀儡师模式。谁能给我解释一下?
  • 嗨 ickyrr,节流在 puppeteer 的非无头模式下工作。请参阅原始问题的解决方法。
猜你喜欢
  • 2017-04-20
  • 2021-05-26
  • 2018-06-02
  • 1970-01-01
  • 1970-01-01
  • 2020-05-12
  • 2018-11-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多