chore: add browser and browserContext methods to Page (#10290)

This commit is contained in:
Nikolay Vitkov 2023-06-01 13:04:36 +02:00 committed by GitHub
parent 7aa7f46c7c
commit 74becdb6a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 7 deletions

View File

@ -61,6 +61,10 @@ export class Browser extends BrowserBase {
this.#defaultViewport = opts.defaultViewport;
}
get connection(): Connection {
return this.#connection;
}
override async close(): Promise<void> {
this.#connection.dispose();
await this.#closeCallback?.call(null);
@ -77,7 +81,7 @@ export class Browser extends BrowserBase {
override async createIncognitoBrowserContext(
_options?: BrowserContextOptions
): Promise<BrowserContextBase> {
return new BrowserContext(this.#connection, {
return new BrowserContext(this, {
defaultViewport: this.#defaultViewport,
});
}

View File

@ -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<string, Page>();
#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;
}
}

View File

@ -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<Frame>();
#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!');

View File

@ -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"],