From 866addd132270e56cb1f84885f2897123087215c Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Thu, 15 Jun 2023 15:58:48 +0200 Subject: [PATCH] chore: emulation over bidi+ (#10391) --- .../puppeteer-core/src/common/bidi/Page.ts | 73 ++++++++++++++----- test/TestExpectations.json | 32 +++++++- 2 files changed, 87 insertions(+), 18 deletions(-) diff --git a/packages/puppeteer-core/src/common/bidi/Page.ts b/packages/puppeteer-core/src/common/bidi/Page.ts index c10bda97be8..324235c5966 100644 --- a/packages/puppeteer-core/src/common/bidi/Page.ts +++ b/packages/puppeteer-core/src/common/bidi/Page.ts @@ -17,8 +17,11 @@ import type {Readable} from 'stream'; import * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js'; +import Protocol from 'devtools-protocol'; import { + GeolocationOptions, + MediaFeature, Page as PageBase, PageEmittedEvents, ScreenshotOptions, @@ -29,6 +32,7 @@ import {Deferred} from '../../util/Deferred.js'; import {Accessibility} from '../Accessibility.js'; import {ConsoleMessage, ConsoleMessageLocation} from '../ConsoleMessage.js'; import {Coverage} from '../Coverage.js'; +import {EmulationManager} from '../EmulationManager.js'; import {TargetCloseError} from '../Errors.js'; import {Handler} from '../EventEmitter.js'; import {FrameManagerEmittedEvents} from '../FrameManager.js'; @@ -121,6 +125,7 @@ export class Page extends PageBase { ]); #tracing: Tracing; #coverage: Coverage; + #emulationManager: EmulationManager; constructor(browserContext: BrowserContext, info: {context: string}) { super(); @@ -148,6 +153,9 @@ export class Page extends PageBase { ); this.#tracing = new Tracing(this.mainFrame().context().cdpSession); this.#coverage = new Coverage(this.mainFrame().context().cdpSession); + this.#emulationManager = new EmulationManager( + this.mainFrame().context().cdpSession + ); } override get accessibility(): Accessibility { @@ -393,25 +401,56 @@ export class Page extends PageBase { return this.mainFrame().content(); } + override isJavaScriptEnabled(): boolean { + return this.#emulationManager.javascriptEnabled; + } + + override async setGeolocation(options: GeolocationOptions): Promise { + return await this.#emulationManager.setGeolocation(options); + } + + override async setJavaScriptEnabled(enabled: boolean): Promise { + return await this.#emulationManager.setJavaScriptEnabled(enabled); + } + + override async emulateMediaType(type?: string): Promise { + return await this.#emulationManager.emulateMediaType(type); + } + + override async emulateCPUThrottling(factor: number | null): Promise { + return await this.#emulationManager.emulateCPUThrottling(factor); + } + + override async emulateMediaFeatures( + features?: MediaFeature[] + ): Promise { + return await this.#emulationManager.emulateMediaFeatures(features); + } + + override async emulateTimezone(timezoneId?: string): Promise { + return await this.#emulationManager.emulateTimezone(timezoneId); + } + + override async emulateIdleState(overrides?: { + isUserActive: boolean; + isScreenUnlocked: boolean; + }): Promise { + return await this.#emulationManager.emulateIdleState(overrides); + } + + override async emulateVisionDeficiency( + type?: Protocol.Emulation.SetEmulatedVisionDeficiencyRequest['type'] + ): Promise { + return await this.#emulationManager.emulateVisionDeficiency(type); + } + override async setViewport(viewport: Viewport): Promise { - // TODO: use BiDi commands when available. - const mobile = false; - const width = viewport.width; - const height = viewport.height; - const deviceScaleFactor = 1; - const screenOrientation = {angle: 0, type: 'portraitPrimary' as const}; - - await this.mainFrame() - .context() - .sendCDPCommand('Emulation.setDeviceMetricsOverride', { - mobile, - width, - height, - deviceScaleFactor, - screenOrientation, - }); - + const needsReload = await this.#emulationManager.emulateViewport(viewport); this.#viewport = viewport; + if (needsReload) { + // TODO: reload seems to hang in BiDi. + // await this.reload(); + } } override viewport(): Viewport | null { diff --git a/test/TestExpectations.json b/test/TestExpectations.json index 19676bde229..70ecedb3174 100644 --- a/test/TestExpectations.json +++ b/test/TestExpectations.json @@ -1230,11 +1230,41 @@ "expectations": ["FAIL"] }, { - "testIdPattern": "[emulation.spec] Emulation Page.viewport should support mobile emulation", + "testIdPattern": "[emulation.spec] *", "platforms": ["darwin", "linux", "win32"], "parameters": ["chrome", "webDriverBiDi"], "expectations": ["PASS"] }, + { + "testIdPattern": "[emulation.spec] Emulation Page.viewport should support touch emulation", + "platforms": ["darwin", "linux", "win32"], + "parameters": ["chrome", "webDriverBiDi"], + "expectations": ["FAIL"] + }, + { + "testIdPattern": "[emulation.spec] Emulation Page.viewport should detect touch when applying viewport with touches", + "platforms": ["darwin", "linux", "win32"], + "parameters": ["chrome", "webDriverBiDi"], + "expectations": ["FAIL"] + }, + { + "testIdPattern": "[emulation.spec] Emulation Page.emulate should work", + "platforms": ["darwin", "linux", "win32"], + "parameters": ["chrome", "webDriverBiDi"], + "expectations": ["FAIL"] + }, + { + "testIdPattern": "[emulation.spec] Emulation Page.emulate should support clicking", + "platforms": ["darwin", "linux", "win32"], + "parameters": ["chrome", "webDriverBiDi"], + "expectations": ["FAIL"] + }, + { + "testIdPattern": "[emulation.spec] Emulation Page.emulateNetworkConditions should change navigator.connection.effectiveType", + "platforms": ["darwin", "linux", "win32"], + "parameters": ["chrome", "webDriverBiDi"], + "expectations": ["FAIL"] + }, { "testIdPattern": "[evaluation.spec] Evaluation specs \"after each\" hook for \"should transfer 100Mb of data from page to node.js\"", "platforms": ["darwin"],