(
options: {
networkConditions?: string;
cpuThrottlingRate?: number;
geolocation?: GeolocationOptions;
userAgent?: string;
colorScheme?: 'dark' | 'light' | 'auto';
viewport?: Viewport;
extraHttpHeaders?: Record<string, string> | undefined;
},
targetPage?: Page,
)
| 358 | } |
| 359 | |
| 360 | async emulate( |
| 361 | options: { |
| 362 | networkConditions?: string; |
| 363 | cpuThrottlingRate?: number; |
| 364 | geolocation?: GeolocationOptions; |
| 365 | userAgent?: string; |
| 366 | colorScheme?: 'dark' | 'light' | 'auto'; |
| 367 | viewport?: Viewport; |
| 368 | extraHttpHeaders?: Record<string, string> | undefined; |
| 369 | }, |
| 370 | targetPage?: Page, |
| 371 | ): Promise<void> { |
| 372 | const page = targetPage ?? this.getSelectedPptrPage(); |
| 373 | const mcpPage = this.#getMcpPage(page); |
| 374 | const newSettings: EmulationSettings = {...mcpPage.emulationSettings}; |
| 375 | |
| 376 | // Skip network emulation if blocklist/allowlist is configured, as it conflicts with blocking rules in Puppeteer. |
| 377 | if (this.#hasNetworkBlockOrAllowlist) { |
| 378 | if (options.networkConditions !== undefined) { |
| 379 | throw new Error( |
| 380 | 'Network throttling is not supported when network blocking (allowlist/blocklist) is configured.', |
| 381 | ); |
| 382 | } |
| 383 | } else if (!options.networkConditions) { |
| 384 | await page.emulateNetworkConditions(null); |
| 385 | delete newSettings.networkConditions; |
| 386 | } else if (options.networkConditions === 'Offline') { |
| 387 | await page.emulateNetworkConditions({ |
| 388 | offline: true, |
| 389 | download: 0, |
| 390 | upload: 0, |
| 391 | latency: 0, |
| 392 | }); |
| 393 | newSettings.networkConditions = 'Offline'; |
| 394 | } else if (options.networkConditions in PredefinedNetworkConditions) { |
| 395 | const networkCondition = |
| 396 | PredefinedNetworkConditions[ |
| 397 | options.networkConditions as keyof typeof PredefinedNetworkConditions |
| 398 | ]; |
| 399 | await page.emulateNetworkConditions(networkCondition); |
| 400 | newSettings.networkConditions = options.networkConditions; |
| 401 | } |
| 402 | |
| 403 | const secondarySession = this.getDevToolsUniverse(mcpPage)?.session; |
| 404 | if (!options.cpuThrottlingRate) { |
| 405 | await page.emulateCPUThrottling(1); |
| 406 | if (secondarySession) { |
| 407 | await secondarySession.send('Emulation.setCPUThrottlingRate', { |
| 408 | rate: 1, |
| 409 | }); |
| 410 | } |
| 411 | delete newSettings.cpuThrottlingRate; |
| 412 | } else { |
| 413 | await page.emulateCPUThrottling(options.cpuThrottlingRate); |
| 414 | if (secondarySession) { |
| 415 | await secondarySession.send('Emulation.setCPUThrottlingRate', { |
| 416 | rate: options.cpuThrottlingRate, |
| 417 | }); |
no test coverage detected