Skip to content

Commit a19b270

Browse files
authored
chore: extract BrowserContext to its own file (#9133)
1 parent 7aaa5f8 commit a19b270

File tree

13 files changed

+199
-173
lines changed

13 files changed

+199
-173
lines changed

packages/puppeteer-core/src/api/Browser.ts

Lines changed: 1 addition & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {Protocol} from 'devtools-protocol';
2121
import {EventEmitter} from '../common/EventEmitter.js';
2222
import type {Page} from './Page.js'; // TODO: move to ./api
2323
import type {Target} from '../common/Target.js'; // TODO: move to ./api
24+
import type {BrowserContext} from './BrowserContext.js';
2425

2526
/**
2627
* BrowserContext options.
@@ -466,163 +467,3 @@ export const enum BrowserContextEmittedEvents {
466467
*/
467468
TargetDestroyed = 'targetdestroyed',
468469
}
469-
470-
/**
471-
* BrowserContexts provide a way to operate multiple independent browser
472-
* sessions. When a browser is launched, it has a single BrowserContext used by
473-
* default. The method {@link Browser.newPage | Browser.newPage} creates a page
474-
* in the default browser context.
475-
*
476-
* @remarks
477-
*
478-
* The Browser class extends from Puppeteer's {@link EventEmitter} class and
479-
* will emit various events which are documented in the
480-
* {@link BrowserContextEmittedEvents} enum.
481-
*
482-
* If a page opens another page, e.g. with a `window.open` call, the popup will
483-
* belong to the parent page's browser context.
484-
*
485-
* Puppeteer allows creation of "incognito" browser contexts with
486-
* {@link Browser.createIncognitoBrowserContext | Browser.createIncognitoBrowserContext}
487-
* method. "Incognito" browser contexts don't write any browsing data to disk.
488-
*
489-
* @example
490-
*
491-
* ```ts
492-
* // Create a new incognito browser context
493-
* const context = await browser.createIncognitoBrowserContext();
494-
* // Create a new page inside context.
495-
* const page = await context.newPage();
496-
* // ... do stuff with page ...
497-
* await page.goto('https://example.com');
498-
* // Dispose context once it's no longer needed.
499-
* await context.close();
500-
* ```
501-
*
502-
* @public
503-
*/
504-
export class BrowserContext extends EventEmitter {
505-
/**
506-
* @internal
507-
*/
508-
constructor() {
509-
super();
510-
}
511-
512-
/**
513-
* An array of all active targets inside the browser context.
514-
*/
515-
targets(): Target[] {
516-
throw new Error('Not implemented');
517-
}
518-
519-
/**
520-
* This searches for a target in this specific browser context.
521-
*
522-
* @example
523-
* An example of finding a target for a page opened via `window.open`:
524-
*
525-
* ```ts
526-
* await page.evaluate(() => window.open('https://www.example.com/'));
527-
* const newWindowTarget = await browserContext.waitForTarget(
528-
* target => target.url() === 'https://www.example.com/'
529-
* );
530-
* ```
531-
*
532-
* @param predicate - A function to be run for every target
533-
* @param options - An object of options. Accepts a timout,
534-
* which is the maximum wait time in milliseconds.
535-
* Pass `0` to disable the timeout. Defaults to 30 seconds.
536-
* @returns Promise which resolves to the first target found
537-
* that matches the `predicate` function.
538-
*/
539-
waitForTarget(
540-
predicate: (x: Target) => boolean | Promise<boolean>,
541-
options?: {timeout?: number}
542-
): Promise<Target>;
543-
waitForTarget(): Promise<Target> {
544-
throw new Error('Not implemented');
545-
}
546-
547-
/**
548-
* An array of all pages inside the browser context.
549-
*
550-
* @returns Promise which resolves to an array of all open pages.
551-
* Non visible pages, such as `"background_page"`, will not be listed here.
552-
* You can find them using {@link Target.page | the target page}.
553-
*/
554-
pages(): Promise<Page[]> {
555-
throw new Error('Not implemented');
556-
}
557-
558-
/**
559-
* Returns whether BrowserContext is incognito.
560-
* The default browser context is the only non-incognito browser context.
561-
*
562-
* @remarks
563-
* The default browser context cannot be closed.
564-
*/
565-
isIncognito(): boolean {
566-
throw new Error('Not implemented');
567-
}
568-
569-
/**
570-
* @example
571-
*
572-
* ```ts
573-
* const context = browser.defaultBrowserContext();
574-
* await context.overridePermissions('https://html5demos.com', [
575-
* 'geolocation',
576-
* ]);
577-
* ```
578-
*
579-
* @param origin - The origin to grant permissions to, e.g. "https://example.com".
580-
* @param permissions - An array of permissions to grant.
581-
* All permissions that are not listed here will be automatically denied.
582-
*/
583-
overridePermissions(origin: string, permissions: Permission[]): Promise<void>;
584-
overridePermissions(): Promise<void> {
585-
throw new Error('Not implemented');
586-
}
587-
588-
/**
589-
* Clears all permission overrides for the browser context.
590-
*
591-
* @example
592-
*
593-
* ```ts
594-
* const context = browser.defaultBrowserContext();
595-
* context.overridePermissions('https://example.com', ['clipboard-read']);
596-
* // do stuff ..
597-
* context.clearPermissionOverrides();
598-
* ```
599-
*/
600-
clearPermissionOverrides(): Promise<void> {
601-
throw new Error('Not implemented');
602-
}
603-
604-
/**
605-
* Creates a new page in the browser context.
606-
*/
607-
newPage(): Promise<Page> {
608-
throw new Error('Not implemented');
609-
}
610-
611-
/**
612-
* The browser this browser context belongs to.
613-
*/
614-
browser(): Browser {
615-
throw new Error('Not implemented');
616-
}
617-
618-
/**
619-
* Closes the browser context. All the targets that belong to the browser context
620-
* will be closed.
621-
*
622-
* @remarks
623-
* Only incognito browser contexts can be closed.
624-
*/
625-
close(): Promise<void> {
626-
throw new Error('Not implemented');
627-
}
628-
}
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/**
2+
* Copyright 2017 Google Inc. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {EventEmitter} from '../common/EventEmitter.js';
18+
import {Page} from './Page.js';
19+
import {Target} from '../common/Target.js';
20+
import type {Permission, Browser} from './Browser.js';
21+
22+
/**
23+
* BrowserContexts provide a way to operate multiple independent browser
24+
* sessions. When a browser is launched, it has a single BrowserContext used by
25+
* default. The method {@link Browser.newPage | Browser.newPage} creates a page
26+
* in the default browser context.
27+
*
28+
* @remarks
29+
*
30+
* The Browser class extends from Puppeteer's {@link EventEmitter} class and
31+
* will emit various events which are documented in the
32+
* {@link BrowserContextEmittedEvents} enum.
33+
*
34+
* If a page opens another page, e.g. with a `window.open` call, the popup will
35+
* belong to the parent page's browser context.
36+
*
37+
* Puppeteer allows creation of "incognito" browser contexts with
38+
* {@link Browser.createIncognitoBrowserContext | Browser.createIncognitoBrowserContext}
39+
* method. "Incognito" browser contexts don't write any browsing data to disk.
40+
*
41+
* @example
42+
*
43+
* ```ts
44+
* // Create a new incognito browser context
45+
* const context = await browser.createIncognitoBrowserContext();
46+
* // Create a new page inside context.
47+
* const page = await context.newPage();
48+
* // ... do stuff with page ...
49+
* await page.goto('https://example.com');
50+
* // Dispose context once it's no longer needed.
51+
* await context.close();
52+
* ```
53+
*
54+
* @public
55+
*/
56+
57+
export class BrowserContext extends EventEmitter {
58+
/**
59+
* @internal
60+
*/
61+
constructor() {
62+
super();
63+
}
64+
65+
/**
66+
* An array of all active targets inside the browser context.
67+
*/
68+
targets(): Target[] {
69+
throw new Error('Not implemented');
70+
}
71+
72+
/**
73+
* This searches for a target in this specific browser context.
74+
*
75+
* @example
76+
* An example of finding a target for a page opened via `window.open`:
77+
*
78+
* ```ts
79+
* await page.evaluate(() => window.open('https://www.example.com/'));
80+
* const newWindowTarget = await browserContext.waitForTarget(
81+
* target => target.url() === 'https://www.example.com/'
82+
* );
83+
* ```
84+
*
85+
* @param predicate - A function to be run for every target
86+
* @param options - An object of options. Accepts a timout,
87+
* which is the maximum wait time in milliseconds.
88+
* Pass `0` to disable the timeout. Defaults to 30 seconds.
89+
* @returns Promise which resolves to the first target found
90+
* that matches the `predicate` function.
91+
*/
92+
waitForTarget(
93+
predicate: (x: Target) => boolean | Promise<boolean>,
94+
options?: {timeout?: number}
95+
): Promise<Target>;
96+
waitForTarget(): Promise<Target> {
97+
throw new Error('Not implemented');
98+
}
99+
100+
/**
101+
* An array of all pages inside the browser context.
102+
*
103+
* @returns Promise which resolves to an array of all open pages.
104+
* Non visible pages, such as `"background_page"`, will not be listed here.
105+
* You can find them using {@link Target.page | the target page}.
106+
*/
107+
pages(): Promise<Page[]> {
108+
throw new Error('Not implemented');
109+
}
110+
111+
/**
112+
* Returns whether BrowserContext is incognito.
113+
* The default browser context is the only non-incognito browser context.
114+
*
115+
* @remarks
116+
* The default browser context cannot be closed.
117+
*/
118+
isIncognito(): boolean {
119+
throw new Error('Not implemented');
120+
}
121+
122+
/**
123+
* @example
124+
*
125+
* ```ts
126+
* const context = browser.defaultBrowserContext();
127+
* await context.overridePermissions('https://html5demos.com', [
128+
* 'geolocation',
129+
* ]);
130+
* ```
131+
*
132+
* @param origin - The origin to grant permissions to, e.g. "https://example.com".
133+
* @param permissions - An array of permissions to grant.
134+
* All permissions that are not listed here will be automatically denied.
135+
*/
136+
overridePermissions(origin: string, permissions: Permission[]): Promise<void>;
137+
overridePermissions(): Promise<void> {
138+
throw new Error('Not implemented');
139+
}
140+
141+
/**
142+
* Clears all permission overrides for the browser context.
143+
*
144+
* @example
145+
*
146+
* ```ts
147+
* const context = browser.defaultBrowserContext();
148+
* context.overridePermissions('https://example.com', ['clipboard-read']);
149+
* // do stuff ..
150+
* context.clearPermissionOverrides();
151+
* ```
152+
*/
153+
clearPermissionOverrides(): Promise<void> {
154+
throw new Error('Not implemented');
155+
}
156+
157+
/**
158+
* Creates a new page in the browser context.
159+
*/
160+
newPage(): Promise<Page> {
161+
throw new Error('Not implemented');
162+
}
163+
164+
/**
165+
* The browser this browser context belongs to.
166+
*/
167+
browser(): Browser {
168+
throw new Error('Not implemented');
169+
}
170+
171+
/**
172+
* Closes the browser context. All the targets that belong to the browser context
173+
* will be closed.
174+
*
175+
* @remarks
176+
* Only incognito browser contexts can be closed.
177+
*/
178+
close(): Promise<void> {
179+
throw new Error('Not implemented');
180+
}
181+
}

packages/puppeteer-core/src/api/Page.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ import type {Target} from '../common/Target.js';
4848
import type {Tracing} from '../common/Tracing.js';
4949
import type {EvaluateFunc, HandleFor, NodeFor} from '../common/types.js';
5050
import type {WebWorker} from '../common/WebWorker.js';
51-
import type {Browser, BrowserContext} from './Browser.js';
51+
import type {Browser} from './Browser.js';
52+
import type {BrowserContext} from './BrowserContext.js';
5253

5354
/**
5455
* @public

packages/puppeteer-core/src/common/Browser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import {ChromeTargetManager} from './ChromeTargetManager.js';
2828
import {FirefoxTargetManager} from './FirefoxTargetManager.js';
2929
import {
3030
Browser as BrowserBase,
31-
BrowserContext,
3231
BrowserCloseCallback,
3332
TargetFilterCallback,
3433
IsPageTargetCallback,
@@ -39,6 +38,7 @@ import {
3938
WaitForTargetOptions,
4039
Permission,
4140
} from '../api/Browser.js';
41+
import {BrowserContext} from '../api/BrowserContext.js';
4242

4343
/**
4444
* @internal

packages/puppeteer-core/src/common/Page.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
import {Protocol} from 'devtools-protocol';
1818
import type {Readable} from 'stream';
19-
import type {Browser, BrowserContext} from '../api/Browser.js';
19+
import type {Browser} from '../api/Browser.js';
20+
import type {BrowserContext} from '../api/BrowserContext.js';
2021
import {
2122
GeolocationOptions,
2223
MediaFeature,

0 commit comments

Comments
 (0)