chore: implement defaultViewport (#10089)

This commit is contained in:
Alex Rudenko 2023-04-26 15:56:23 +02:00 committed by GitHub
parent deeb314930
commit 1b125094b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 6 deletions

View File

@ -24,6 +24,7 @@ import {
BrowserContextOptions,
} from '../../api/Browser.js';
import {BrowserContext as BrowserContextBase} from '../../api/BrowserContext.js';
import {Viewport} from '../PuppeteerViewport.js';
import {BrowserContext} from './BrowserContext.js';
import {Connection} from './Connection.js';
@ -48,12 +49,14 @@ export class Browser extends BrowserBase {
#process?: ChildProcess;
#closeCallback?: BrowserCloseCallback;
#connection: Connection;
#defaultViewport: Viewport | null;
constructor(opts: Options) {
super();
this.#process = opts.process;
this.#closeCallback = opts.closeCallback;
this.#connection = opts.connection;
this.#defaultViewport = opts.defaultViewport;
}
override async close(): Promise<void> {
@ -72,7 +75,9 @@ export class Browser extends BrowserBase {
override async createIncognitoBrowserContext(
_options?: BrowserContextOptions
): Promise<BrowserContextBase> {
return new BrowserContext(this.#connection);
return new BrowserContext(this.#connection, {
defaultViewport: this.#defaultViewport,
});
}
}
@ -80,4 +85,5 @@ interface Options {
process?: ChildProcess;
closeCallback?: BrowserCloseCallback;
connection: Connection;
defaultViewport: Viewport | null;
}

View File

@ -16,20 +16,27 @@
import {BrowserContext as BrowserContextBase} from '../../api/BrowserContext.js';
import {Page as PageBase} from '../../api/Page.js';
import {Viewport} from '../PuppeteerViewport.js';
import {Connection} from './Connection.js';
import {Context} from './Context.js';
import {Page} from './Page.js';
interface BrowserContextOptions {
defaultViewport: Viewport | null;
}
/**
* @internal
*/
export class BrowserContext extends BrowserContextBase {
#connection: Connection;
#defaultViewport: Viewport | null;
constructor(connection: Connection) {
constructor(connection: Connection, options: BrowserContextOptions) {
super();
this.#connection = connection;
this.#defaultViewport = options.defaultViewport;
}
override async newPage(): Promise<PageBase> {
@ -37,7 +44,15 @@ export class BrowserContext extends BrowserContextBase {
type: 'tab',
});
const context = this.#connection.context(result.context) as Context;
return new Page(context);
const page = new Page(context);
if (this.#defaultViewport) {
try {
await page.setViewport(this.#defaultViewport);
} catch {
// No support for setViewport in Firefox.
}
}
return page;
}
override async close(): Promise<void> {}

View File

@ -30,6 +30,7 @@ import {Connection} from '../common/Connection.js';
import {TimeoutError} from '../common/Errors.js';
import {NodeWebSocketTransport as WebSocketTransport} from '../common/NodeWebSocketTransport.js';
import {Product} from '../common/Product.js';
import {Viewport} from '../common/PuppeteerViewport.js';
import {debugError} from '../common/util.js';
import {
@ -139,6 +140,7 @@ export class ProductLauncher {
timeout,
protocolTimeout,
slowMo,
defaultViewport,
}
);
} else {
@ -159,7 +161,13 @@ export class ProductLauncher {
browser = await this.createBiDiOverCDPBrowser(
browserProcess,
connection,
browserCloseCallback
browserCloseCallback,
{
timeout,
protocolTimeout,
slowMo,
defaultViewport,
}
);
} else {
browser = await CDPBrowser._create(
@ -313,8 +321,15 @@ export class ProductLauncher {
protected async createBiDiOverCDPBrowser(
browserProcess: ReturnType<typeof launch>,
connection: Connection,
closeCallback: BrowserCloseCallback
closeCallback: BrowserCloseCallback,
opts: {
timeout: number;
protocolTimeout: number | undefined;
slowMo: number;
defaultViewport: Viewport | null;
}
): Promise<Browser> {
// TODO: use other options too.
const BiDi = await import(
/* webpackIgnore: true */ '../common/bidi/bidi.js'
);
@ -323,6 +338,7 @@ export class ProductLauncher {
connection: bidiConnection,
closeCallback,
process: browserProcess.nodeProcess,
defaultViewport: opts.defaultViewport,
});
}
@ -332,7 +348,12 @@ export class ProductLauncher {
protected async createBiDiBrowser(
browserProcess: ReturnType<typeof launch>,
closeCallback: BrowserCloseCallback,
opts: {timeout: number; protocolTimeout: number | undefined; slowMo: number}
opts: {
timeout: number;
protocolTimeout: number | undefined;
slowMo: number;
defaultViewport: Viewport | null;
}
): Promise<Browser> {
const browserWSEndpoint =
(await browserProcess.waitForLineOutput(
@ -348,10 +369,12 @@ export class ProductLauncher {
opts.slowMo,
opts.protocolTimeout
);
// TODO: use other options too.
return await BiDi.Browser.create({
connection: bidiConnection,
closeCallback,
process: browserProcess.nodeProcess,
defaultViewport: opts.defaultViewport,
});
}

View File

@ -725,6 +725,12 @@
"parameters": ["cdp", "firefox"],
"expectations": ["PASS", "TIMEOUT"]
},
{
"testIdPattern": "[emulation.spec] Emulation Page.viewport should get the proper viewport size",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["chrome", "webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[emulation.spec] Emulation Page.viewport should support landscape emulation",
"platforms": ["darwin", "linux", "win32"],