ferridriver_test/context.rs
1//! Test context: the single object passed to every `#[ferritest]` function.
2//!
3//! Wraps `FixturePool` and provides typed getters for built-in fixtures.
4//! This is the Rust equivalent of Playwright's destructured `{ page, browser, context, testInfo }`.
5//!
6//! ```ignore
7//! #[ferritest]
8//! async fn my_test(ctx: TestContext) {
9//! let page = ctx.page().await?;
10//! page.goto("https://example.com", None).await?;
11//! }
12//! ```
13
14use std::sync::Arc;
15
16use crate::fixture::FixturePool;
17use crate::model::{TestFailure, TestInfo};
18
19/// Context object passed to every `#[ferritest]` test function.
20///
21/// Provides typed access to all built-in fixtures (page, browser, context, test_info)
22/// and raw access to the underlying `FixturePool` for custom fixtures.
23#[derive(Clone)]
24pub struct TestContext {
25 pool: FixturePool,
26}
27
28impl TestContext {
29 /// Create a new `TestContext` wrapping a `FixturePool`.
30 pub fn new(pool: FixturePool) -> Self {
31 Self { pool }
32 }
33
34 /// Get the `Page` fixture (test-scoped, fresh per test).
35 pub async fn page(&self) -> Result<Arc<ferridriver::Page>, TestFailure> {
36 self
37 .pool
38 .get::<ferridriver::Page>("page")
39 .await
40 .map_err(TestFailure::from)
41 }
42
43 /// Get the `Browser` fixture (worker-scoped, shared across tests in a worker).
44 pub async fn browser(&self) -> Result<Arc<ferridriver::Browser>, TestFailure> {
45 self
46 .pool
47 .get::<ferridriver::Browser>("browser")
48 .await
49 .map_err(TestFailure::from)
50 }
51
52 /// Get the `BrowserContext` fixture (test-scoped).
53 pub async fn browser_context(&self) -> Result<Arc<ferridriver::ContextRef>, TestFailure> {
54 self
55 .pool
56 .get::<ferridriver::ContextRef>("context")
57 .await
58 .map_err(TestFailure::from)
59 }
60
61 /// Get the `TestInfo` fixture (test-scoped runtime context).
62 pub async fn test_info(&self) -> Result<Arc<TestInfo>, TestFailure> {
63 self.pool.get::<TestInfo>("test_info").await.map_err(TestFailure::from)
64 }
65
66 /// Access the underlying `FixturePool` directly (for custom fixtures).
67 pub fn pool(&self) -> &FixturePool {
68 &self.pool
69 }
70}