viewpoint_core/context/tracing_access/
mod.rs

1//! Tracing access for BrowserContext.
2//!
3//! This module provides the tracing controller access method.
4
5use super::trace::Tracing;
6use super::BrowserContext;
7
8impl BrowserContext {
9    /// Get a tracing controller for recording test execution traces.
10    ///
11    /// Traces capture screenshots, DOM snapshots, and network activity
12    /// for debugging test failures. The resulting trace files are compatible
13    /// with Playwright's Trace Viewer.
14    ///
15    /// **Note:** At least one page must exist in the context before starting
16    /// tracing. The tracing state is shared across all `tracing()` calls within
17    /// the same context.
18    ///
19    /// # Example
20    ///
21    /// ```
22    /// # #[cfg(feature = "integration")]
23    /// # tokio_test::block_on(async {
24    /// # use viewpoint_core::Browser;
25    /// use viewpoint_core::context::TracingOptions;
26    /// # let browser = Browser::launch().headless(true).launch().await.unwrap();
27    /// # let context = browser.new_context().await.unwrap();
28    ///
29    /// // Create a page first (required before starting tracing)
30    /// let page = context.new_page().await.unwrap();
31    ///
32    /// // Start tracing with screenshots
33    /// context.tracing().start(
34    ///     TracingOptions::new()
35    ///         .screenshots(true)
36    ///         .snapshots(true)
37    /// ).await.unwrap();
38    ///
39    /// // Perform test actions
40    /// page.goto("https://example.com").goto().await.unwrap();
41    ///
42    /// // Stop and save trace (state persists across tracing() calls)
43    /// context.tracing().stop("/tmp/trace.zip").await.unwrap();
44    /// # });
45    /// ```
46    pub fn tracing(&self) -> Tracing {
47        // The session_ids will be populated dynamically - the tracing listener
48        // will be attached to page sessions via the context's pages list.
49        // State is shared across all Tracing instances from the same context.
50        Tracing::new(
51            self.connection.clone(),
52            self.context_id.clone(),
53            self.pages.clone(),
54            self.tracing_state.clone(),
55        )
56    }
57}