Skip to content

Commit

Permalink
chore: emulation over bidi+ (#10391)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Jun 15, 2023
1 parent e411074 commit 866addd
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 18 deletions.
73 changes: 56 additions & 17 deletions packages/puppeteer-core/src/common/bidi/Page.ts
Expand Up @@ -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,
Expand All @@ -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';
Expand Down Expand Up @@ -121,6 +125,7 @@ export class Page extends PageBase {
]);
#tracing: Tracing;
#coverage: Coverage;
#emulationManager: EmulationManager;

constructor(browserContext: BrowserContext, info: {context: string}) {
super();
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -393,25 +401,56 @@ export class Page extends PageBase {
return this.mainFrame().content();
}

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,
});
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> {
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 {
Expand Down
32 changes: 31 additions & 1 deletion test/TestExpectations.json
Expand Up @@ -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"],
Expand Down

0 comments on commit 866addd

Please sign in to comment.