BrowserContext

Struct BrowserContext 

Source
pub struct BrowserContext {
    inner: Weak<BrowserContext>,
}
Expand description

BrowserContexts provide a way to operate multiple independent browser sessions.

If a page opens another page, e.g. with a window.open call, the popup will belong to the parent page’s browser context.

Playwright allows creation of “incognito” browser contexts with browser.newContext() method. “Incognito” browser contexts don’t write any browsing data to disk.

Fields§

§inner: Weak<BrowserContext>

Implementations§

Source§

impl BrowserContext

Source

pub(crate) fn new(inner: Weak<Impl>) -> Self

Source

pub fn pages(&self) -> Result<Vec<Page>, Error>

Returns all open pages in the context.

Source

pub fn browser(&self) -> Result<Option<Browser>, Error>

Returns the browser instance of the context. If it was launched as a persistent context None gets returned.

Source

pub async fn new_page(&self) -> Result<Page, Arc<Error>>

Creates a new page in the browser context.

Source

pub async fn set_default_navigation_timeout( &self, timeout: u32, ) -> Result<(), Arc<Error>>

Source

pub async fn set_default_timeout(&self, timeout: u32) -> Result<(), Arc<Error>>

Source

pub async fn cookies(&self, urls: &[String]) -> Result<Vec<Cookie>, Arc<Error>>

If no URLs are specified, this method returns all cookies. If URLs are specified, only cookies that affect those URLs are returned.

Source

pub async fn add_cookies(&self, cookies: &[Cookie]) -> Result<(), Arc<Error>>

Adds cookies into this browser context. All pages within this context will have these cookies installed.

Source

pub async fn clear_cookies(&self) -> Result<(), Arc<Error>>

Clears context cookies.

Source

pub async fn grant_permissions( &self, permissions: &[String], origin: Option<&str>, ) -> Result<(), Arc<Error>>

Grants specified permissions to the browser context. Only grants corresponding permissions to the given origin if specified.

const context = await browser.newContext();
await context.grantPermissions(['clipboard-read']);
context.clearPermissions();
§Args
§permissions

A permission or an array of permissions to grant. Permissions can be one of the following values:

  • 'geolocation'
  • 'midi'
  • 'midi-sysex' (system-exclusive midi)
  • 'notifications'
  • 'push'
  • 'camera'
  • 'microphone'
  • 'background-sync'
  • 'ambient-light-sensor'
  • 'accelerometer'
  • 'gyroscope'
  • 'magnetometer'
  • 'accessibility-events'
  • 'clipboard-read'
  • 'clipboard-write'
  • 'payment-handler'
§origin

The origin to grant permissions to, e.g. "https://example.com".

Source

pub async fn clear_permissions(&self) -> Result<(), Arc<Error>>

Clears all permission overrides for the browser context.

Source

pub async fn set_geolocation( &self, geolocation: Option<&Geolocation>, ) -> Result<(), Arc<Error>>

Sets the context’s geolocation. Passing null or undefined emulates position unavailable.

await browserContext.setGeolocation({latitude: 59.95, longitude: 30.31667});

NOTE: Consider using [method: BrowserContext.grantPermissions] to grant permissions for the browser context pages to read its geolocation.

Source

pub async fn set_offline(&self, offline: bool) -> Result<(), Arc<Error>>

Sets whether to emulate network being offline for the browser context.

Source

pub async fn add_init_script(&self, script: &str) -> Result<(), Arc<Error>>

Adds a script which would be evaluated in one of the following scenarios:

  • Whenever a page is created in the browser context or is navigated.
  • Whenever a child frame is attached or navigated in any page in the browser context. In this case, the script is evaluated in the context of the newly attached frame.

The script is evaluated after the document was created but before any of its scripts were run. This is useful to amend the JavaScript environment, e.g. to seed Math.random.

An example of overriding Math.random before the page loads:

Math.random = () => 42;
await browserContext.addInitScript({
 path: 'preload.js'
});

NOTE: The order of evaluation of multiple scripts installed via [method: BrowserContext.addInitScript] and [method: Page.addInitScript] is not defined.

Source

pub async fn set_extra_http_headers<T>( &self, headers: T, ) -> Result<(), Arc<Error>>
where T: IntoIterator<Item = (String, String)>,

The extra HTTP headers will be sent with every request initiated by any page in the context. These headers are merged with page-specific extra HTTP headers set with [method: Page.setExtraHTTPHeaders]. If page overrides a particular header, page-specific header value will be used instead of the browser context header value.

NOTE: [method: BrowserContext.setExtraHTTPHeaders] does not guarantee the order of headers in the outgoing requests.

Source

pub async fn expect_event(&self, evt: EventType) -> Result<Event, Error>

Source

pub async fn storage_state(&self) -> Result<StorageState, Arc<Error>>

Returns storage state for this browser context, contains current cookies and local storage snapshot.

Source

pub async fn close(&self) -> Result<(), Arc<Error>>

All temporary browsers will be closed when the connection is terminated, but this struct has no Drop. it needs to be called explicitly to close it at any given time.

NOTE: The default browser context cannot be closed.

Source

pub fn subscribe_event( &self, ) -> Result<impl Stream<Item = Result<Event, BroadcastStreamRecvError>>, Error>

Trait Implementations§

Source§

impl Debug for BrowserContext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for BrowserContext

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.