Page.emulateNetworkConditions() 方法
🌐 Page.emulateNetworkConditions() method
这不会影响 WebSockets 和 WebRTC PeerConnections(参见 https://crbug.com/563644)。要将页面设置为离线,可以使用 Page.setOfflineMode()。
🌐 This does not affect WebSockets and WebRTC PeerConnections (see https://crbug.com/563644). To set the page offline, you can use Page.setOfflineMode().
可以通过导入 PredefinedNetworkConditions 来使用预定义的网络条件列表。
🌐 A list of predefined network conditions can be used by importing PredefinedNetworkConditions.
语法
🌐 Signature
class Page {
abstract emulateNetworkConditions(
networkConditions: NetworkConditions | null,
): Promise<void>;
}
参数
🌐 Parameters
范围 🌐 Parameter | 类型 🌐 Type | 描述 🌐 Description |
|---|---|---|
networkConditions | 网络状况 | null | 传递 🌐 Passing |
返回:
Promise<void>
示例
🌐 Example
import {PredefinedNetworkConditions} from 'puppeteer';
const slow3G = PredefinedNetworkConditions['Slow 3G'];
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.emulateNetworkConditions(slow3G);
await page.goto('https://www.google.com');
// other actions...
await browser.close();