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
impl BrowserContext
pub(crate) fn new(inner: Weak<Impl>) -> Self
Sourcepub fn browser(&self) -> Result<Option<Browser>, Error>
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.
Sourcepub async fn new_page(&self) -> Result<Page, Arc<Error>>
pub async fn new_page(&self) -> Result<Page, Arc<Error>>
Creates a new page in the browser context.
pub async fn set_default_timeout(&self, timeout: u32) -> Result<(), Arc<Error>>
If no URLs are specified, this method returns all cookies. If URLs are specified, only cookies that affect those URLs are returned.
Adds cookies into this browser context. All pages within this context will have these cookies installed.
Clears context cookies.
Sourcepub async fn grant_permissions(
&self,
permissions: &[String],
origin: Option<&str>,
) -> Result<(), Arc<Error>>
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".
Sourcepub async fn clear_permissions(&self) -> Result<(), Arc<Error>>
pub async fn clear_permissions(&self) -> Result<(), Arc<Error>>
Clears all permission overrides for the browser context.
Sourcepub async fn set_geolocation(
&self,
geolocation: Option<&Geolocation>,
) -> Result<(), Arc<Error>>
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.
Sourcepub async fn set_offline(&self, offline: bool) -> Result<(), Arc<Error>>
pub async fn set_offline(&self, offline: bool) -> Result<(), Arc<Error>>
Sets whether to emulate network being offline for the browser context.
Sourcepub async fn add_init_script(&self, script: &str) -> Result<(), Arc<Error>>
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.
Sourcepub async fn set_extra_http_headers<T>(
&self,
headers: T,
) -> Result<(), Arc<Error>>
pub async fn set_extra_http_headers<T>( &self, headers: T, ) -> Result<(), Arc<Error>>
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.
pub async fn expect_event(&self, evt: EventType) -> Result<Event, Error>
Sourcepub async fn storage_state(&self) -> Result<StorageState, Arc<Error>>
pub async fn storage_state(&self) -> Result<StorageState, Arc<Error>>
Returns storage state for this browser context, contains current cookies and local storage snapshot.
Sourcepub async fn close(&self) -> Result<(), Arc<Error>>
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.