Home Reference Source
public class | source

BrowserContext

Extends:

eventemitter3~EventEmitter → BrowserContext

Constructor Summary

Public Constructor
public

constructor(connection: Chrome | CRIConnection, browser: Browser, contextId: string)

Member Summary

Private Members
private
private
private

Method Summary

Public Methods
public

The browser this browser context belongs to

public

Reset all permissions overrides for this browser context

public

async close(): Promise<void>

Closes the browser context.

public

Closes the target specified by the targetId.

since chrome-remote-interface-extra
public

async exposeCDPOnTarget(targetId: string, bindingName: string): Promise<void>

Inject object to the target's main frame that provides a communication channel with browser target.

Injected object will be available as window[bindingName].

The object has the following API:

  • binding.send(json) - a method to send messages over the remote debugging protocol
  • binding.onmessage = json => handleMessage(json) - a callback that will be called for the protocol notifications and command responses.

EXPERIMENTAL

since chrome-remote-interface-extra
public

Get a Chrome histogram by name.

since chrome-remote-interface-extra
public

Get Chrome histograms.

since chrome-remote-interface-extra
public

Get position and size of the browser window.

since chrome-remote-interface-extra
public

getWindowForTarget(targetId: string): Promise<{bounds: WindowBounds, windowId: number}>

Get the browser window that contains the target.

since chrome-remote-interface-extra
public

Returns whether BrowserContext is incognito.

public

Creates a new page in the browser context

public

async overridePermissions(origin: string, permissions: Array<string>)

Grant specific permissions to the given origin and reject all others.

public

async pages(): Promise<Array<Page>>

An array of all pages inside the browser context

public

async setWindowBounds(windowId: number, bounds: WindowBounds): Promise<void>

Set position and/or size of the browser window.

since chrome-remote-interface-extra
public

An array of all active targets inside the browser context

public
public

waitForTarget(predicate: function(target: Target): boolean, options: {timeout?: number}): Promise<Target>

This searches for a target in this specific browser context.

Public Constructors

public constructor(connection: Chrome | CRIConnection, browser: Browser, contextId: string) source

Params:

NameTypeAttributeDescription
connection Chrome | CRIConnection
  • nullable: false
browser Browser
  • nullable: false
contextId string
  • nullable: true

Private Members

private _browser: Browser source

private _connection: Chrome | CRIConnection source

private _id: string source

Public Methods

public browser(): Browser source

The browser this browser context belongs to

Return:

Browser (nullable: false)

public async clearPermissionOverrides(): Promise<void> source

Reset all permissions overrides for this browser context

Return:

Promise<void>

See:

public async close(): Promise<void> source

Closes the browser context. All the targets that belong to the browser context will be closed.

Return:

Promise<void>

public closeTarget(targetId: string): Promise<boolean> since chrome-remote-interface-extra source

Closes the target specified by the targetId. If the target is a page that gets closed too.

Params:

NameTypeAttributeDescription
targetId string

Return:

Promise<boolean>

public async exposeCDPOnTarget(targetId: string, bindingName: string): Promise<void> since chrome-remote-interface-extra source

Inject object to the target's main frame that provides a communication channel with browser target.

Injected object will be available as window[bindingName].

The object has the following API:

  • binding.send(json) - a method to send messages over the remote debugging protocol
  • binding.onmessage = json => handleMessage(json) - a callback that will be called for the protocol notifications and command responses.

EXPERIMENTAL

Params:

NameTypeAttributeDescription
targetId string
bindingName string
  • optional

Binding name, 'cdp' if not specified

Return:

Promise<void>

See:

public getHistogram(name: string, delta: boolean): Promise<CDPHistogram> since chrome-remote-interface-extra source

Get a Chrome histogram by name. EXPERIMENTAL

Params:

NameTypeAttributeDescription
name string

Requested histogram name

delta boolean
  • optional

If true, retrieve delta since last call

See:

public getHistograms(options: BrowserHistogramQuery): Promise<Array<CDPHistogram>> since chrome-remote-interface-extra source

Get Chrome histograms. EXPERIMENTAL Optional options:

  • query: Requested substring in name. Only histograms which have query as a substring in their name are extracted. An empty or absent query returns all histograms.
  • delta: If true, retrieve delta since last call

Params:

NameTypeAttributeDescription
options BrowserHistogramQuery
  • optional

See:

public getWindowBounds(windowId: number): Promise<WindowBounds> since chrome-remote-interface-extra source

Get position and size of the browser window. EXPERIMENTAL

Params:

NameTypeAttributeDescription
windowId number

See:

public getWindowForTarget(targetId: string): Promise<{bounds: WindowBounds, windowId: number}> since chrome-remote-interface-extra source

Get the browser window that contains the target. EXPERIMENTAL

Params:

NameTypeAttributeDescription
targetId string
  • optional

Optional target id of the target to receive the window id and its bound for. If called as a part of the session, associated targetId is used.

Return:

Promise<{bounds: WindowBounds, windowId: number}>

See:

public isIncognito(): boolean source

Returns whether BrowserContext is incognito. The default browser context is the only non-incognito browser context

Return:

boolean

public newPage(): Promise<Page> source

Creates a new page in the browser context

Return:

Promise<Page>

public async overridePermissions(origin: string, permissions: Array<string>) source

Grant specific permissions to the given origin and reject all others. EXPERIMENTAL

Params:

NameTypeAttributeDescription
origin string

The origin these permissions will be granted for

permissions Array<string>

Array of permission overrides

Example:

const context = browser.defaultBrowserContext();
context.overridePermissions('https://example.com', ['clipboard-read']);
// do stuff ..
context.clearPermissionOverrides();

See:

public async pages(): Promise<Array<Page>> source

An array of all pages inside the browser context

Return:

Promise<Array<Page>>

public async setWindowBounds(windowId: number, bounds: WindowBounds): Promise<void> since chrome-remote-interface-extra source

Set position and/or size of the browser window. EXPERIMENTAL

Params:

NameTypeAttributeDescription
windowId number

An browser window id

bounds WindowBounds

New window bounds. The 'minimized', 'maximized' and 'fullscreen' states cannot be combined with 'left', 'top', 'width' or 'height'. Leaves unspecified fields unchanged.

Return:

Promise<void>

See:

public targets(): Array<Target> source

An array of all active targets inside the browser context

Return:

Array<Target>

target

public toString(): string source

Return:

string

public waitForTarget(predicate: function(target: Target): boolean, options: {timeout?: number}): Promise<Target> source

This searches for a target in this specific browser context.

Params:

NameTypeAttributeDescription
predicate function(target: Target): boolean
options {timeout?: number}
  • optional

Return:

Promise<Target>

Example:

await page.evaluate(() => window.open('https://www.example.com/'))
const newWindowTarget = await browserContext.waitForTarget(target => target.url() === 'https://www.example.com/')