chore: use setViewport (#10748)
This commit is contained in:
parent
532c0eb40c
commit
8424ecf4c4
43
packages/puppeteer-core/src/common/bidi/EmulationManager.ts
Normal file
43
packages/puppeteer-core/src/common/bidi/EmulationManager.ts
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2023 Google Inc. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {BrowsingContext} from './BrowsingContext.js';
|
||||||
|
|
||||||
|
interface Viewport {
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
export class EmulationManager {
|
||||||
|
#browsingContext: BrowsingContext;
|
||||||
|
|
||||||
|
constructor(browsingContext: BrowsingContext) {
|
||||||
|
this.#browsingContext = browsingContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
async emulateViewport(viewport: Viewport): Promise<void> {
|
||||||
|
await this.#browsingContext.connection.send(
|
||||||
|
'browsingContext.setViewport' as any,
|
||||||
|
{
|
||||||
|
context: this.#browsingContext.id,
|
||||||
|
viewport,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -34,7 +34,7 @@ import {Accessibility} from '../Accessibility.js';
|
|||||||
import {CDPSession} from '../Connection.js';
|
import {CDPSession} from '../Connection.js';
|
||||||
import {ConsoleMessage, ConsoleMessageLocation} from '../ConsoleMessage.js';
|
import {ConsoleMessage, ConsoleMessageLocation} from '../ConsoleMessage.js';
|
||||||
import {Coverage} from '../Coverage.js';
|
import {Coverage} from '../Coverage.js';
|
||||||
import {EmulationManager} from '../EmulationManager.js';
|
import {EmulationManager as CDPEmulationManager} from '../EmulationManager.js';
|
||||||
import {TargetCloseError} from '../Errors.js';
|
import {TargetCloseError} from '../Errors.js';
|
||||||
import {Handler} from '../EventEmitter.js';
|
import {Handler} from '../EventEmitter.js';
|
||||||
import {FrameTree} from '../FrameTree.js';
|
import {FrameTree} from '../FrameTree.js';
|
||||||
@ -63,6 +63,7 @@ import {
|
|||||||
} from './BrowsingContext.js';
|
} from './BrowsingContext.js';
|
||||||
import {Connection} from './Connection.js';
|
import {Connection} from './Connection.js';
|
||||||
import {Dialog} from './Dialog.js';
|
import {Dialog} from './Dialog.js';
|
||||||
|
import {EmulationManager} from './EmulationManager.js';
|
||||||
import {Frame} from './Frame.js';
|
import {Frame} from './Frame.js';
|
||||||
import {HTTPRequest} from './HTTPRequest.js';
|
import {HTTPRequest} from './HTTPRequest.js';
|
||||||
import {HTTPResponse} from './HTTPResponse.js';
|
import {HTTPResponse} from './HTTPResponse.js';
|
||||||
@ -127,6 +128,7 @@ export class Page extends PageBase {
|
|||||||
]);
|
]);
|
||||||
#tracing: Tracing;
|
#tracing: Tracing;
|
||||||
#coverage: Coverage;
|
#coverage: Coverage;
|
||||||
|
#cdpEmulationManager: CDPEmulationManager;
|
||||||
#emulationManager: EmulationManager;
|
#emulationManager: EmulationManager;
|
||||||
#mouse: Mouse;
|
#mouse: Mouse;
|
||||||
#touchscreen: Touchscreen;
|
#touchscreen: Touchscreen;
|
||||||
@ -176,9 +178,10 @@ export class Page extends PageBase {
|
|||||||
);
|
);
|
||||||
this.#tracing = new Tracing(this.mainFrame().context().cdpSession);
|
this.#tracing = new Tracing(this.mainFrame().context().cdpSession);
|
||||||
this.#coverage = new Coverage(this.mainFrame().context().cdpSession);
|
this.#coverage = new Coverage(this.mainFrame().context().cdpSession);
|
||||||
this.#emulationManager = new EmulationManager(
|
this.#cdpEmulationManager = new CDPEmulationManager(
|
||||||
this.mainFrame().context().cdpSession
|
this.mainFrame().context().cdpSession
|
||||||
);
|
);
|
||||||
|
this.#emulationManager = new EmulationManager(browsingContext);
|
||||||
this.#mouse = new Mouse(this.mainFrame().context());
|
this.#mouse = new Mouse(this.mainFrame().context());
|
||||||
this.#touchscreen = new Touchscreen(this.mainFrame().context());
|
this.#touchscreen = new Touchscreen(this.mainFrame().context());
|
||||||
this.#keyboard = new Keyboard(this.mainFrame().context());
|
this.#keyboard = new Keyboard(this.mainFrame().context());
|
||||||
@ -508,50 +511,57 @@ export class Page extends PageBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override isJavaScriptEnabled(): boolean {
|
override isJavaScriptEnabled(): boolean {
|
||||||
return this.#emulationManager.javascriptEnabled;
|
return this.#cdpEmulationManager.javascriptEnabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
override async setGeolocation(options: GeolocationOptions): Promise<void> {
|
override async setGeolocation(options: GeolocationOptions): Promise<void> {
|
||||||
return await this.#emulationManager.setGeolocation(options);
|
return await this.#cdpEmulationManager.setGeolocation(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
override async setJavaScriptEnabled(enabled: boolean): Promise<void> {
|
override async setJavaScriptEnabled(enabled: boolean): Promise<void> {
|
||||||
return await this.#emulationManager.setJavaScriptEnabled(enabled);
|
return await this.#cdpEmulationManager.setJavaScriptEnabled(enabled);
|
||||||
}
|
}
|
||||||
|
|
||||||
override async emulateMediaType(type?: string): Promise<void> {
|
override async emulateMediaType(type?: string): Promise<void> {
|
||||||
return await this.#emulationManager.emulateMediaType(type);
|
return await this.#cdpEmulationManager.emulateMediaType(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
override async emulateCPUThrottling(factor: number | null): Promise<void> {
|
override async emulateCPUThrottling(factor: number | null): Promise<void> {
|
||||||
return await this.#emulationManager.emulateCPUThrottling(factor);
|
return await this.#cdpEmulationManager.emulateCPUThrottling(factor);
|
||||||
}
|
}
|
||||||
|
|
||||||
override async emulateMediaFeatures(
|
override async emulateMediaFeatures(
|
||||||
features?: MediaFeature[]
|
features?: MediaFeature[]
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
return await this.#emulationManager.emulateMediaFeatures(features);
|
return await this.#cdpEmulationManager.emulateMediaFeatures(features);
|
||||||
}
|
}
|
||||||
|
|
||||||
override async emulateTimezone(timezoneId?: string): Promise<void> {
|
override async emulateTimezone(timezoneId?: string): Promise<void> {
|
||||||
return await this.#emulationManager.emulateTimezone(timezoneId);
|
return await this.#cdpEmulationManager.emulateTimezone(timezoneId);
|
||||||
}
|
}
|
||||||
|
|
||||||
override async emulateIdleState(overrides?: {
|
override async emulateIdleState(overrides?: {
|
||||||
isUserActive: boolean;
|
isUserActive: boolean;
|
||||||
isScreenUnlocked: boolean;
|
isScreenUnlocked: boolean;
|
||||||
}): Promise<void> {
|
}): Promise<void> {
|
||||||
return await this.#emulationManager.emulateIdleState(overrides);
|
return await this.#cdpEmulationManager.emulateIdleState(overrides);
|
||||||
}
|
}
|
||||||
|
|
||||||
override async emulateVisionDeficiency(
|
override async emulateVisionDeficiency(
|
||||||
type?: Protocol.Emulation.SetEmulatedVisionDeficiencyRequest['type']
|
type?: Protocol.Emulation.SetEmulatedVisionDeficiencyRequest['type']
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
return await this.#emulationManager.emulateVisionDeficiency(type);
|
return await this.#cdpEmulationManager.emulateVisionDeficiency(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
override async setViewport(viewport: Viewport): Promise<void> {
|
override async setViewport(viewport: Viewport): Promise<void> {
|
||||||
const needsReload = await this.#emulationManager.emulateViewport(viewport);
|
if (!this.#browsingContext.supportsCDP()) {
|
||||||
|
await this.#emulationManager.emulateViewport(viewport);
|
||||||
|
this.#viewport = viewport;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const needsReload = await this.#cdpEmulationManager.emulateViewport(
|
||||||
|
viewport
|
||||||
|
);
|
||||||
this.#viewport = viewport;
|
this.#viewport = viewport;
|
||||||
if (needsReload) {
|
if (needsReload) {
|
||||||
// TODO: reload seems to hang in BiDi.
|
// TODO: reload seems to hang in BiDi.
|
||||||
|
@ -89,6 +89,12 @@
|
|||||||
"parameters": ["webDriverBiDi"],
|
"parameters": ["webDriverBiDi"],
|
||||||
"expectations": ["PASS"]
|
"expectations": ["PASS"]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"testIdPattern": "[locator.spec] *",
|
||||||
|
"platforms": ["darwin", "linux", "win32"],
|
||||||
|
"parameters": ["webDriverBiDi"],
|
||||||
|
"expectations": ["PASS"]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"testIdPattern": "[navigation.spec] *",
|
"testIdPattern": "[navigation.spec] *",
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
"platforms": ["darwin", "linux", "win32"],
|
||||||
@ -971,48 +977,6 @@
|
|||||||
"parameters": ["firefox"],
|
"parameters": ["firefox"],
|
||||||
"expectations": ["SKIP"]
|
"expectations": ["SKIP"]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"testIdPattern": "[locator.spec] *",
|
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
|
||||||
"parameters": ["chrome", "webDriverBiDi"],
|
|
||||||
"expectations": ["PASS"]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"testIdPattern": "[locator.spec] *",
|
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
|
||||||
"parameters": ["firefox", "webDriverBiDi"],
|
|
||||||
"expectations": ["SKIP"]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"testIdPattern": "[locator.spec] Locator Locator.change should override pre-filled inputs",
|
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
|
||||||
"parameters": ["webDriverBiDi"],
|
|
||||||
"expectations": ["PASS"]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"testIdPattern": "[locator.spec] Locator Locator.change should work for contenteditable",
|
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
|
||||||
"parameters": ["webDriverBiDi"],
|
|
||||||
"expectations": ["FAIL", "PASS"]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"testIdPattern": "[locator.spec] Locator Locator.change should work for inputs",
|
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
|
||||||
"parameters": ["webDriverBiDi"],
|
|
||||||
"expectations": ["PASS"]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"testIdPattern": "[locator.spec] Locator Locator.change should work for pre-filled inputs",
|
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
|
||||||
"parameters": ["webDriverBiDi"],
|
|
||||||
"expectations": ["PASS"]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"testIdPattern": "[locator.spec] Locator Locator.change should work if the input becomes enabled later",
|
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
|
||||||
"parameters": ["webDriverBiDi"],
|
|
||||||
"expectations": ["PASS"]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"testIdPattern": "[locator.spec] Locator Locator.click should work with a OOPIF",
|
"testIdPattern": "[locator.spec] Locator Locator.click should work with a OOPIF",
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
"platforms": ["darwin", "linux", "win32"],
|
||||||
@ -1865,12 +1829,6 @@
|
|||||||
"parameters": ["firefox", "webDriverBiDi"],
|
"parameters": ["firefox", "webDriverBiDi"],
|
||||||
"expectations": ["FAIL"]
|
"expectations": ["FAIL"]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"testIdPattern": "[click.spec] Page.click should not hang with touch-enabled viewports",
|
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
|
||||||
"parameters": ["firefox", "webDriverBiDi"],
|
|
||||||
"expectations": ["FAIL"]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"testIdPattern": "[click.spec] Page.click should scroll and click with disabled javascript",
|
"testIdPattern": "[click.spec] Page.click should scroll and click with disabled javascript",
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
"platforms": ["darwin", "linux", "win32"],
|
||||||
@ -2003,12 +1961,6 @@
|
|||||||
"parameters": ["chrome", "webDriverBiDi"],
|
"parameters": ["chrome", "webDriverBiDi"],
|
||||||
"expectations": ["FAIL"]
|
"expectations": ["FAIL"]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"testIdPattern": "[elementhandle.spec] ElementHandle specs ElementHandle.boundingBox should force a layout",
|
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
|
||||||
"parameters": ["firefox", "webDriverBiDi"],
|
|
||||||
"expectations": ["FAIL"]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"testIdPattern": "[elementhandle.spec] ElementHandle specs ElementHandle.boundingBox should handle nested frames",
|
"testIdPattern": "[elementhandle.spec] ElementHandle specs ElementHandle.boundingBox should handle nested frames",
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
"platforms": ["darwin", "linux", "win32"],
|
||||||
@ -2615,12 +2567,6 @@
|
|||||||
"parameters": ["cdp", "firefox"],
|
"parameters": ["cdp", "firefox"],
|
||||||
"expectations": ["SKIP"]
|
"expectations": ["SKIP"]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"testIdPattern": "[launcher.spec] Launcher specs Puppeteer Puppeteer.launch should set the default viewport",
|
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
|
||||||
"parameters": ["firefox", "webDriverBiDi"],
|
|
||||||
"expectations": ["FAIL"]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"testIdPattern": "[launcher.spec] Launcher specs Puppeteer Puppeteer.launch should work with no default arguments",
|
"testIdPattern": "[launcher.spec] Launcher specs Puppeteer Puppeteer.launch should work with no default arguments",
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
"platforms": ["darwin", "linux", "win32"],
|
||||||
|
Loading…
Reference in New Issue
Block a user