pub struct BrowserContext { /* private fields */ }Implementations§
Source§impl BrowserContext
impl BrowserContext
Sourcepub fn new(
parent: Arc<dyn ChannelOwner>,
type_name: String,
guid: Arc<str>,
initializer: Value,
) -> Result<Self>
pub fn new( parent: Arc<dyn ChannelOwner>, type_name: String, guid: Arc<str>, initializer: Value, ) -> Result<Self>
Creates a new BrowserContext from protocol initialization
This is called by the object factory when the server sends a __create__ message
for a BrowserContext object.
§Arguments
parent- The parent Browser objecttype_name- The protocol type name (“BrowserContext”)guid- The unique identifier for this contextinitializer- The initialization data from the server
§Errors
Returns error if initializer is malformed
Sourcepub async fn add_init_script(&self, script: &str) -> Result<()>
pub async fn add_init_script(&self, script: &str) -> Result<()>
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.
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.
§Arguments
script- Script to be evaluated in all pages in the browser context.
§Errors
Returns error if:
- Context has been closed
- Communication with browser process fails
See: https://playwright.dev/docs/api/class-browsercontext#browser-context-add-init-script
Sourcepub async fn new_page(&self) -> Result<Page>
pub async fn new_page(&self) -> Result<Page>
Creates a new page in this browser context.
Pages are isolated tabs/windows within a context. Each page starts at “about:blank” and can be navigated independently.
§Errors
Returns error if:
- Context has been closed
- Communication with browser process fails
See: https://playwright.dev/docs/api/class-browsercontext#browser-context-new-page
Sourcepub fn pages(&self) -> Vec<Page>
pub fn pages(&self) -> Vec<Page>
Returns all open pages in the context.
This method provides a snapshot of all currently active pages that belong
to this browser context instance. Pages created via new_page() and popup
pages opened through user interactions are included.
In persistent contexts launched with --app=url, this will include the
initial page created automatically by Playwright.
§Errors
This method does not return errors. It provides a snapshot of pages at the time of invocation.
See: https://playwright.dev/docs/api/class-browsercontext#browser-context-pages
Sourcepub fn browser(&self) -> Option<Browser>
pub fn browser(&self) -> Option<Browser>
Returns the browser instance that owns this context.
Returns None only for contexts created outside of normal browser
(e.g., Android or Electron contexts). For both regular contexts and
persistent contexts, this returns the owning Browser instance.
§Errors
This method does not return errors.
See: https://playwright.dev/docs/api/class-browsercontext#browser-context-browser
Sourcepub async fn request(&self) -> Result<APIRequestContext>
pub async fn request(&self) -> Result<APIRequestContext>
Returns the APIRequestContext associated with this context.
The APIRequestContext is created automatically by the server for each
BrowserContext. It enables performing HTTP requests and is used internally
by Route::fetch().
See: https://playwright.dev/docs/api/class-browsercontext#browser-context-request
Sourcepub async fn close(&self) -> Result<()>
pub async fn close(&self) -> Result<()>
Closes the browser context and all its pages.
This is a graceful operation that sends a close command to the context and waits for it to shut down properly.
§Errors
Returns error if:
- Context has already been closed
- Communication with browser process fails
See: https://playwright.dev/docs/api/class-browsercontext#browser-context-close
Sourcepub async fn set_default_timeout(&self, timeout: f64)
pub async fn set_default_timeout(&self, timeout: f64)
Sets the default timeout for all operations in this browser context.
This applies to all pages already open in this context as well as pages
created subsequently. Pass 0 to disable timeouts.
§Arguments
timeout- Timeout in milliseconds
See: https://playwright.dev/docs/api/class-browsercontext#browser-context-set-default-timeout
Sets the default timeout for navigation operations in this browser context.
This applies to all pages already open in this context as well as pages
created subsequently. Pass 0 to disable timeouts.
§Arguments
timeout- Timeout in milliseconds
See: https://playwright.dev/docs/api/class-browsercontext#browser-context-set-default-navigation-timeout
Sourcepub async fn pause(&self) -> Result<()>
pub async fn pause(&self) -> Result<()>
Pauses the browser context.
This pauses the execution of all pages in the context.
Sourcepub async fn storage_state(&self) -> Result<StorageState>
pub async fn storage_state(&self) -> Result<StorageState>
Returns storage state for this browser context.
Contains current cookies and local storage snapshots.
See: https://playwright.dev/docs/api/class-browsercontext#browser-context-storage-state
Adds cookies into this browser context.
All pages within this context will have these cookies installed. Cookies can be granularly specified
with name, value, url, domain, path, expires, httpOnly, secure, sameSite.
See: https://playwright.dev/docs/api/class-browsercontext#browser-context-add-cookies
Returns cookies for this browser context, optionally filtered by URLs.
If urls is None or empty, all cookies are returned.
§Arguments
urls- Optional list of URLs to filter cookies by
§Errors
Returns error if:
- Context has been closed
- Communication with browser process fails
See: https://playwright.dev/docs/api/class-browsercontext#browser-context-cookies
Clears cookies from this browser context, with optional filters.
When called with no options, all cookies are removed. Use ClearCookiesOptions
to filter which cookies to clear by name, domain, or path.
§Arguments
options- Optional filters for which cookies to clear
§Errors
Returns error if:
- Context has been closed
- Communication with browser process fails
See: https://playwright.dev/docs/api/class-browsercontext#browser-context-clear-cookies
Sourcepub async fn set_extra_http_headers(
&self,
headers: HashMap<String, String>,
) -> Result<()>
pub async fn set_extra_http_headers( &self, headers: HashMap<String, String>, ) -> Result<()>
Sets extra HTTP headers that will be sent with every request from this context.
These headers are merged with per-page extra headers set with page.set_extra_http_headers().
If the page has specific headers that conflict, page-level headers take precedence.
§Arguments
headers- Map of header names to values. All header names are lowercased.
§Errors
Returns error if:
- Context has been closed
- Communication with browser process fails
See: https://playwright.dev/docs/api/class-browsercontext#browser-context-set-extra-http-headers
Sourcepub async fn grant_permissions(
&self,
permissions: &[&str],
options: Option<GrantPermissionsOptions>,
) -> Result<()>
pub async fn grant_permissions( &self, permissions: &[&str], options: Option<GrantPermissionsOptions>, ) -> Result<()>
Grants browser permissions to the context.
Permissions are granted for all pages in the context. The optional origin
in GrantPermissionsOptions restricts the grant to a specific URL origin.
Common permissions: "geolocation", "notifications", "camera",
"microphone", "clipboard-read", "clipboard-write".
§Arguments
permissions- List of permission strings to grantoptions- Optional options, includingoriginto restrict the grant
§Errors
Returns error if:
- Permission name is not recognised
- Context has been closed
- Communication with browser process fails
See: https://playwright.dev/docs/api/class-browsercontext#browser-context-grant-permissions
Sourcepub async fn clear_permissions(&self) -> Result<()>
pub async fn clear_permissions(&self) -> Result<()>
Clears all permission overrides for this browser context.
Reverts all permissions previously set with grant_permissions() back to
the browser default state.
§Errors
Returns error if:
- Context has been closed
- Communication with browser process fails
See: https://playwright.dev/docs/api/class-browsercontext#browser-context-clear-permissions
Sourcepub async fn set_geolocation(
&self,
geolocation: Option<Geolocation>,
) -> Result<()>
pub async fn set_geolocation( &self, geolocation: Option<Geolocation>, ) -> Result<()>
Sets or clears the geolocation for all pages in this context.
Pass Some(Geolocation { ... }) to set a specific location, or None to
clear the override and let the browser handle location requests naturally.
Note: Geolocation access requires the "geolocation" permission to be granted
via grant_permissions() for navigator.geolocation to succeed.
§Arguments
geolocation- Location to set, orNoneto clear
§Errors
Returns error if:
- Latitude or longitude is out of range
- Context has been closed
- Communication with browser process fails
See: https://playwright.dev/docs/api/class-browsercontext#browser-context-set-geolocation
Sourcepub async fn set_offline(&self, offline: bool) -> Result<()>
pub async fn set_offline(&self, offline: bool) -> Result<()>
Toggles the offline mode for this browser context.
When true, all network requests from pages in this context will fail with
a network error. Set to false to restore network connectivity.
§Arguments
offline-trueto go offline,falseto go back online
§Errors
Returns error if:
- Context has been closed
- Communication with browser process fails
See: https://playwright.dev/docs/api/class-browsercontext#browser-context-set-offline
Sourcepub async fn route<F, Fut>(&self, pattern: &str, handler: F) -> Result<()>
pub async fn route<F, Fut>(&self, pattern: &str, handler: F) -> Result<()>
Registers a route handler for context-level network interception.
Routes registered on a context apply to all pages within the context. Page-level routes take precedence over context-level routes.
§Arguments
pattern- URL pattern to match (supports glob patterns like “**/*.png”)handler- Async closure that handles the route
See: https://playwright.dev/docs/api/class-browsercontext#browser-context-route
Sourcepub async fn unroute(&self, pattern: &str) -> Result<()>
pub async fn unroute(&self, pattern: &str) -> Result<()>
Removes route handler(s) matching the given URL pattern.
§Arguments
pattern- URL pattern to remove handlers for
See: https://playwright.dev/docs/api/class-browsercontext#browser-context-unroute
Sourcepub async fn unroute_all(
&self,
_behavior: Option<UnrouteBehavior>,
) -> Result<()>
pub async fn unroute_all( &self, _behavior: Option<UnrouteBehavior>, ) -> Result<()>
Removes all registered route handlers.
§Arguments
behavior- Optional behavior for in-flight handlers
See: https://playwright.dev/docs/api/class-browsercontext#browser-context-unroute-all
Trait Implementations§
Source§impl Clone for BrowserContext
impl Clone for BrowserContext
Source§fn clone(&self) -> BrowserContext
fn clone(&self) -> BrowserContext
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more