pub struct BrowserContext { /* private fields */ }Expand description
BrowserContext represents an isolated browser session.
Contexts are isolated environments within a browser instance. Each context has its own cookies, cache, and local storage, enabling independent sessions without interference.
§Example
use playwright_core::protocol::Playwright;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let playwright = Playwright::launch().await?;
let browser = playwright.chromium().launch().await?;
// Create isolated contexts
let context1 = browser.new_context().await?;
let context2 = browser.new_context().await?;
// Create pages in each context
let page1 = context1.new_page().await?;
let page2 = context2.new_page().await?;
// Cleanup
context1.close().await?;
context2.close().await?;
browser.close().await?;
Ok(())
}Implementations§
Source§impl BrowserContext
impl BrowserContext
Sourcepub fn new(
parent: Arc<dyn ChannelOwner>,
type_name: String,
guid: Arc<str>,
initializer: Value,
) -> Result<BrowserContext, Error>
pub fn new( parent: Arc<dyn ChannelOwner>, type_name: String, guid: Arc<str>, initializer: Value, ) -> Result<BrowserContext, Error>
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 new_page(&self) -> Result<Page, Error>
pub async fn new_page(&self) -> Result<Page, Error>
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 async fn close(&self) -> Result<(), Error>
pub async fn close(&self) -> Result<(), Error>
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
Trait Implementations§
Source§impl ChannelOwner for BrowserContext
impl ChannelOwner for BrowserContext
Source§fn parent(&self) -> Option<Arc<dyn ChannelOwner>>
fn parent(&self) -> Option<Arc<dyn ChannelOwner>>
Source§fn connection(&self) -> Arc<dyn ConnectionLike>
fn connection(&self) -> Arc<dyn ConnectionLike>
Source§fn initializer(&self) -> &Value
fn initializer(&self) -> &Value
Source§fn dispose(&self, reason: DisposeReason)
fn dispose(&self, reason: DisposeReason)
Source§fn adopt(&self, child: Arc<dyn ChannelOwner>)
fn adopt(&self, child: Arc<dyn ChannelOwner>)
Source§fn add_child(&self, guid: Arc<str>, child: Arc<dyn ChannelOwner>)
fn add_child(&self, guid: Arc<str>, child: Arc<dyn ChannelOwner>)
Source§fn remove_child(&self, guid: &str)
fn remove_child(&self, guid: &str)
Source§fn on_event(&self, method: &str, params: Value)
fn on_event(&self, method: &str, params: Value)
Source§fn was_collected(&self) -> bool
fn was_collected(&self) -> bool
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