pub struct TestHarness { /* private fields */ }Expand description
Test harness that manages browser, context, and page lifecycle.
The harness provides access to browser automation fixtures and handles
cleanup automatically via Drop. It supports different scoping levels
to balance test isolation against performance.
§Scoping Levels
TestHarness::new()- Test-scoped: new browser per test (default)TestHarness::from_browser()- Module-scoped: reuse browser, fresh context/pageTestHarness::from_context()- Shared context: reuse context, fresh page only
§Example
use viewpoint_test::TestHarness;
#[tokio::test]
async fn my_test() -> Result<(), Box<dyn std::error::Error>> {
let harness = TestHarness::new().await?;
let page = harness.page();
page.goto("https://example.com").goto().await?;
Ok(()) // harness drops and cleans up
}Implementations§
Source§impl TestHarness
impl TestHarness
Sourcepub async fn new() -> Result<Self, TestError>
pub async fn new() -> Result<Self, TestError>
Create a new test harness with default configuration.
This creates a new browser, context, and page for the test. All resources are owned and will be cleaned up on drop.
§Errors
Returns an error if browser launch or page creation fails.
Sourcepub async fn with_config(config: TestConfig) -> Result<Self, TestError>
pub async fn with_config(config: TestConfig) -> Result<Self, TestError>
Create a new test harness with custom configuration.
§Errors
Returns an error if browser launch or page creation fails.
Sourcepub async fn from_browser(browser: &Browser) -> Result<Self, TestError>
pub async fn from_browser(browser: &Browser) -> Result<Self, TestError>
Create a test harness using an existing browser.
This creates a new context and page in the provided browser. The browser will NOT be closed when the harness is dropped.
§Errors
Returns an error if context or page creation fails.
Sourcepub async fn from_context(context: &BrowserContext) -> Result<Self, TestError>
pub async fn from_context(context: &BrowserContext) -> Result<Self, TestError>
Create a test harness using an existing context.
This creates a new page in the provided context. Neither the browser nor context will be closed when the harness is dropped.
§Errors
Returns an error if page creation fails.
Sourcepub fn context(&self) -> Option<&BrowserContext>
pub fn context(&self) -> Option<&BrowserContext>
Get a reference to the browser context.
Returns None if this harness was created with from_context().
Sourcepub fn browser(&self) -> Option<&Browser>
pub fn browser(&self) -> Option<&Browser>
Get a reference to the browser.
Returns None if this harness was created with from_browser() or from_context().
Sourcepub fn config(&self) -> &TestConfig
pub fn config(&self) -> &TestConfig
Get the test configuration.