refactor!: rename createIncognitoBrowserContext to createBrowserContext (#11834)

This commit is contained in:
Alex Rudenko 2024-02-05 10:43:37 +01:00 committed by GitHub
parent c63414dbb4
commit 46a3ef2681
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
18 changed files with 117 additions and 115 deletions

View File

@ -7,10 +7,10 @@ sidebar_label: API
## Classes
| Class | Description |
| --------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| --------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [Accessibility](./puppeteer.accessibility.md) | The Accessibility class provides methods for inspecting the browser's accessibility tree. The accessibility tree is used by assistive technology such as [screen readers](https://en.wikipedia.org/wiki/Screen_reader) or [switches](https://en.wikipedia.org/wiki/Switch_access). |
| [Browser](./puppeteer.browser.md) | <p>[Browser](./puppeteer.browser.md) represents a browser instance that is either:</p><p>- connected to via [Puppeteer.connect()](./puppeteer.puppeteer.connect.md) or - launched by [PuppeteerNode.launch()](./puppeteer.puppeteernode.launch.md).</p><p>[Browser](./puppeteer.browser.md) [emits](./puppeteer.eventemitter.md) various events which are documented in the [BrowserEvent](./puppeteer.browserevent.md) enum.</p> |
| [BrowserContext](./puppeteer.browsercontext.md) | <p>[BrowserContext](./puppeteer.browsercontext.md) represents individual sessions within a [browser](./puppeteer.browser.md).</p><p>When a [browser](./puppeteer.browser.md) is launched, it has a single [browser context](./puppeteer.browsercontext.md) by default. Others can be created using [Browser.createIncognitoBrowserContext()](./puppeteer.browser.createincognitobrowsercontext.md).</p><p>[BrowserContext](./puppeteer.browsercontext.md) [emits](./puppeteer.eventemitter.md) various events which are documented in the [BrowserContextEvent](./puppeteer.browsercontextevent.md) enum.</p><p>If a [page](./puppeteer.page.md) opens another [page](./puppeteer.page.md), e.g. using <code>window.open</code>, the popup will belong to the parent [page's browser context](./puppeteer.page.browsercontext.md).</p> |
| [BrowserContext](./puppeteer.browsercontext.md) | <p>[BrowserContext](./puppeteer.browsercontext.md) represents individual user contexts within a [browser](./puppeteer.browser.md).</p><p>When a [browser](./puppeteer.browser.md) is launched, it has a single [browser context](./puppeteer.browsercontext.md) by default. Others can be created using [Browser.createBrowserContext()](./puppeteer.browser.createbrowsercontext.md). Each context has isolated storage (cookies/localStorage/etc.)</p><p>[BrowserContext](./puppeteer.browsercontext.md) [emits](./puppeteer.eventemitter.md) various events which are documented in the [BrowserContextEvent](./puppeteer.browsercontextevent.md) enum.</p><p>If a [page](./puppeteer.page.md) opens another [page](./puppeteer.page.md), e.g. using <code>window.open</code>, the popup will belong to the parent [page's browser context](./puppeteer.page.browsercontext.md).</p> |
| [CDPSession](./puppeteer.cdpsession.md) | The <code>CDPSession</code> instances are used to talk raw Chrome Devtools Protocol. |
| [Connection](./puppeteer.connection.md) | |
| [ConsoleMessage](./puppeteer.consolemessage.md) | ConsoleMessage objects are dispatched by page via the 'console' event. |

View File

@ -1,10 +1,10 @@
---
sidebar_label: Browser.createIncognitoBrowserContext
sidebar_label: Browser.createBrowserContext
---
# Browser.createIncognitoBrowserContext() method
# Browser.createBrowserContext() method
Creates a new incognito [browser context](./puppeteer.browsercontext.md).
Creates a new [browser context](./puppeteer.browsercontext.md).
This won't share cookies/cache with other [browser contexts](./puppeteer.browsercontext.md).
@ -12,7 +12,7 @@ This won't share cookies/cache with other [browser contexts](./puppeteer.browser
```typescript
class Browser {
abstract createIncognitoBrowserContext(
abstract createBrowserContext(
options?: BrowserContextOptions
): Promise<BrowserContext>;
}
@ -34,8 +34,8 @@ Promise&lt;[BrowserContext](./puppeteer.browsercontext.md)&gt;
import puppeteer from 'puppeteer';
const browser = await puppeteer.launch();
// Create a new incognito browser context.
const context = await browser.createIncognitoBrowserContext();
// Create a new browser context.
const context = await browser.createBrowserContext();
// Create a new page in a pristine context.
const page = await context.newPage();
// Do stuff

View File

@ -64,10 +64,10 @@ await browser2.close();
## Methods
| Method | Modifiers | Description |
| ---------------------------------------------------------------------------------------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| ---------------------------------------------------------------------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [browserContexts()](./puppeteer.browser.browsercontexts.md) | | <p>Gets a list of open [browser contexts](./puppeteer.browsercontext.md).</p><p>In a newly-created [browser](./puppeteer.browser.md), this will return a single instance of [BrowserContext](./puppeteer.browsercontext.md).</p> |
| [close()](./puppeteer.browser.close.md) | | Closes this [browser](./puppeteer.browser.md) and all associated [pages](./puppeteer.page.md). |
| [createIncognitoBrowserContext(options)](./puppeteer.browser.createincognitobrowsercontext.md) | | <p>Creates a new incognito [browser context](./puppeteer.browsercontext.md).</p><p>This won't share cookies/cache with other [browser contexts](./puppeteer.browsercontext.md).</p> |
| [createBrowserContext(options)](./puppeteer.browser.createbrowsercontext.md) | | <p>Creates a new [browser context](./puppeteer.browsercontext.md).</p><p>This won't share cookies/cache with other [browser contexts](./puppeteer.browsercontext.md).</p> |
| [defaultBrowserContext()](./puppeteer.browser.defaultbrowsercontext.md) | | Gets the default [browser context](./puppeteer.browsercontext.md). |
| [disconnect()](./puppeteer.browser.disconnect.md) | | Disconnects Puppeteer from this [browser](./puppeteer.browser.md), but leaves the process running. |
| [isConnected()](./puppeteer.browser.isconnected.md) | | Whether Puppeteer is connected to this [browser](./puppeteer.browser.md). |

View File

@ -6,7 +6,7 @@ sidebar_label: BrowserContext.isIncognito
Whether this [browser context](./puppeteer.browsercontext.md) is incognito.
The [default browser context](./puppeteer.browser.defaultbrowsercontext.md) is the only non-incognito browser context.
In Chrome, the [default browser context](./puppeteer.browser.defaultbrowsercontext.md) is the only non-incognito browser context.
#### Signature:

View File

@ -4,9 +4,9 @@ sidebar_label: BrowserContext
# BrowserContext class
[BrowserContext](./puppeteer.browsercontext.md) represents individual sessions within a [browser](./puppeteer.browser.md).
[BrowserContext](./puppeteer.browsercontext.md) represents individual user contexts within a [browser](./puppeteer.browser.md).
When a [browser](./puppeteer.browser.md) is launched, it has a single [browser context](./puppeteer.browsercontext.md) by default. Others can be created using [Browser.createIncognitoBrowserContext()](./puppeteer.browser.createincognitobrowsercontext.md).
When a [browser](./puppeteer.browser.md) is launched, it has a single [browser context](./puppeteer.browsercontext.md) by default. Others can be created using [Browser.createBrowserContext()](./puppeteer.browser.createbrowsercontext.md). Each context has isolated storage (cookies/localStorage/etc.)
[BrowserContext](./puppeteer.browsercontext.md) [emits](./puppeteer.eventemitter.md) various events which are documented in the [BrowserContextEvent](./puppeteer.browsercontextevent.md) enum.
@ -26,11 +26,11 @@ The constructor for this class is marked as internal. Third-party code should no
## Example
Creating an incognito [browser context](./puppeteer.browsercontext.md):
Creating a new [browser context](./puppeteer.browsercontext.md):
```ts
// Create a new incognito browser context
const context = await browser.createIncognitoBrowserContext();
// Create a new browser context
const context = await browser.createBrowserContext();
// Create a new page inside context.
const page = await context.newPage();
// ... do stuff with page ...
@ -49,11 +49,11 @@ await context.close();
## Methods
| Method | Modifiers | Description |
| --------------------------------------------------------------------------------------------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| --------------------------------------------------------------------------------------------- | --------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [browser()](./puppeteer.browsercontext.browser.md) | | Gets the [browser](./puppeteer.browser.md) associated with this [browser context](./puppeteer.browsercontext.md). |
| [clearPermissionOverrides()](./puppeteer.browsercontext.clearpermissionoverrides.md) | | Clears all permission overrides for this [browser context](./puppeteer.browsercontext.md). |
| [close()](./puppeteer.browsercontext.close.md) | | Closes this [browser context](./puppeteer.browsercontext.md) and all associated [pages](./puppeteer.page.md). |
| [isIncognito()](./puppeteer.browsercontext.isincognito.md) | | <p>Whether this [browser context](./puppeteer.browsercontext.md) is incognito.</p><p>The [default browser context](./puppeteer.browser.defaultbrowsercontext.md) is the only non-incognito browser context.</p> |
| [isIncognito()](./puppeteer.browsercontext.isincognito.md) | | <p>Whether this [browser context](./puppeteer.browsercontext.md) is incognito.</p><p>In Chrome, the [default browser context](./puppeteer.browser.defaultbrowsercontext.md) is the only non-incognito browser context.</p> |
| [newPage()](./puppeteer.browsercontext.newpage.md) | | Creates a new [page](./puppeteer.page.md) in this [browser context](./puppeteer.browsercontext.md). |
| [overridePermissions(origin, permissions)](./puppeteer.browsercontext.overridepermissions.md) | | Grants this [browser context](./puppeteer.browsercontext.md) the given <code>permissions</code> within the given <code>origin</code>. |
| [pages()](./puppeteer.browsercontext.pages.md) | | Gets a list of all open [pages](./puppeteer.page.md) inside this [browser context](./puppeteer.browsercontext.md). |

View File

@ -136,7 +136,7 @@ export const enum BrowserEvent {
* Emitted when the URL of a target changes. Contains a {@link Target}
* instance.
*
* @remarks Note that this includes target changes in incognito browser
* @remarks Note that this includes target changes in all browser
* contexts.
*/
TargetChanged = 'targetchanged',
@ -147,7 +147,7 @@ export const enum BrowserEvent {
*
* Contains a {@link Target} instance.
*
* @remarks Note that this includes target creations in incognito browser
* @remarks Note that this includes target creations in all browser
* contexts.
*/
TargetCreated = 'targetcreated',
@ -155,7 +155,7 @@ export const enum BrowserEvent {
* Emitted when a target is destroyed, for example when a page is closed.
* Contains a {@link Target} instance.
*
* @remarks Note that this includes target destructions in incognito browser
* @remarks Note that this includes target destructions in all browser
* contexts.
*/
TargetDestroyed = 'targetdestroyed',
@ -244,7 +244,7 @@ export abstract class Browser extends EventEmitter<BrowserEvents> {
abstract process(): ChildProcess | null;
/**
* Creates a new incognito {@link BrowserContext | browser context}.
* Creates a new {@link BrowserContext | browser context}.
*
* This won't share cookies/cache with other {@link BrowserContext | browser contexts}.
*
@ -254,15 +254,15 @@ export abstract class Browser extends EventEmitter<BrowserEvents> {
* import puppeteer from 'puppeteer';
*
* const browser = await puppeteer.launch();
* // Create a new incognito browser context.
* const context = await browser.createIncognitoBrowserContext();
* // Create a new browser context.
* const context = await browser.createBrowserContext();
* // Create a new page in a pristine context.
* const page = await context.newPage();
* // Do stuff
* await page.goto('https://example.com');
* ```
*/
abstract createIncognitoBrowserContext(
abstract createBrowserContext(
options?: BrowserContextOptions
): Promise<BrowserContext>;

View File

@ -48,12 +48,13 @@ export interface BrowserContextEvents extends Record<EventType, unknown> {
}
/**
* {@link BrowserContext} represents individual sessions within a
* {@link BrowserContext} represents individual user contexts within a
* {@link Browser | browser}.
*
* When a {@link Browser | browser} is launched, it has a single
* {@link BrowserContext | browser context} by default. Others can be created
* using {@link Browser.createIncognitoBrowserContext}.
* using {@link Browser.createBrowserContext}. Each context has isolated storage
* (cookies/localStorage/etc.)
*
* {@link BrowserContext} {@link EventEmitter | emits} various events which are
* documented in the {@link BrowserContextEvent} enum.
@ -62,11 +63,11 @@ export interface BrowserContextEvents extends Record<EventType, unknown> {
* `window.open`, the popup will belong to the parent {@link Page.browserContext
* | page's browser context}.
*
* @example Creating an incognito {@link BrowserContext | browser context}:
* @example Creating a new {@link BrowserContext | browser context}:
*
* ```ts
* // Create a new incognito browser context
* const context = await browser.createIncognitoBrowserContext();
* // Create a new browser context
* const context = await browser.createBrowserContext();
* // Create a new page inside context.
* const page = await context.newPage();
* // ... do stuff with page ...
@ -124,8 +125,9 @@ export abstract class BrowserContext extends EventEmitter<BrowserContextEvents>
/**
* Whether this {@link BrowserContext | browser context} is incognito.
*
* The {@link Browser.defaultBrowserContext | default browser context} is the
* only non-incognito browser context.
* In Chrome, the
* {@link Browser.defaultBrowserContext | default browser context} is the only
* non-incognito browser context.
*/
abstract isIncognito(): boolean;

View File

@ -262,7 +262,7 @@ export class BidiBrowser extends Browser {
return this.#process ?? null;
}
override async createIncognitoBrowserContext(
override async createBrowserContext(
_options?: BrowserContextOptions
): Promise<BidiBrowserContext> {
const userContext = await this.#browserCore.createUserContext();

View File

@ -201,7 +201,7 @@ export class CdpBrowser extends BrowserBase {
return this.#isPageTargetCallback;
}
override async createIncognitoBrowserContext(
override async createBrowserContext(
options: BrowserContextOptions = {}
): Promise<CdpBrowserContext> {
const {proxyServer, proxyBypassList} = options;

View File

@ -34,7 +34,7 @@ describe('BrowserContext', function () {
});
expect(browser.browserContexts()).toHaveLength(1);
const context = await browser.createIncognitoBrowserContext();
const context = await browser.createBrowserContext();
expect(context.isIncognito()).toBe(true);
expect(browser.browserContexts()).toHaveLength(2);
expect(browser.browserContexts().indexOf(context) !== -1).toBe(true);
@ -48,7 +48,7 @@ describe('BrowserContext', function () {
expect(await browser.pages()).toHaveLength(1);
const context = await browser.createIncognitoBrowserContext();
const context = await browser.createBrowserContext();
await context.newPage();
expect(await browser.pages()).toHaveLength(2);
expect(await context.pages()).toHaveLength(1);
@ -128,7 +128,7 @@ describe('BrowserContext', function () {
it('should timeout waiting for a non-existent target', async () => {
const {browser, server} = await getTestState();
const context = await browser.createIncognitoBrowserContext();
const context = await browser.createBrowserContext();
const error = await context
.waitForTarget(
target => {
@ -151,8 +151,8 @@ describe('BrowserContext', function () {
});
// Create two incognito contexts.
const context1 = await browser.createIncognitoBrowserContext();
const context2 = await browser.createIncognitoBrowserContext();
const context1 = await browser.createBrowserContext();
const context2 = await browser.createBrowserContext();
expect(context1.targets()).toHaveLength(0);
expect(context2.targets()).toHaveLength(0);
@ -213,7 +213,7 @@ describe('BrowserContext', function () {
});
expect(browser.browserContexts()).toHaveLength(1);
const context = await browser.createIncognitoBrowserContext();
const context = await browser.createBrowserContext();
try {
expect(browser.browserContexts()).toHaveLength(2);
const remoteBrowser = await puppeteer.connect({
@ -236,7 +236,7 @@ describe('BrowserContext', function () {
expect(browser.browserContexts()).toHaveLength(1);
expect(browser.browserContexts()[0]!.id).toBeUndefined();
const context = await browser.createIncognitoBrowserContext();
const context = await browser.createBrowserContext();
expect(browser.browserContexts()).toHaveLength(2);
expect(browser.browserContexts()[1]!.id).toBeDefined();
await context.close();
@ -336,7 +336,7 @@ describe('BrowserContext', function () {
const {page, server, context, browser} = await getTestState();
await page.goto(server.EMPTY_PAGE);
const otherContext = await browser.createIncognitoBrowserContext();
const otherContext = await browser.createBrowserContext();
const otherPage = await otherContext.newPage();
await otherPage.goto(server.EMPTY_PAGE);
expect(await getPermission(page, 'geolocation')).toBe('prompt');

View File

@ -93,7 +93,7 @@ describe('DevTools', function () {
const browser = await launchBrowser(
Object.assign({devtools: true}, launchOptions)
);
const context = await browser.createIncognitoBrowserContext();
const context = await browser.createBrowserContext();
await Promise.all([
context.newPage(),
browser.waitForTarget((target: {url: () => string | string[]}) => {
@ -106,7 +106,7 @@ describe('DevTools', function () {
const browser = await launchBrowser(
Object.assign({devtools: true}, launchOptions)
);
const context = await browser.createIncognitoBrowserContext();
const context = await browser.createBrowserContext();
const [target] = await Promise.all([
browser.waitForTarget((target: {url: () => string | string[]}) => {
return target.url().includes('devtools://');

View File

@ -224,7 +224,7 @@ describe('Cookie specs', () => {
it('should isolate cookies in browser contexts', async () => {
const {page, server, browser} = await getTestState();
const anotherContext = await browser.createIncognitoBrowserContext();
const anotherContext = await browser.createBrowserContext();
const anotherPage = await anotherContext.newPage();
await page.goto(server.EMPTY_PAGE);

View File

@ -28,7 +28,7 @@ describe('device request prompt', function () {
});
beforeEach(async () => {
state.context = await state.browser.createIncognitoBrowserContext();
state.context = await state.browser.createBrowserContext();
state.page = await state.context.newPage();
});

View File

@ -32,7 +32,7 @@ describe('ignoreHTTPSErrors', function () {
});
beforeEach(async () => {
state.context = await state.browser.createIncognitoBrowserContext();
state.context = await state.browser.createBrowserContext();
state.page = await state.context.newPage();
});

View File

@ -216,7 +216,7 @@ export const getTestState = async (
}
if (!skipContextCreation) {
state.context = await state.browser!.createIncognitoBrowserContext();
state.context = await state.browser!.createBrowserContext();
state.page = await state.context.newPage();
}
return state as PuppeteerTestState;
@ -479,7 +479,7 @@ export const launch = async (
let context: BrowserContext;
let page: Page;
if (createContext) {
context = await browser.createIncognitoBrowserContext();
context = await browser.createBrowserContext();
cleanupStorage.push(() => {
return context.close();
});

View File

@ -33,7 +33,7 @@ describe('OOPIF', function () {
});
beforeEach(async () => {
state.context = await state.browser.createIncognitoBrowserContext();
state.context = await state.browser.createBrowserContext();
state.page = await state.context.newPage();
});

View File

@ -150,7 +150,7 @@ describe('request proxy', () => {
args: [...defaultArgs, `--proxy-server=${proxyServerUrl}`],
});
try {
const context = await browser.createIncognitoBrowserContext();
const context = await browser.createBrowserContext();
const page = await context.newPage();
const response = (await page.goto(emptyPageUrl))!;
@ -174,7 +174,7 @@ describe('request proxy', () => {
],
});
try {
const context = await browser.createIncognitoBrowserContext();
const context = await browser.createBrowserContext();
const page = await context.newPage();
const response = (await page.goto(emptyPageUrl))!;
@ -197,7 +197,7 @@ describe('request proxy', () => {
args: defaultArgs,
});
try {
const context = await browser.createIncognitoBrowserContext({
const context = await browser.createBrowserContext({
proxyServer: proxyServerUrl,
});
const page = await context.newPage();
@ -219,7 +219,7 @@ describe('request proxy', () => {
args: defaultArgs,
});
try {
const context = await browser.createIncognitoBrowserContext({
const context = await browser.createBrowserContext({
proxyServer: proxyServerUrl,
proxyBypassList: [new URL(emptyPageUrl).host],
});

View File

@ -361,7 +361,7 @@ describe('Screenshots', function () {
it('should run in parallel in multiple pages', async () => {
const {browser, server} = await getTestState();
const context = await browser.createIncognitoBrowserContext();
const context = await browser.createBrowserContext();
const N = 2;
const pages = await Promise.all(