pub struct Browser { /* private fields */ }Expand description
A browser instance connected via CDP.
The Browser struct represents a connection to a Chromium-based browser.
It can be obtained by:
Browser::launch()- Spawn and connect to a new browser processBrowser::connect()- Connect to an existing browser via WebSocket URLBrowser::connect_over_cdp()- Connect via HTTP endpoint (auto-discovers WebSocket)
§Key Methods
new_context()- Create a new isolated browser contextcontexts()- List all browser contexts (including pre-existing ones)close()- Close the browser connection
§Ownership
Use is_owned() to check if this browser was launched by us
(vs connected to an existing process). Owned browsers are terminated when closed.
Implementations§
Source§impl Browser
impl Browser
Sourcepub fn launch() -> BrowserBuilder
pub fn launch() -> BrowserBuilder
Create a browser builder for launching a new browser.
§Example
use viewpoint_core::Browser;
let browser = Browser::launch()
.headless(true)
.launch()
.await?;Sourcepub fn connect_over_cdp(
endpoint_url: impl Into<String>,
) -> ConnectOverCdpBuilder
pub fn connect_over_cdp( endpoint_url: impl Into<String>, ) -> ConnectOverCdpBuilder
Connect to an already-running browser via HTTP endpoint or WebSocket URL.
This method supports both:
- HTTP endpoint URLs (e.g.,
http://localhost:9222) - auto-discovers WebSocket URL - WebSocket URLs (e.g.,
ws://localhost:9222/devtools/browser/...) - direct connection
For HTTP endpoints, the method fetches /json/version to discover the WebSocket URL,
similar to Playwright’s connectOverCDP.
§Example
use viewpoint_core::Browser;
use std::time::Duration;
// Connect via HTTP endpoint (recommended)
let browser = Browser::connect_over_cdp("http://localhost:9222")
.connect()
.await?;
// With custom timeout and headers
let browser = Browser::connect_over_cdp("http://localhost:9222")
.timeout(Duration::from_secs(10))
.header("Authorization", "Bearer token")
.connect()
.await?;
// Access existing browser contexts and pages
let contexts = browser.contexts().await?;
for context in contexts {
let pages = context.pages().await?;
for page in pages {
println!("Found page: {:?}", page.target_id);
}
}Sourcepub async fn contexts(&self) -> Result<Vec<BrowserContext>, BrowserError>
pub async fn contexts(&self) -> Result<Vec<BrowserContext>, BrowserError>
Get all browser contexts.
Returns all existing browser contexts, including:
- Contexts created via
new_context() - The default context (for connected browsers)
- Any pre-existing contexts (when connecting to an already-running browser)
§Example
use viewpoint_core::Browser;
let browser = Browser::connect_over_cdp("http://localhost:9222")
.connect()
.await?;
let contexts = browser.contexts().await?;
println!("Found {} browser contexts", contexts.len());
// The default context (empty string ID) represents the browser's main profile
for context in &contexts {
if context.id().is_empty() {
println!("This is the default context");
}
}§Errors
Returns an error if querying contexts fails.
Sourcepub async fn new_context(&self) -> Result<BrowserContext, BrowserError>
pub async fn new_context(&self) -> Result<BrowserContext, BrowserError>
Create a new isolated browser context.
Browser contexts are isolated environments within the browser, similar to incognito windows. They have their own cookies, cache, and storage.
§Errors
Returns an error if context creation fails.
Sourcepub fn new_context_builder(&self) -> NewContextBuilder<'_>
pub fn new_context_builder(&self) -> NewContextBuilder<'_>
Create a new context options builder.
Use this to create a browser context with custom configuration.
§Example
use viewpoint_core::{Browser, Permission};
let browser = Browser::launch().headless(true).launch().await?;
let context = browser.new_context_builder()
.geolocation(37.7749, -122.4194)
.permissions(vec![Permission::Geolocation])
.offline(false)
.build()
.await?;Sourcepub async fn new_context_with_options(
&self,
options: ContextOptions,
) -> Result<BrowserContext, BrowserError>
pub async fn new_context_with_options( &self, options: ContextOptions, ) -> Result<BrowserContext, BrowserError>
Create a new isolated browser context with options.
§Errors
Returns an error if context creation fails.
Sourcepub async fn close(&self) -> Result<(), BrowserError>
pub async fn close(&self) -> Result<(), BrowserError>
Close the browser.
If this browser was launched by us, the process will be terminated. If it was connected to, only the WebSocket connection is closed.
§Errors
Returns an error if closing fails.
Sourcepub fn connection(&self) -> &Arc<CdpConnection>
pub fn connection(&self) -> &Arc<CdpConnection>
Get a reference to the CDP connection.