From 74becdb6a0eb22399ccbaf766311c7c63e7ee92b Mon Sep 17 00:00:00 2001 From: Nikolay Vitkov <34244704+Lightning00Blade@users.noreply.github.com> Date: Thu, 1 Jun 2023 13:04:36 +0200 Subject: [PATCH] chore: add browser and browserContext methods to Page (#10290) --- .../puppeteer-core/src/common/bidi/Browser.ts | 6 +++++- .../src/common/bidi/BrowserContext.ts | 17 ++++++++++++++--- .../puppeteer-core/src/common/bidi/Page.ts | 18 +++++++++++++++--- test/TestExpectations.json | 12 ++++++++++++ 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/packages/puppeteer-core/src/common/bidi/Browser.ts b/packages/puppeteer-core/src/common/bidi/Browser.ts index 089deedf..747cabaa 100644 --- a/packages/puppeteer-core/src/common/bidi/Browser.ts +++ b/packages/puppeteer-core/src/common/bidi/Browser.ts @@ -61,6 +61,10 @@ export class Browser extends BrowserBase { this.#defaultViewport = opts.defaultViewport; } + get connection(): Connection { + return this.#connection; + } + override async close(): Promise { this.#connection.dispose(); await this.#closeCallback?.call(null); @@ -77,7 +81,7 @@ export class Browser extends BrowserBase { override async createIncognitoBrowserContext( _options?: BrowserContextOptions ): Promise { - return new BrowserContext(this.#connection, { + return new BrowserContext(this, { defaultViewport: this.#defaultViewport, }); } diff --git a/packages/puppeteer-core/src/common/bidi/BrowserContext.ts b/packages/puppeteer-core/src/common/bidi/BrowserContext.ts index 123686be..82b6959b 100644 --- a/packages/puppeteer-core/src/common/bidi/BrowserContext.ts +++ b/packages/puppeteer-core/src/common/bidi/BrowserContext.ts @@ -20,6 +20,7 @@ import {BrowserContext as BrowserContextBase} from '../../api/BrowserContext.js' import {Page as PageBase} from '../../api/Page.js'; import {Viewport} from '../PuppeteerViewport.js'; +import {Browser} from './Browser.js'; import {Connection} from './Connection.js'; import {Page} from './Page.js'; import {debugError} from './utils.js'; @@ -32,14 +33,16 @@ interface BrowserContextOptions { * @internal */ export class BrowserContext extends BrowserContextBase { + #browser: Browser; #connection: Connection; #defaultViewport: Viewport | null; #pages = new Map(); #onContextDestroyedBind = this.#onContextDestroyed.bind(this); - constructor(connection: Connection, options: BrowserContextOptions) { + constructor(browser: Browser, options: BrowserContextOptions) { super(); - this.#connection = connection; + this.#browser = browser; + this.#connection = this.#browser.connection; this.#defaultViewport = options.defaultViewport; this.#connection.on( 'browsingContext.contextDestroyed', @@ -47,6 +50,10 @@ export class BrowserContext extends BrowserContextBase { ); } + get connection(): Connection { + return this.#connection; + } + async #onContextDestroyed( event: Bidi.BrowsingContext.ContextDestroyedEvent['params'] ) { @@ -61,7 +68,7 @@ export class BrowserContext extends BrowserContextBase { const {result} = await this.#connection.send('browsingContext.create', { type: 'tab', }); - const page = new Page(this.#connection, result); + const page = new Page(this, result); if (this.#defaultViewport) { try { await page.setViewport(this.#defaultViewport); @@ -83,4 +90,8 @@ export class BrowserContext extends BrowserContextBase { } this.#pages.clear(); } + + override browser(): Browser { + return this.#browser; + } } diff --git a/packages/puppeteer-core/src/common/bidi/Page.ts b/packages/puppeteer-core/src/common/bidi/Page.ts index c02d0bd6..9d9ea13b 100644 --- a/packages/puppeteer-core/src/common/bidi/Page.ts +++ b/packages/puppeteer-core/src/common/bidi/Page.ts @@ -44,6 +44,8 @@ import { withSourcePuppeteerURLIfNone, } from '../util.js'; +import {Browser} from './Browser.js'; +import {BrowserContext} from './BrowserContext.js'; import {BrowsingContext, getBidiHandle} from './BrowsingContext.js'; import {Connection} from './Connection.js'; import {Frame} from './Frame.js'; @@ -57,6 +59,7 @@ import {BidiSerializer} from './Serializer.js'; */ export class Page extends PageBase { #timeoutSettings = new TimeoutSettings(); + #browserContext: BrowserContext; #connection: Connection; #frameTree = new FrameTree(); #networkManager: NetworkManager; @@ -113,11 +116,12 @@ export class Page extends PageBase { ], ]); - constructor(connection: Connection, info: {context: string}) { + constructor(browserContext: BrowserContext, info: {context: string}) { super(); - this.#connection = connection; + this.#browserContext = browserContext; + this.#connection = browserContext.connection; - this.#networkManager = new NetworkManager(connection, this); + this.#networkManager = new NetworkManager(this.#connection, this); this.#onFrameAttached({ ...info, url: 'about:blank', @@ -133,6 +137,14 @@ export class Page extends PageBase { } } + override browser(): Browser { + return this.#browserContext.browser(); + } + + override browserContext(): BrowserContext { + return this.#browserContext; + } + override mainFrame(): Frame { const mainFrame = this.#frameTree.getMainFrame(); assert(mainFrame, 'Requesting main frame too early!'); diff --git a/test/TestExpectations.json b/test/TestExpectations.json index 69fc6cbc..22d165bc 100644 --- a/test/TestExpectations.json +++ b/test/TestExpectations.json @@ -137,6 +137,18 @@ "parameters": ["webDriverBiDi"], "expectations": ["PASS"] }, + { + "testIdPattern": "[page.spec] Page Page.browser *", + "platforms": ["darwin", "linux", "win32"], + "parameters": ["webDriverBiDi"], + "expectations": ["PASS"] + }, + { + "testIdPattern": "[page.spec] Page Page.browserContext *", + "platforms": ["darwin", "linux", "win32"], + "parameters": ["webDriverBiDi"], + "expectations": ["PASS"] + }, { "testIdPattern": "[page.spec] Page Page.Events.DOMContentLoaded *", "platforms": ["darwin", "linux", "win32"],