From 2292078969fa46a27d5759989cd44a4d48beb310 Mon Sep 17 00:00:00 2001 From: jrandolf <101637635+jrandolf@users.noreply.github.com> Date: Mon, 18 Sep 2023 20:11:55 +0200 Subject: [PATCH] feat: implement `BrowserContext.closed` (#10928) --- docs/api/index.md | 72 ++++----- docs/api/puppeteer.browsercontext.browser.md | 4 +- ...browsercontext.clearpermissionoverrides.md | 4 +- docs/api/puppeteer.browsercontext.close.md | 6 +- .../puppeteer.browsercontext.isincognito.md | 10 +- docs/api/puppeteer.browsercontext.md | 47 +++--- docs/api/puppeteer.browsercontext.newpage.md | 4 +- ...teer.browsercontext.overridepermissions.md | 4 + docs/api/puppeteer.browsercontext.pages.md | 8 +- docs/api/puppeteer.browsercontext.targets.md | 2 +- .../puppeteer.browsercontext.waitfortarget.md | 18 +-- .../puppeteer-core/src/api/BrowserContext.ts | 139 ++++++++++-------- 12 files changed, 168 insertions(+), 150 deletions(-) diff --git a/docs/api/index.md b/docs/api/index.md index 24b13550..2fd07171 100644 --- a/docs/api/index.md +++ b/docs/api/index.md @@ -6,42 +6,42 @@ 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) |
[Browser](./puppeteer.browser.md) represents a browser instance that is either:
- connected to via [Puppeteer.connect()](./puppeteer.puppeteer.connect.md) or - launched by [PuppeteerNode.launch()](./puppeteer.puppeteernode.launch.md).
[Browser](./puppeteer.browser.md) [emits](./puppeteer.eventemitter.md) various events which are documented in the [BrowserEvent](./puppeteer.browserevent.md) enum.
| -| [BrowserContext](./puppeteer.browsercontext.md) | BrowserContexts provide a way to operate multiple independent browser sessions. When a browser is launched, it has a single BrowserContext used by default. The method [Browser.newPage](./puppeteer.browser.newpage.md) creates a page in the default browser context. | -| [CDPSession](./puppeteer.cdpsession.md) | TheCDPSession
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. |
-| [Coverage](./puppeteer.coverage.md) | The Coverage class provides methods to gather information about parts of JavaScript and CSS that were used by the page. |
-| [CSSCoverage](./puppeteer.csscoverage.md) | |
-| [CustomError](./puppeteer.customerror.md) | |
-| [DeviceRequestPrompt](./puppeteer.devicerequestprompt.md) | Device request prompts let you respond to the page requesting for a device through an API like WebBluetooth. |
-| [DeviceRequestPromptDevice](./puppeteer.devicerequestpromptdevice.md) | Device in a request prompt. |
-| [Dialog](./puppeteer.dialog.md) | Dialog instances are dispatched by the [Page](./puppeteer.page.md) via the dialog
event. |
-| [ElementHandle](./puppeteer.elementhandle.md) | ElementHandle represents an in-page DOM element. |
-| [EventEmitter](./puppeteer.eventemitter.md) | The EventEmitter class that many Puppeteer classes extend. |
-| [FileChooser](./puppeteer.filechooser.md) | File choosers let you react to the page requesting for a file. |
-| [Frame](./puppeteer.frame.md) | Represents a DOM frame.
To understand frames, you can think of frames as <iframe>
elements. Just like iframes, frames can be nested, and when JavaScript is executed in a frame, the JavaScript does not effect frames inside the ambient frame the JavaScript executes in.
Represents a reference to a JavaScript object. Instances can be created using [Page.evaluateHandle()](./puppeteer.page.evaluatehandle.md).
Handles prevent the referenced JavaScript object from being garbage-collected unless the handle is purposely [disposed](./puppeteer.jshandle.dispose.md). JSHandles are auto-disposed when their associated frame is navigated away or the parent context gets destroyed.
Handles can be used as arguments for any evaluation function such as [Page.$eval()](./puppeteer.page._eval.md), [Page.evaluate()](./puppeteer.page.evaluate.md), and [Page.evaluateHandle()](./puppeteer.page.evaluatehandle.md). They are resolved to their referenced object.
| -| [Keyboard](./puppeteer.keyboard.md) | Keyboard provides an api for managing a virtual keyboard. The high level api is [Keyboard.type()](./puppeteer.keyboard.type.md), which takes raw characters and generates proper keydown, keypress/input, and keyup events on your page. | -| [Locator](./puppeteer.locator.md) | Locators describe a strategy of locating objects and performing an action on them. If the action fails because the object is not ready for the action, the whole operation is retried. Various preconditions for a successful action are checked automatically. | -| [Mouse](./puppeteer.mouse.md) | The Mouse class operates in main-frame CSS pixels relative to the top-left corner of the viewport. | -| [Page](./puppeteer.page.md) |Page provides methods to interact with a single tab or [extension background page](https://developer.chrome.com/extensions/background_pages) in the browser.
:::note
One Browser instance might have multiple Page instances.
:::
| -| [ProductLauncher](./puppeteer.productlauncher.md) | Describes a launcher - a class that is able to create and launch a browser instance. | -| [ProtocolError](./puppeteer.protocolerror.md) | ProtocolError is emitted whenever there is an error from the protocol. | -| [Puppeteer](./puppeteer.puppeteer.md) |The main Puppeteer class.
IMPORTANT: if you are using Puppeteer in a Node environment, you will get an instance of [PuppeteerNode](./puppeteer.puppeteernode.md) when you import or require puppeteer
. That class extends Puppeteer
, so has all the methods documented below as well as all that are defined on [PuppeteerNode](./puppeteer.puppeteernode.md).
Extends the main [Puppeteer](./puppeteer.puppeteer.md) class with Node specific behaviour for fetching and downloading browsers.
If you're using Puppeteer in a Node environment, this is the class you'll get when you run require('puppeteer')
(or the equivalent ES import
).
[Browser](./puppeteer.browser.md) represents a browser instance that is either:
- connected to via [Puppeteer.connect()](./puppeteer.puppeteer.connect.md) or - launched by [PuppeteerNode.launch()](./puppeteer.puppeteernode.launch.md).
[Browser](./puppeteer.browser.md) [emits](./puppeteer.eventemitter.md) various events which are documented in the [BrowserEvent](./puppeteer.browserevent.md) enum.
| +| [BrowserContext](./puppeteer.browsercontext.md) |[BrowserContext](./puppeteer.browsercontext.md) represents individual sessions 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).
[BrowserContext](./puppeteer.browsercontext.md) [emits](./puppeteer.eventemitter.md) various events which are documented in the [BrowserContextEvent](./puppeteer.browsercontextevent.md) enum.
If a [page](./puppeteer.page.md) opens another [page](./puppeteer.page.md), e.g. using window.open
, the popup will belong to the parent [page's browser context](./puppeteer.page.browsercontext.md).
CDPSession
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. |
+| [Coverage](./puppeteer.coverage.md) | The Coverage class provides methods to gather information about parts of JavaScript and CSS that were used by the page. |
+| [CSSCoverage](./puppeteer.csscoverage.md) | |
+| [CustomError](./puppeteer.customerror.md) | |
+| [DeviceRequestPrompt](./puppeteer.devicerequestprompt.md) | Device request prompts let you respond to the page requesting for a device through an API like WebBluetooth. |
+| [DeviceRequestPromptDevice](./puppeteer.devicerequestpromptdevice.md) | Device in a request prompt. |
+| [Dialog](./puppeteer.dialog.md) | Dialog instances are dispatched by the [Page](./puppeteer.page.md) via the dialog
event. |
+| [ElementHandle](./puppeteer.elementhandle.md) | ElementHandle represents an in-page DOM element. |
+| [EventEmitter](./puppeteer.eventemitter.md) | The EventEmitter class that many Puppeteer classes extend. |
+| [FileChooser](./puppeteer.filechooser.md) | File choosers let you react to the page requesting for a file. |
+| [Frame](./puppeteer.frame.md) | Represents a DOM frame.
To understand frames, you can think of frames as <iframe>
elements. Just like iframes, frames can be nested, and when JavaScript is executed in a frame, the JavaScript does not effect frames inside the ambient frame the JavaScript executes in.
Represents a reference to a JavaScript object. Instances can be created using [Page.evaluateHandle()](./puppeteer.page.evaluatehandle.md).
Handles prevent the referenced JavaScript object from being garbage-collected unless the handle is purposely [disposed](./puppeteer.jshandle.dispose.md). JSHandles are auto-disposed when their associated frame is navigated away or the parent context gets destroyed.
Handles can be used as arguments for any evaluation function such as [Page.$eval()](./puppeteer.page._eval.md), [Page.evaluate()](./puppeteer.page.evaluate.md), and [Page.evaluateHandle()](./puppeteer.page.evaluatehandle.md). They are resolved to their referenced object.
| +| [Keyboard](./puppeteer.keyboard.md) | Keyboard provides an api for managing a virtual keyboard. The high level api is [Keyboard.type()](./puppeteer.keyboard.type.md), which takes raw characters and generates proper keydown, keypress/input, and keyup events on your page. | +| [Locator](./puppeteer.locator.md) | Locators describe a strategy of locating objects and performing an action on them. If the action fails because the object is not ready for the action, the whole operation is retried. Various preconditions for a successful action are checked automatically. | +| [Mouse](./puppeteer.mouse.md) | The Mouse class operates in main-frame CSS pixels relative to the top-left corner of the viewport. | +| [Page](./puppeteer.page.md) |Page provides methods to interact with a single tab or [extension background page](https://developer.chrome.com/extensions/background_pages) in the browser.
:::note
One Browser instance might have multiple Page instances.
:::
| +| [ProductLauncher](./puppeteer.productlauncher.md) | Describes a launcher - a class that is able to create and launch a browser instance. | +| [ProtocolError](./puppeteer.protocolerror.md) | ProtocolError is emitted whenever there is an error from the protocol. | +| [Puppeteer](./puppeteer.puppeteer.md) |The main Puppeteer class.
IMPORTANT: if you are using Puppeteer in a Node environment, you will get an instance of [PuppeteerNode](./puppeteer.puppeteernode.md) when you import or require puppeteer
. That class extends Puppeteer
, so has all the methods documented below as well as all that are defined on [PuppeteerNode](./puppeteer.puppeteernode.md).
Extends the main [Puppeteer](./puppeteer.puppeteer.md) class with Node specific behaviour for fetching and downloading browsers.
If you're using Puppeteer in a Node environment, this is the class you'll get when you run require('puppeteer')
(or the equivalent ES import
).
readonly
| string \| undefined | |
+| Property | Modifiers | Type | Description |
+| -------- | --------------------- | ------------------- | ------------------------------------------------------------------------ |
+| closed | readonly
| boolean | Whether this [browser context](./puppeteer.browsercontext.md) is closed. |
+| id | readonly
| string \| undefined | Identifier for this [browser context](./puppeteer.browsercontext.md). |
## Methods
-| Method | Modifiers | Description |
-| --------------------------------------------------------------------------------------------- | --------- | ------------------------------------------------------------------------------------------------------------------- |
-| [browser()](./puppeteer.browsercontext.browser.md) | | The browser this browser context belongs to. |
-| [clearPermissionOverrides()](./puppeteer.browsercontext.clearpermissionoverrides.md) | | Clears all permission overrides for the browser context. |
-| [close()](./puppeteer.browsercontext.close.md) | | Closes the browser context. All the targets that belong to the browser context will be closed. |
-| [isIncognito()](./puppeteer.browsercontext.isincognito.md) | | Returns whether BrowserContext is incognito. The default browser context is the only non-incognito browser context. |
-| [newPage()](./puppeteer.browsercontext.newpage.md) | | Creates a new page in the browser context. |
-| [overridePermissions(origin, permissions)](./puppeteer.browsercontext.overridepermissions.md) | | |
-| [pages()](./puppeteer.browsercontext.pages.md) | | An array of all pages inside the browser context. |
-| [targets()](./puppeteer.browsercontext.targets.md) | | An array of all active targets inside the browser context. |
-| [waitForTarget(predicate, options)](./puppeteer.browsercontext.waitfortarget.md) | | This searches for a target in this specific browser context. |
+| 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) | | Whether this [browser context](./puppeteer.browsercontext.md) is incognito.
The [default browser context](./puppeteer.browser.defaultbrowsercontext.md) is the only non-incognito browser context.
| +| [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 givenpermissions
within the given origin
. |
+| [pages()](./puppeteer.browsercontext.pages.md) | | Gets a list of all open [pages](./puppeteer.page.md) inside this [browser context](./puppeteer.browsercontext.md). |
+| [targets()](./puppeteer.browsercontext.targets.md) | | Gets all active [targets](./puppeteer.target.md) inside this [browser context](./puppeteer.browsercontext.md). |
+| [waitForTarget(predicate, options)](./puppeteer.browsercontext.waitfortarget.md) | | Waits until a [target](./puppeteer.target.md) matching the given predicate
appears and returns it.
This will look all open [browser contexts](./puppeteer.browsercontext.md).
| diff --git a/docs/api/puppeteer.browsercontext.newpage.md b/docs/api/puppeteer.browsercontext.newpage.md index 83c1da98..351bd977 100644 --- a/docs/api/puppeteer.browsercontext.newpage.md +++ b/docs/api/puppeteer.browsercontext.newpage.md @@ -4,13 +4,13 @@ sidebar_label: BrowserContext.newPage # BrowserContext.newPage() method -Creates a new page in the browser context. +Creates a new [page](./puppeteer.page.md) in this [browser context](./puppeteer.browsercontext.md). #### Signature: ```typescript class BrowserContext { - newPage(): Promise0
to disable the timeout. Defaults to 30 seconds. |
+| Parameter | Type | Description |
+| --------- | ---------------------------------------------------------------------------- | ------------ |
+| predicate | (x: [Target](./puppeteer.target.md)) => boolean \| Promise<boolean> | |
+| options | { timeout?: number; } | _(Optional)_ |
**Returns:**
Promise<[Target](./puppeteer.target.md)>
-Promise which resolves to the first target found that matches the `predicate` function.
-
## Example
-An example of finding a target for a page opened via `window.open`:
+Finding a target for a page opened via `window.open`:
```ts
await page.evaluate(() => window.open('https://www.example.com/'));
diff --git a/packages/puppeteer-core/src/api/BrowserContext.ts b/packages/puppeteer-core/src/api/BrowserContext.ts
index bf22e598..4d27f8e9 100644
--- a/packages/puppeteer-core/src/api/BrowserContext.ts
+++ b/packages/puppeteer-core/src/api/BrowserContext.ts
@@ -15,6 +15,7 @@
*/
import {EventEmitter, type EventType} from '../common/EventEmitter.js';
+import {debugError} from '../common/util.js';
import type {Browser, Permission} from './Browser.js';
import {type Page} from './Page.js';
@@ -63,25 +64,21 @@ export interface BrowserContextEvents extends Record