diff --git a/packages/puppeteer-core/src/common/bidi/Browser.ts b/packages/puppeteer-core/src/common/bidi/Browser.ts index f0282a751b4..9741ce7129d 100644 --- a/packages/puppeteer-core/src/common/bidi/Browser.ts +++ b/packages/puppeteer-core/src/common/bidi/Browser.ts @@ -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 { @@ -72,7 +75,9 @@ export class Browser extends BrowserBase { override async createIncognitoBrowserContext( _options?: BrowserContextOptions ): Promise { - 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; } diff --git a/packages/puppeteer-core/src/common/bidi/BrowserContext.ts b/packages/puppeteer-core/src/common/bidi/BrowserContext.ts index e015586ee39..92950b87b02 100644 --- a/packages/puppeteer-core/src/common/bidi/BrowserContext.ts +++ b/packages/puppeteer-core/src/common/bidi/BrowserContext.ts @@ -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 { @@ -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 {} diff --git a/packages/puppeteer-core/src/node/ProductLauncher.ts b/packages/puppeteer-core/src/node/ProductLauncher.ts index 3c3b7b790ac..719051930f6 100644 --- a/packages/puppeteer-core/src/node/ProductLauncher.ts +++ b/packages/puppeteer-core/src/node/ProductLauncher.ts @@ -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, connection: Connection, - closeCallback: BrowserCloseCallback + closeCallback: BrowserCloseCallback, + opts: { + timeout: number; + protocolTimeout: number | undefined; + slowMo: number; + defaultViewport: Viewport | null; + } ): Promise { + // 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, closeCallback: BrowserCloseCallback, - opts: {timeout: number; protocolTimeout: number | undefined; slowMo: number} + opts: { + timeout: number; + protocolTimeout: number | undefined; + slowMo: number; + defaultViewport: Viewport | null; + } ): Promise { 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, }); } diff --git a/test/TestExpectations.json b/test/TestExpectations.json index fc532f48929..e305f5a2330 100644 --- a/test/TestExpectations.json +++ b/test/TestExpectations.json @@ -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"],