From dd8c229a4fed64913fd682a9fd0c36b4377a6e32 Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Wed, 26 Apr 2023 14:37:31 +0200 Subject: [PATCH] chore: add bidi+ emulation command (#10087) --- .../src/common/bidi/Connection.ts | 8 ++++++ .../puppeteer-core/src/common/bidi/Context.ts | 17 +++++++++++++ .../puppeteer-core/src/common/bidi/Page.ts | 25 +++++++++++++++++++ test/TestExpectations.json | 18 +++++++++++++ 4 files changed, 68 insertions(+) diff --git a/packages/puppeteer-core/src/common/bidi/Connection.ts b/packages/puppeteer-core/src/common/bidi/Connection.ts index c690d640834..5f26ee00fb1 100644 --- a/packages/puppeteer-core/src/common/bidi/Connection.ts +++ b/packages/puppeteer-core/src/common/bidi/Connection.ts @@ -80,6 +80,14 @@ interface Commands { params: Bidi.Session.SubscribeParameters; returnType: Bidi.Session.UnsubscribeResult; }; + 'cdp.sendCommand': { + params: Bidi.CDP.SendCommandParams; + returnType: Bidi.CDP.SendCommandResult; + }; + 'cdp.getSession': { + params: Bidi.CDP.GetSessionParams; + returnType: Bidi.CDP.GetSessionResult; + }; } /** diff --git a/packages/puppeteer-core/src/common/bidi/Context.ts b/packages/puppeteer-core/src/common/bidi/Context.ts index 82dc23cc2c5..4d3711d6aac 100644 --- a/packages/puppeteer-core/src/common/bidi/Context.ts +++ b/packages/puppeteer-core/src/common/bidi/Context.ts @@ -20,6 +20,7 @@ import {HTTPResponse} from '../../api/HTTPResponse.js'; import {WaitForOptions} from '../../api/Page.js'; import {assert} from '../../util/assert.js'; import {stringifyFunction} from '../../util/Function.js'; +import {ProtocolMapping} from '../Connection.js'; import {ProtocolError, TimeoutError} from '../Errors.js'; import {EventEmitter} from '../EventEmitter.js'; import {PuppeteerLifeCycleEvent} from '../LifecycleWatcher.js'; @@ -222,6 +223,22 @@ export class Context extends EventEmitter { ), ]); } + + async sendCDPCommand( + method: keyof ProtocolMapping.Commands, + params: object = {} + ): Promise { + const session = await this.#connection.send('cdp.getSession', { + context: this._contextId, + }); + // TODO: remove any once chromium-bidi types are updated. + const sessionId = (session.result as any).cdpSession; + return await this.#connection.send('cdp.sendCommand', { + cdpMethod: method, + cdpParams: params, + cdpSession: sessionId, + }); + } } /** diff --git a/packages/puppeteer-core/src/common/bidi/Page.ts b/packages/puppeteer-core/src/common/bidi/Page.ts index 223a63c5115..524f5ed122f 100644 --- a/packages/puppeteer-core/src/common/bidi/Page.ts +++ b/packages/puppeteer-core/src/common/bidi/Page.ts @@ -29,6 +29,7 @@ import {isErrorLike} from '../../util/ErrorLike.js'; import {ConsoleMessage, ConsoleMessageLocation} from '../ConsoleMessage.js'; import {Handler} from '../EventEmitter.js'; import {PDFOptions} from '../PDFOptions.js'; +import {Viewport} from '../PuppeteerViewport.js'; import {EvaluateFunc, HandleFor} from '../types.js'; import {debugError, waitWithTimeout} from '../util.js'; @@ -45,6 +46,7 @@ export class Page extends PageBase { ['browsingContext.load', this.#onLoad.bind(this)], ['browsingContext.domContentLoaded', this.#onDOMLoad.bind(this)], ]) as Map; + #viewport: Viewport | null = null; constructor(context: Context) { super(); @@ -204,6 +206,29 @@ export class Page extends PageBase { }); } + 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'}; + + await this.#context.sendCDPCommand('Emulation.setDeviceMetricsOverride', { + mobile, + width, + height, + deviceScaleFactor, + screenOrientation, + }); + + this.#viewport = viewport; + } + + override viewport(): Viewport | null { + return this.#viewport; + } + override async pdf(options: PDFOptions = {}): Promise { const {path = undefined} = options; const { diff --git a/test/TestExpectations.json b/test/TestExpectations.json index 6b7fb0353df..28b39009acb 100644 --- a/test/TestExpectations.json +++ b/test/TestExpectations.json @@ -419,6 +419,12 @@ "parameters": ["cdp", "firefox"], "expectations": ["FAIL", "SKIP"] }, + { + "testIdPattern": "[screenshot.spec] Screenshots Page.screenshot *", + "platforms": ["darwin", "linux", "win32"], + "parameters": ["chrome", "webDriverBiDi"], + "expectations": ["FAIL"] + }, { "testIdPattern": "[TargetManager.spec] *", "platforms": ["darwin", "linux", "win32"], @@ -1949,6 +1955,12 @@ "parameters": ["cdp", "firefox"], "expectations": ["FAIL"] }, + { + "testIdPattern": "[screenshot.spec] Screenshots Page.screenshot should return base64", + "platforms": ["darwin", "linux", "win32"], + "parameters": ["chrome", "webDriverBiDi"], + "expectations": ["PASS"] + }, { "testIdPattern": "[screenshot.spec] Screenshots Page.screenshot should return base64", "platforms": ["darwin", "linux", "win32"], @@ -1967,6 +1979,12 @@ "parameters": ["cdp", "firefox"], "expectations": ["FAIL", "PASS"] }, + { + "testIdPattern": "[screenshot.spec] Screenshots Page.screenshot should work", + "platforms": ["darwin", "linux", "win32"], + "parameters": ["chrome", "webDriverBiDi"], + "expectations": ["PASS"] + }, { "testIdPattern": "[screenshot.spec] Screenshots Page.screenshot should work", "platforms": ["darwin", "linux", "win32"],