chore: add bidi+ emulation command (#10087)

This commit is contained in:
Alex Rudenko 2023-04-26 14:37:31 +02:00 committed by GitHub
parent e4b57c279a
commit dd8c229a4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 68 additions and 0 deletions

View File

@ -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;
};
}
/**

View File

@ -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<unknown> {
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,
});
}
}
/**

View File

@ -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<Bidi.Session.SubscribeParametersEvent, Handler>;
#viewport: Viewport | null = null;
constructor(context: Context) {
super();
@ -204,6 +206,29 @@ export class Page extends PageBase {
});
}
override async setViewport(viewport: Viewport): Promise<void> {
// 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<Buffer> {
const {path = undefined} = options;
const {

View File

@ -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"],