BrowserPool <Options, BrowserPlugins, BrowserControllerReturn, LaunchContextReturn, PageOptions, PageReturn>
Hierarchy
- TypedEmitter<BrowserPoolEvents<BrowserControllerReturn, PageReturn>>
- BrowserPool
Index
Constructors
Properties
- activeBrowserControllers
- browserPlugins
- closeInactiveBrowserAfterMillis
- fingerprintCache
- fingerprintGenerator
- fingerprintInjector
- fingerprintOptions
- maxOpenPagesPerBrowser
- operationTimeoutMillis
- pageCounter
- pageIds
- pageToBrowserController
- pages
- postLaunchHooks
- postPageCloseHooks
- postPageCreateHooks
- preLaunchHooks
- prePageCloseHooks
- prePageCreateHooks
- retireBrowserAfterPageCount
- retiredBrowserControllers
- useFingerprints
- defaultMaxListeners
Methods
- addListener
- closeAllBrowsers
- destroy
- emit
- eventNames
- getBrowserControllerByPage
- getMaxListeners
- getPage
- getPageId
- listenerCount
- listeners
- newPage
- newPageInNewBrowser
- newPageWithEachPlugin
- off
- on
- once
- prependListener
- prependOnceListener
- rawListeners
- removeAllListeners
- removeListener
- retireAllBrowsers
- retireBrowserByPage
- retireBrowserController
- setMaxListeners
Constructors
constructor
Type parameters
- Options: BrowserPoolOptions<BrowserPlugin<CommonLibrary, unknown, CommonBrowser, unknown, CommonPage>, Options> = BrowserPoolOptions<BrowserPlugin<CommonLibrary, unknown, CommonBrowser, unknown, CommonPage>>
- BrowserPlugins: BrowserPlugin<CommonLibrary, unknown, CommonBrowser, unknown, CommonPage>[] = InferBrowserPluginArray<Options[browserPlugins], []>
- BrowserControllerReturn: BrowserController<CommonLibrary, unknown, CommonBrowser, unknown, CommonPage, BrowserControllerReturn> = ReturnType<BrowserPlugins[number][createController]>
- LaunchContextReturn: LaunchContext<CommonLibrary, unknown, CommonBrowser, unknown, CommonPage, LaunchContextReturn> = ReturnType<BrowserPlugins[number][createLaunchContext]>
- PageOptions = Parameters<BrowserControllerReturn[newPage]>[0]
- PageReturn: CommonPage = UnwrapPromise<ReturnType<BrowserControllerReturn[newPage]>>
Parameters
options: Options & BrowserPoolHooks<BrowserControllerReturn, LaunchContextReturn, PageReturn>
Returns BrowserPool<Options, BrowserPlugins, BrowserControllerReturn, LaunchContextReturn, PageOptions, PageReturn>
Properties
activeBrowserControllers
browserPlugins
closeInactiveBrowserAfterMillis
optionalfingerprintCache
optionalfingerprintGenerator
optionalfingerprintInjector
fingerprintOptions
maxOpenPagesPerBrowser
operationTimeoutMillis
pageCounter
pageIds
pageToBrowserController
pages
postLaunchHooks
postPageCloseHooks
postPageCreateHooks
preLaunchHooks
prePageCloseHooks
prePageCreateHooks
retireBrowserAfterPageCount
retiredBrowserControllers
optionaluseFingerprints
staticexternaldefaultMaxListeners
Methods
externaladdListener
Type parameters
- U: keyof BrowserPoolEvents<BrowserControllerReturn, PageReturn>
Parameters
externalevent: U
externallistener: BrowserPoolEvents<BrowserControllerReturn, PageReturn>[U]
Returns BrowserPool<Options, BrowserPlugins, BrowserControllerReturn, LaunchContextReturn, PageOptions, PageReturn>
closeAllBrowsers
Closes all managed browsers without waiting for pages to close.
Returns Promise<void>
destroy
Closes all managed browsers and tears down the pool.
Returns Promise<void>
externalemit
Type parameters
- U: keyof BrowserPoolEvents<BrowserControllerReturn, PageReturn>
Parameters
externalevent: U
externalrest...args: Parameters<BrowserPoolEvents<BrowserControllerReturn, PageReturn>[U]>
Returns boolean
externaleventNames
Type parameters
- U: keyof BrowserPoolEvents<BrowserControllerReturn, PageReturn>
Returns U[]
getBrowserControllerByPage
Retrieves a BrowserController for a given page. This is useful when you're working only with pages and need to access the browser manipulation functionality.
You could access the browser directly from the page, but that would circumvent
BrowserPool
and most likely cause weird things to happen, so please always useBrowserController
to control your browsers. The function returnsundefined
if the browser is closed.Parameters
page: PageReturn
Browser plugin page
Returns undefined | BrowserControllerReturn
externalgetMaxListeners
Returns number
getPage
If you provided a custom ID to one of your pages or saved the randomly generated one, you can use this function to retrieve the page. If the page is no longer open, the function will return
undefined
.Parameters
id: string
Returns undefined | PageReturn
getPageId
Page IDs are used throughout
BrowserPool
as a method of linking events. You can use a page ID to track the full lifecycle of the page. It is created even before a browser is launched and stays with the page until it's closed.Parameters
page: PageReturn
Returns undefined | string
externallistenerCount
Parameters
externaltype: keyof BrowserPoolEvents<BrowserControllerReturn, PageReturn>
Returns number
externallisteners
Type parameters
- U: keyof BrowserPoolEvents<BrowserControllerReturn, PageReturn>
Parameters
externaltype: U
Returns BrowserPoolEvents<BrowserControllerReturn, PageReturn>[U][]
newPage
Opens a new page in one of the running browsers or launches a new browser and opens a page there, if no browsers are active, or their page limits have been exceeded.
Parameters
options: BrowserPoolNewPageOptions<PageOptions, BrowserPlugins[number]> = {}
Returns Promise<PageReturn>
newPageInNewBrowser
Unlike newPage,
newPageInNewBrowser
always launches a new browser to open the page in. Use thelaunchOptions
option to configure the new browser.Parameters
options: BrowserPoolNewPageInNewBrowserOptions<PageOptions, BrowserPlugins[number]> = {}
Returns Promise<PageReturn>
newPageWithEachPlugin
Opens new pages with all available plugins and returns an array of pages in the same order as the plugins were provided to
BrowserPool
. This is useful when you want to run a script in multiple environments at the same time, typically in testing or website analysis.Example:
const browserPool = new BrowserPool({
browserPlugins: [
new PlaywrightPlugin(playwright.chromium),
new PlaywrightPlugin(playwright.firefox),
new PlaywrightPlugin(playwright.webkit),
]
});
const pages = await browserPool.newPageWithEachPlugin();
const [chromiumPage, firefoxPage, webkitPage] = pages;Parameters
optionsList: Omit<BrowserPoolNewPageOptions<PageOptions, BrowserPlugins[number]>, browserPlugin>[] = []
Returns Promise<PageReturn[]>
externaloff
Type parameters
- U: keyof BrowserPoolEvents<BrowserControllerReturn, PageReturn>
Parameters
externalevent: U
externallistener: BrowserPoolEvents<BrowserControllerReturn, PageReturn>[U]
Returns BrowserPool<Options, BrowserPlugins, BrowserControllerReturn, LaunchContextReturn, PageOptions, PageReturn>
externalon
Type parameters
- U: keyof BrowserPoolEvents<BrowserControllerReturn, PageReturn>
Parameters
externalevent: U
externallistener: BrowserPoolEvents<BrowserControllerReturn, PageReturn>[U]
Returns BrowserPool<Options, BrowserPlugins, BrowserControllerReturn, LaunchContextReturn, PageOptions, PageReturn>
externalonce
Type parameters
- U: keyof BrowserPoolEvents<BrowserControllerReturn, PageReturn>
Parameters
externalevent: U
externallistener: BrowserPoolEvents<BrowserControllerReturn, PageReturn>[U]
Returns BrowserPool<Options, BrowserPlugins, BrowserControllerReturn, LaunchContextReturn, PageOptions, PageReturn>
externalprependListener
Type parameters
- U: keyof BrowserPoolEvents<BrowserControllerReturn, PageReturn>
Parameters
externalevent: U
externallistener: BrowserPoolEvents<BrowserControllerReturn, PageReturn>[U]
Returns BrowserPool<Options, BrowserPlugins, BrowserControllerReturn, LaunchContextReturn, PageOptions, PageReturn>
externalprependOnceListener
Type parameters
- U: keyof BrowserPoolEvents<BrowserControllerReturn, PageReturn>
Parameters
externalevent: U
externallistener: BrowserPoolEvents<BrowserControllerReturn, PageReturn>[U]
Returns BrowserPool<Options, BrowserPlugins, BrowserControllerReturn, LaunchContextReturn, PageOptions, PageReturn>
externalrawListeners
Type parameters
- U: keyof BrowserPoolEvents<BrowserControllerReturn, PageReturn>
Parameters
externaltype: U
Returns BrowserPoolEvents<BrowserControllerReturn, PageReturn>[U][]
externalremoveAllListeners
Parameters
externaloptionalevent: keyof BrowserPoolEvents<BrowserControllerReturn, PageReturn>
Returns BrowserPool<Options, BrowserPlugins, BrowserControllerReturn, LaunchContextReturn, PageOptions, PageReturn>
externalremoveListener
Type parameters
- U: keyof BrowserPoolEvents<BrowserControllerReturn, PageReturn>
Parameters
externalevent: U
externallistener: BrowserPoolEvents<BrowserControllerReturn, PageReturn>[U]
Returns BrowserPool<Options, BrowserPlugins, BrowserControllerReturn, LaunchContextReturn, PageOptions, PageReturn>
retireAllBrowsers
Removes all active browsers from the pool. The browsers will be closed after all their pages are closed.
Returns void
retireBrowserByPage
Removes a browser from the pool. It will be closed after all its pages are closed.
Parameters
page: PageReturn
Returns void
retireBrowserController
Removes a browser controller from the pool. The underlying browser will be closed after all its pages are closed.
Parameters
browserController: BrowserControllerReturn
Returns void
externalsetMaxListeners
Parameters
externaln: number
Returns BrowserPool<Options, BrowserPlugins, BrowserControllerReturn, LaunchContextReturn, PageOptions, PageReturn>
The
BrowserPool
class is the most important class of thebrowser-pool
module. It manages opening and closing of browsers and their pages and its constructor options allow easy configuration of the browsers' and pages' lifecycle.The most important and useful constructor options are the various lifecycle hooks. Those allow you to sequentially call a list of (asynchronous) functions at each stage of the browser / page lifecycle.
Example: