viewpoint-core
High-level browser automation API for Rust, inspired by Playwright.
This crate provides the core browser, context, and page abstractions for the Viewpoint browser automation framework.
Features
- Browser launching and management
- Page navigation with configurable wait states
- Element locators (CSS, text, role, label, test ID, placeholder)
- Element actions (click, fill, type, hover, check, select)
- Automatic element waiting
- Browser Context Features:
- Cookie management (add, get, clear)
- Storage state persistence (including IndexedDB)
- Permission granting (geolocation, notifications, camera, etc.)
- Geolocation mocking
- HTTP credentials for authentication
- Extra HTTP headers
- Offline mode simulation
- Configurable timeouts
- Context-level init scripts
- Context-level route interception
- Network Interception:
- Request routing and modification
- Response mocking
- HAR file replay
- WebSocket monitoring
- HTTP authentication handling
- Event System:
- Page lifecycle events
- Popup handling
- Console and page error events
- Dialog handling
- Download handling
- Advanced Features:
- Exposed Rust functions callable from JavaScript
- ARIA accessibility snapshots
- Element highlighting for debugging
- Tracing for test debugging
Usage
For testing, use viewpoint-test which re-exports this crate with additional assertions and test fixtures.
use ;
// Launch browser
let browser = launch.headless.launch.await?;
let context = browser.new_context.await?;
let page = context.new_page.await?;
// Navigate
page.goto
.wait_until
.goto
.await?;
// Locate and interact with elements
let button = page.locator;
button.click.await?;
Context Features
use ;
use HashMap;
let browser = launch.headless.launch.await?;
// Create context with options
let context = browser.new_context_builder
.geolocation // San Francisco
.permissions
.has_touch
.locale
.timezone_id
.build
.await?;
let page = context.new_page.await?;
page.goto.goto.await?;
// Cookie management
context.add_cookies.await?;
let cookies = context.cookies.await?;
// Extra HTTP headers
let mut headers = new;
headers.insert;
context.set_extra_http_headers.await?;
// Offline mode
context.set_offline.await?;
// Save storage state for reuse
let state = context.storage_state.await?;
state.save.await?;
Event System
use ;
let browser = launch.headless.launch.await?;
let context = browser.new_context.await?;
// Listen for new pages (e.g., popups)
context.on_page.await;
let page = context.new_page.await?;
// Wait for a popup triggered by an action
let popup = context.wait_for_page.await?;
Network Interception
use ;
let browser = launch.headless.launch.await?;
let context = browser.new_context.await?;
let page = context.new_page.await?;
// Mock an API response
page.route.await?;
// Block certain requests
page.route.await?;
// HAR replay - serve requests from recorded HAR file
page.route_from_har.await?;
WebSocket Monitoring
use ;
let browser = launch.headless.launch.await?;
let context = browser.new_context.await?;
let page = context.new_page.await?;
page.on_websocket.await?;
Exposed Functions
use Browser;
let browser = launch.headless.launch.await?;
let context = browser.new_context.await?;
let page = context.new_page.await?;
// Expose a Rust function to JavaScript
page.expose_function.await?;
// Now callable from JavaScript:
// const result = await window.compute(1, 2); // returns 3
ARIA Accessibility Testing
use Browser;
let browser = launch.headless.launch.await?;
let context = browser.new_context.await?;
let page = context.new_page.await?;
page.goto.goto.await?;
// Get ARIA snapshot of an element
let snapshot = page.locator.aria_snapshot.await?;
println!; // YAML-like output
// Compare with expected structure
let expected = from_yaml?;
assert!;
Init Scripts
use Browser;
let browser = launch.headless.launch.await?;
let context = browser.new_context.await?;
// Add a script that runs before every page load
context.add_init_script.await?;
// All pages in this context will have the script
let page = context.new_page.await?;
License
MIT