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

View File

@ -16,20 +16,27 @@
import {BrowserContext as BrowserContextBase} from '../../api/BrowserContext.js'; import {BrowserContext as BrowserContextBase} from '../../api/BrowserContext.js';
import {Page as PageBase} from '../../api/Page.js'; import {Page as PageBase} from '../../api/Page.js';
import {Viewport} from '../PuppeteerViewport.js';
import {Connection} from './Connection.js'; import {Connection} from './Connection.js';
import {Context} from './Context.js'; import {Context} from './Context.js';
import {Page} from './Page.js'; import {Page} from './Page.js';
interface BrowserContextOptions {
defaultViewport: Viewport | null;
}
/** /**
* @internal * @internal
*/ */
export class BrowserContext extends BrowserContextBase { export class BrowserContext extends BrowserContextBase {
#connection: Connection; #connection: Connection;
#defaultViewport: Viewport | null;
constructor(connection: Connection) { constructor(connection: Connection, options: BrowserContextOptions) {
super(); super();
this.#connection = connection; this.#connection = connection;
this.#defaultViewport = options.defaultViewport;
} }
override async newPage(): Promise<PageBase> { override async newPage(): Promise<PageBase> {
@ -37,7 +44,15 @@ export class BrowserContext extends BrowserContextBase {
type: 'tab', type: 'tab',
}); });
const context = this.#connection.context(result.context) as Context; 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> {} override async close(): Promise<void> {}

View File

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

View File

@ -725,6 +725,12 @@
"parameters": ["cdp", "firefox"], "parameters": ["cdp", "firefox"],
"expectations": ["PASS", "TIMEOUT"] "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", "testIdPattern": "[emulation.spec] Emulation Page.viewport should support landscape emulation",
"platforms": ["darwin", "linux", "win32"], "platforms": ["darwin", "linux", "win32"],