chore: emulation over bidi+ (#10391)

This commit is contained in:
Alex Rudenko 2023-06-15 15:58:48 +02:00 committed by GitHub
parent e4110746f1
commit 866addd132
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 87 additions and 18 deletions

View File

@ -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<void> {
return await this.#emulationManager.setGeolocation(options);
}
override async setJavaScriptEnabled(enabled: boolean): Promise<void> {
return await this.#emulationManager.setJavaScriptEnabled(enabled);
}
override async emulateMediaType(type?: string): Promise<void> {
return await this.#emulationManager.emulateMediaType(type);
}
override async emulateCPUThrottling(factor: number | null): Promise<void> {
return await this.#emulationManager.emulateCPUThrottling(factor);
}
override async emulateMediaFeatures(
features?: MediaFeature[]
): Promise<void> {
return await this.#emulationManager.emulateMediaFeatures(features);
}
override async emulateTimezone(timezoneId?: string): Promise<void> {
return await this.#emulationManager.emulateTimezone(timezoneId);
}
override async emulateIdleState(overrides?: {
isUserActive: boolean;
isScreenUnlocked: boolean;
}): Promise<void> {
return await this.#emulationManager.emulateIdleState(overrides);
}
override async emulateVisionDeficiency(
type?: Protocol.Emulation.SetEmulatedVisionDeficiencyRequest['type']
): Promise<void> {
return await this.#emulationManager.emulateVisionDeficiency(type);
}
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' 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 {

View File

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