chore: use setViewport (#10748)

This commit is contained in:
Alex Rudenko 2023-08-18 10:15:43 +02:00 committed by GitHub
parent 532c0eb40c
commit 8424ecf4c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 72 deletions

View 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,
}
);
}
}

View File

@ -34,7 +34,7 @@ import {Accessibility} from '../Accessibility.js';
import {CDPSession} from '../Connection.js';
import {ConsoleMessage, ConsoleMessageLocation} from '../ConsoleMessage.js';
import {Coverage} from '../Coverage.js';
import {EmulationManager} from '../EmulationManager.js';
import {EmulationManager as CDPEmulationManager} from '../EmulationManager.js';
import {TargetCloseError} from '../Errors.js';
import {Handler} from '../EventEmitter.js';
import {FrameTree} from '../FrameTree.js';
@ -63,6 +63,7 @@ import {
} from './BrowsingContext.js';
import {Connection} from './Connection.js';
import {Dialog} from './Dialog.js';
import {EmulationManager} from './EmulationManager.js';
import {Frame} from './Frame.js';
import {HTTPRequest} from './HTTPRequest.js';
import {HTTPResponse} from './HTTPResponse.js';
@ -127,6 +128,7 @@ export class Page extends PageBase {
]);
#tracing: Tracing;
#coverage: Coverage;
#cdpEmulationManager: CDPEmulationManager;
#emulationManager: EmulationManager;
#mouse: Mouse;
#touchscreen: Touchscreen;
@ -176,9 +178,10 @@ 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.#cdpEmulationManager = new CDPEmulationManager(
this.mainFrame().context().cdpSession
);
this.#emulationManager = new EmulationManager(browsingContext);
this.#mouse = new Mouse(this.mainFrame().context());
this.#touchscreen = new Touchscreen(this.mainFrame().context());
this.#keyboard = new Keyboard(this.mainFrame().context());
@ -508,50 +511,57 @@ export class Page extends PageBase {
}
override isJavaScriptEnabled(): boolean {
return this.#emulationManager.javascriptEnabled;
return this.#cdpEmulationManager.javascriptEnabled;
}
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> {
return await this.#emulationManager.setJavaScriptEnabled(enabled);
return await this.#cdpEmulationManager.setJavaScriptEnabled(enabled);
}
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> {
return await this.#emulationManager.emulateCPUThrottling(factor);
return await this.#cdpEmulationManager.emulateCPUThrottling(factor);
}
override async emulateMediaFeatures(
features?: MediaFeature[]
): Promise<void> {
return await this.#emulationManager.emulateMediaFeatures(features);
return await this.#cdpEmulationManager.emulateMediaFeatures(features);
}
override async emulateTimezone(timezoneId?: string): Promise<void> {
return await this.#emulationManager.emulateTimezone(timezoneId);
return await this.#cdpEmulationManager.emulateTimezone(timezoneId);
}
override async emulateIdleState(overrides?: {
isUserActive: boolean;
isScreenUnlocked: boolean;
}): Promise<void> {
return await this.#emulationManager.emulateIdleState(overrides);
return await this.#cdpEmulationManager.emulateIdleState(overrides);
}
override async emulateVisionDeficiency(
type?: Protocol.Emulation.SetEmulatedVisionDeficiencyRequest['type']
): Promise<void> {
return await this.#emulationManager.emulateVisionDeficiency(type);
return await this.#cdpEmulationManager.emulateVisionDeficiency(type);
}
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;
if (needsReload) {
// TODO: reload seems to hang in BiDi.

View File

@ -89,6 +89,12 @@
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[locator.spec] *",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[navigation.spec] *",
"platforms": ["darwin", "linux", "win32"],
@ -971,48 +977,6 @@
"parameters": ["firefox"],
"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",
"platforms": ["darwin", "linux", "win32"],
@ -1865,12 +1829,6 @@
"parameters": ["firefox", "webDriverBiDi"],
"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",
"platforms": ["darwin", "linux", "win32"],
@ -2003,12 +1961,6 @@
"parameters": ["chrome", "webDriverBiDi"],
"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",
"platforms": ["darwin", "linux", "win32"],
@ -2615,12 +2567,6 @@
"parameters": ["cdp", "firefox"],
"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",
"platforms": ["darwin", "linux", "win32"],