mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore: add browser and browserContext methods to Page (#10290)
This commit is contained in:
parent
7aa7f46c7c
commit
74becdb6a0
@ -61,6 +61,10 @@ export class Browser extends BrowserBase {
|
|||||||
this.#defaultViewport = opts.defaultViewport;
|
this.#defaultViewport = opts.defaultViewport;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get connection(): Connection {
|
||||||
|
return this.#connection;
|
||||||
|
}
|
||||||
|
|
||||||
override async close(): Promise<void> {
|
override async close(): Promise<void> {
|
||||||
this.#connection.dispose();
|
this.#connection.dispose();
|
||||||
await this.#closeCallback?.call(null);
|
await this.#closeCallback?.call(null);
|
||||||
@ -77,7 +81,7 @@ 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, {
|
||||||
defaultViewport: this.#defaultViewport,
|
defaultViewport: this.#defaultViewport,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ 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 {Viewport} from '../PuppeteerViewport.js';
|
||||||
|
|
||||||
|
import {Browser} from './Browser.js';
|
||||||
import {Connection} from './Connection.js';
|
import {Connection} from './Connection.js';
|
||||||
import {Page} from './Page.js';
|
import {Page} from './Page.js';
|
||||||
import {debugError} from './utils.js';
|
import {debugError} from './utils.js';
|
||||||
@ -32,14 +33,16 @@ interface BrowserContextOptions {
|
|||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
export class BrowserContext extends BrowserContextBase {
|
export class BrowserContext extends BrowserContextBase {
|
||||||
|
#browser: Browser;
|
||||||
#connection: Connection;
|
#connection: Connection;
|
||||||
#defaultViewport: Viewport | null;
|
#defaultViewport: Viewport | null;
|
||||||
#pages = new Map<string, Page>();
|
#pages = new Map<string, Page>();
|
||||||
#onContextDestroyedBind = this.#onContextDestroyed.bind(this);
|
#onContextDestroyedBind = this.#onContextDestroyed.bind(this);
|
||||||
|
|
||||||
constructor(connection: Connection, options: BrowserContextOptions) {
|
constructor(browser: Browser, options: BrowserContextOptions) {
|
||||||
super();
|
super();
|
||||||
this.#connection = connection;
|
this.#browser = browser;
|
||||||
|
this.#connection = this.#browser.connection;
|
||||||
this.#defaultViewport = options.defaultViewport;
|
this.#defaultViewport = options.defaultViewport;
|
||||||
this.#connection.on(
|
this.#connection.on(
|
||||||
'browsingContext.contextDestroyed',
|
'browsingContext.contextDestroyed',
|
||||||
@ -47,6 +50,10 @@ export class BrowserContext extends BrowserContextBase {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get connection(): Connection {
|
||||||
|
return this.#connection;
|
||||||
|
}
|
||||||
|
|
||||||
async #onContextDestroyed(
|
async #onContextDestroyed(
|
||||||
event: Bidi.BrowsingContext.ContextDestroyedEvent['params']
|
event: Bidi.BrowsingContext.ContextDestroyedEvent['params']
|
||||||
) {
|
) {
|
||||||
@ -61,7 +68,7 @@ export class BrowserContext extends BrowserContextBase {
|
|||||||
const {result} = await this.#connection.send('browsingContext.create', {
|
const {result} = await this.#connection.send('browsingContext.create', {
|
||||||
type: 'tab',
|
type: 'tab',
|
||||||
});
|
});
|
||||||
const page = new Page(this.#connection, result);
|
const page = new Page(this, result);
|
||||||
if (this.#defaultViewport) {
|
if (this.#defaultViewport) {
|
||||||
try {
|
try {
|
||||||
await page.setViewport(this.#defaultViewport);
|
await page.setViewport(this.#defaultViewport);
|
||||||
@ -83,4 +90,8 @@ export class BrowserContext extends BrowserContextBase {
|
|||||||
}
|
}
|
||||||
this.#pages.clear();
|
this.#pages.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override browser(): Browser {
|
||||||
|
return this.#browser;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,6 +44,8 @@ import {
|
|||||||
withSourcePuppeteerURLIfNone,
|
withSourcePuppeteerURLIfNone,
|
||||||
} from '../util.js';
|
} from '../util.js';
|
||||||
|
|
||||||
|
import {Browser} from './Browser.js';
|
||||||
|
import {BrowserContext} from './BrowserContext.js';
|
||||||
import {BrowsingContext, getBidiHandle} from './BrowsingContext.js';
|
import {BrowsingContext, getBidiHandle} from './BrowsingContext.js';
|
||||||
import {Connection} from './Connection.js';
|
import {Connection} from './Connection.js';
|
||||||
import {Frame} from './Frame.js';
|
import {Frame} from './Frame.js';
|
||||||
@ -57,6 +59,7 @@ import {BidiSerializer} from './Serializer.js';
|
|||||||
*/
|
*/
|
||||||
export class Page extends PageBase {
|
export class Page extends PageBase {
|
||||||
#timeoutSettings = new TimeoutSettings();
|
#timeoutSettings = new TimeoutSettings();
|
||||||
|
#browserContext: BrowserContext;
|
||||||
#connection: Connection;
|
#connection: Connection;
|
||||||
#frameTree = new FrameTree<Frame>();
|
#frameTree = new FrameTree<Frame>();
|
||||||
#networkManager: NetworkManager;
|
#networkManager: NetworkManager;
|
||||||
@ -113,11 +116,12 @@ export class Page extends PageBase {
|
|||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
constructor(connection: Connection, info: {context: string}) {
|
constructor(browserContext: BrowserContext, info: {context: string}) {
|
||||||
super();
|
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({
|
this.#onFrameAttached({
|
||||||
...info,
|
...info,
|
||||||
url: 'about:blank',
|
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 {
|
override mainFrame(): Frame {
|
||||||
const mainFrame = this.#frameTree.getMainFrame();
|
const mainFrame = this.#frameTree.getMainFrame();
|
||||||
assert(mainFrame, 'Requesting main frame too early!');
|
assert(mainFrame, 'Requesting main frame too early!');
|
||||||
|
@ -137,6 +137,18 @@
|
|||||||
"parameters": ["webDriverBiDi"],
|
"parameters": ["webDriverBiDi"],
|
||||||
"expectations": ["PASS"]
|
"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 *",
|
"testIdPattern": "[page.spec] Page Page.Events.DOMContentLoaded *",
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
"platforms": ["darwin", "linux", "win32"],
|
||||||
|
Loading…
Reference in New Issue
Block a user