Expand description
§Browser Context Management
Browser contexts are isolated environments within a browser, similar to incognito windows. Each context has its own cookies, cache, localStorage, and other browser storage.
§Features
- Isolation: Each context is completely isolated from others
- Cookie Management: Add, get, and clear cookies with
BrowserContext::add_cookies,BrowserContext::cookies,BrowserContext::clear_cookies - Storage State: Save and restore browser state (cookies, localStorage) for authentication
- Permissions: Grant permissions like geolocation, camera, microphone
- Geolocation: Mock browser location with
BrowserContext::set_geolocation - HTTP Credentials: Configure basic/digest authentication
- Extra Headers: Add headers to all requests in the context
- Offline Mode: Simulate network offline conditions
- Event Handling: Listen for page creation and context close events
- Init Scripts: Run scripts before every page load
- Custom Test ID: Configure which attribute is used for test IDs
- Network Routing: Intercept and mock requests at the context level
- HAR Recording: Record network traffic for debugging
- Tracing: Record traces for debugging
§Quick Start
use viewpoint_core::{Browser, BrowserContext, Permission};
let browser = Browser::launch().headless(true).launch().await?;
// Create a simple context
let context = browser.new_context().await?;
// Create a context with options
let context = browser.new_context_builder()
.viewport(1920, 1080)
.geolocation(37.7749, -122.4194)
.permissions(vec![Permission::Geolocation])
.build()
.await?;
// Create a page in the context
let page = context.new_page().await?;
page.goto("https://example.com").goto().await?;§Cookie Management
ⓘ
use viewpoint_core::{Browser, Cookie, SameSite};
// Add cookies
context.add_cookies(vec![
Cookie {
name: "session".to_string(),
value: "abc123".to_string(),
domain: Some(".example.com".to_string()),
path: Some("/".to_string()),
expires: None,
http_only: Some(true),
secure: Some(true),
same_site: Some(SameSite::Lax),
}
]).await?;
// Get all cookies
let cookies = context.cookies(None).await?;
// Get cookies for specific URLs
let cookies = context.cookies(Some(&["https://example.com"])).await?;
// Clear all cookies
context.clear_cookies().clear().await?;
// Clear cookies matching a pattern
context.clear_cookies()
.domain("example.com")
.clear()
.await?;§Storage State
Save and restore browser state for authentication:
ⓘ
use viewpoint_core::{Browser, StorageStateSource};
// Save storage state after login
context.storage_state()
.path("auth.json")
.save()
.await?;
// Create a new context with saved state
let context = browser.new_context_builder()
.storage_state_path("auth.json")
.build()
.await?;§Event Handling
ⓘ
use viewpoint_core::Browser;
// Listen for new pages
let handler_id = context.on_page(|page_info| async move {
println!("New page created: {}", page_info.target_id);
Ok(())
}).await;
// Listen for context close
context.on_close(|| async {
println!("Context closed");
Ok(())
}).await;
// Remove handler later
context.off_page(handler_id).await;§Tracing
ⓘ
use viewpoint_core::Browser;
// Start tracing
context.tracing().start().await?;
// ... perform actions ...
// Stop and save trace
context.tracing().stop("trace.zip").await?;Re-exports§
pub use events::ContextEventManager;pub use events::HandlerId;pub use storage::StorageStateBuilder;pub use storage::StorageStateOptions;pub use trace::Tracing;pub use trace::TracingOptions;pub use types::ColorScheme;pub use types::ContextOptions;pub use types::ContextOptionsBuilder;pub use types::Cookie;pub use types::ForcedColors;pub use types::Geolocation;pub use types::HttpCredentials;pub use types::IndexedDbDatabase;pub use types::IndexedDbEntry;pub use types::IndexedDbIndex;pub use types::IndexedDbObjectStore;pub use types::LocalStorageEntry;pub use types::Permission;pub use types::ProxyConfig;pub use types::ReducedMotion;pub use types::SameSite;pub use types::StorageOrigin;pub use types::StorageState;pub use types::StorageStateSource;pub use types::ViewportSize;pub use crate::page::page_error::WebError;
Modules§
- binding
- Context-level exposed function bindings.
- events
- Event system infrastructure for browser context.
- routing
- Context-level network routing.
- storage
- Storage state collection and restoration.
- trace
- Tracing implementation for recording test execution traces.
- types
- Context-related types.
Structs§
- Browser
Context - An isolated browser context.
- Clear
Cookies Builder - Builder for clearing cookies with filters.
- Page
Info - Information about a page in the context.
- SetGeolocation
Builder - Builder for setting geolocation.
Constants§
- DEFAULT_
TEST_ ID_ ATTRIBUTE - Default test ID attribute name.
Type Aliases§
- WebError
Handler - Type alias for web error handler function.