playwright-rs 0.14.0

Rust bindings for Microsoft Playwright
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// Copyright 2026 Paul Adamson
// Licensed under the Apache License, Version 2.0
//
// Tracing — Playwright trace recording
//
// Architecture Reference:
// - Python: playwright-python/playwright/_impl/_tracing.py
// - JavaScript: playwright/packages/playwright-core/src/client/tracing.ts
// - Docs: https://playwright.dev/docs/api/class-tracing

//! Tracing — record Playwright traces for debugging
//!
//! Tracing is a per-context feature. Access the Tracing object via
//! [`BrowserContext::tracing`](crate::protocol::BrowserContext::tracing).
//!
//! # Example
//!
//! ```no_run
//! use playwright_rs::protocol::{Playwright, TracingStartOptions};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let playwright = Playwright::launch().await?;
//!     let browser = playwright.chromium().launch().await?;
//!     let context = browser.new_context().await?;
//!
//!     let tracing = context.tracing().await?;
//!
//!     // Start tracing with options
//!     tracing.start(Some(TracingStartOptions::default()
//!         .name("my-trace")
//!         .screenshots(true)
//!         .snapshots(true))).await?;
//!
//!     let page = context.new_page().await?;
//!     page.goto("https://example.com", None).await?;
//!
//!     // Stop and save the trace
//!     use playwright_rs::protocol::TracingStopOptions;
//!     tracing.stop(Some(TracingStopOptions::default()
//!         .path("/tmp/trace.zip"))).await?;
//!
//!     context.close().await?;
//!     browser.close().await?;
//!     Ok(())
//! }
//! ```
//!
//! See: <https://playwright.dev/docs/api/class-tracing>

use crate::error::Result;
use crate::protocol::har_options::StartHarOptions;
use crate::server::channel::Channel;
use crate::server::channel_owner::{
    ChannelOwner, ChannelOwnerImpl, DisposeReason, ParentOrConnection,
};
use crate::server::connection::ConnectionLike;
use serde_json::Value;
use std::any::Any;
use std::sync::Arc;

/// Options for starting a trace recording.
///
/// See: <https://playwright.dev/docs/api/class-tracing#tracing-start>
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct TracingStartOptions {
    /// Custom name for the trace. Shown in trace viewer as the trace title.
    pub name: Option<String>,
    /// Whether to capture screenshots during tracing. Screenshots are used as
    /// a timeline preview in the trace viewer.
    pub screenshots: Option<bool>,
    /// Whether to capture DOM snapshots on each action.
    pub snapshots: Option<bool>,
    /// Whether to enable live trace updates while recording. When `true`,
    /// the trace viewer can attach and observe the trace as it is being
    /// captured, rather than waiting for the recording to finish. Useful
    /// for debugging long-running flows.
    ///
    /// See: <https://playwright.dev/docs/api/class-tracing#tracing-start-option-live>
    pub live: Option<bool>,
}

impl TracingStartOptions {
    /// Trace name (affects file naming in the traces directory).
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }
    /// Capture screenshots during tracing.
    pub fn screenshots(mut self, screenshots: bool) -> Self {
        self.screenshots = Some(screenshots);
        self
    }
    /// Capture DOM snapshots during tracing.
    pub fn snapshots(mut self, snapshots: bool) -> Self {
        self.snapshots = Some(snapshots);
        self
    }
    /// Enable live tracing (view in the trace viewer while running).
    pub fn live(mut self, live: bool) -> Self {
        self.live = Some(live);
        self
    }
}

/// Options for stopping a trace recording.
///
/// See: <https://playwright.dev/docs/api/class-tracing#tracing-stop>
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct TracingStopOptions {
    /// Path to export the trace file to. If not provided, the trace is discarded.
    /// The file is written as a `.zip` archive.
    pub path: Option<String>,
}

impl TracingStopOptions {
    /// Export the trace to the given path.
    pub fn path(mut self, path: impl Into<String>) -> Self {
        self.path = Some(path.into());
        self
    }
}

/// In-flight HAR recording state, captured by `start_har` for `stop_har`.
struct HarRecording {
    har_id: Option<String>,
    path: String,
    resources_dir: Option<String>,
}

/// Tracing — records Playwright traces for debugging and inspection.
///
/// Trace files can be opened in the Playwright Trace Viewer.
/// This is a Chromium-only feature; calling tracing methods on Firefox or
/// WebKit contexts will fail.
///
/// See: <https://playwright.dev/docs/api/class-tracing>
#[derive(Clone)]
pub struct Tracing {
    base: ChannelOwnerImpl,
    /// Shared across clones so `start_har`/`stop_har` on the same context's
    /// `Tracing` see one recording. `stop_har` takes no path (matching the
    /// upstream API), so the path and `harId` are stashed here at start.
    har: Arc<parking_lot::Mutex<Option<HarRecording>>>,
}

impl Tracing {
    /// Creates a new Tracing from protocol initialization.
    ///
    /// Called by the object factory when the server sends a `__create__` message.
    pub fn new(
        parent: ParentOrConnection,
        type_name: String,
        guid: Arc<str>,
        initializer: Value,
    ) -> Result<Self> {
        Ok(Self {
            base: ChannelOwnerImpl::new(parent, type_name, guid, initializer),
            har: Arc::new(parking_lot::Mutex::new(None)),
        })
    }

    /// Start tracing.
    ///
    /// Playwright implements tracing as a two-step process: `tracingStart` to
    /// configure the trace, then `tracingStartChunk` to begin recording.
    ///
    /// # Arguments
    ///
    /// * `options` - Optional trace configuration (name, screenshots, snapshots)
    ///
    /// # Errors
    ///
    /// Returns error if:
    /// - Tracing is already active
    /// - Communication with browser process fails
    ///
    /// See: <https://playwright.dev/docs/api/class-tracing#tracing-start>
    #[tracing::instrument(level = "info", skip_all, fields(guid = %self.guid()))]
    pub async fn start(&self, options: Option<TracingStartOptions>) -> Result<()> {
        let opts = options.unwrap_or_default();

        // Step 1: tracingStart — configure the trace
        let mut start_params = serde_json::json!({});
        if let Some(ref name) = opts.name {
            start_params["name"] = serde_json::Value::String(name.clone());
        }
        if let Some(screenshots) = opts.screenshots {
            start_params["screenshots"] = serde_json::Value::Bool(screenshots);
        }
        if let Some(snapshots) = opts.snapshots {
            start_params["snapshots"] = serde_json::Value::Bool(snapshots);
        }
        if let Some(live) = opts.live {
            start_params["live"] = serde_json::Value::Bool(live);
        }

        self.channel()
            .send_no_result("tracingStart", start_params)
            .await?;

        // Step 2: tracingStartChunk — begin the chunk/recording
        let mut chunk_params = serde_json::json!({});
        if let Some(name) = opts.name {
            chunk_params["name"] = serde_json::Value::String(name);
        }

        self.channel()
            .send_no_result("tracingStartChunk", chunk_params)
            .await
    }

    /// Stop tracing.
    ///
    /// Playwright implements stopping as a two-step process: `tracingStopChunk`
    /// to finalize the recording, then `tracingStop` to tear down.
    ///
    /// If `options.path` is provided, the trace is exported to that file as a
    /// `.zip` archive. If no path is provided, the trace is discarded.
    ///
    /// # Arguments
    ///
    /// * `options` - Optional stop options; set `path` to save the trace to a file
    ///
    /// # Errors
    ///
    /// Returns error if:
    /// - Tracing was not active
    /// - Communication with browser process fails
    ///
    /// See: <https://playwright.dev/docs/api/class-tracing#tracing-stop>
    #[tracing::instrument(level = "info", skip_all, fields(guid = %self.guid()))]
    pub async fn stop(&self, options: Option<TracingStopOptions>) -> Result<()> {
        let path = options.and_then(|o| o.path);

        // Step 1: tracingStopChunk — mode "entries" collects trace data
        // mode "archive" or "compressedTrace" would export, but "entries" is simpler
        let mode = if path.is_some() { "archive" } else { "discard" };
        let stop_chunk_params = serde_json::json!({ "mode": mode });

        let chunk_result: Value = self
            .channel()
            .send("tracingStopChunk", stop_chunk_params)
            .await?;

        // Step 2: tracingStop — tear down
        self.channel()
            .send_no_result("tracingStop", serde_json::json!({}))
            .await?;

        // If a path was requested, save the artifact
        if let Some(dest_path) = path
            && let Some(artifact_guid) = chunk_result
                .get("artifact")
                .and_then(|a| a.get("guid"))
                .and_then(|g| g.as_str())
        {
            // Resolve the artifact and save it
            self.save_artifact(artifact_guid, &dest_path).await?;
        }

        Ok(())
    }

    /// Save a trace artifact to a file path.
    async fn save_artifact(&self, artifact_guid: &str, dest_path: &str) -> Result<()> {
        use crate::protocol::artifact::Artifact;
        use crate::server::connection::ConnectionExt;

        let artifact = self
            .connection()
            .get_typed::<Artifact>(artifact_guid)
            .await?;

        artifact.save_as(dest_path).await
    }

    /// Start recording a HAR (HTTP Archive) of network traffic to `path`.
    ///
    /// The HAR is written when [`stop_har`](Self::stop_har) is called. A `.zip`
    /// path bundles resource bodies as separate entries (`Attach`); a plain
    /// path inlines them (`Embed`). The recorded HAR can be opened in browser
    /// devtools or replayed in tests via `route_from_har`.
    ///
    /// # Errors
    ///
    /// Returns an error if communication with the browser process fails.
    ///
    /// See: <https://playwright.dev/docs/api/class-tracing#tracing-start-har>
    #[tracing::instrument(level = "info", skip_all, fields(guid = %self.guid()))]
    pub async fn start_har(
        &self,
        path: impl Into<String>,
        options: Option<StartHarOptions>,
    ) -> Result<()> {
        let path = path.into();
        let opts = options.unwrap_or_default();
        let rec_options = opts.to_record_har_json(&path);

        let result: Value = self
            .channel()
            .send("harStart", serde_json::json!({ "options": rec_options }))
            .await?;
        let har_id = result
            .get("harId")
            .and_then(|v| v.as_str())
            .map(str::to_owned);

        *self.har.lock() = Some(HarRecording {
            har_id,
            path,
            resources_dir: opts.resources_dir,
        });
        Ok(())
    }

    /// Stop the HAR recording started by [`start_har`](Self::start_har) and
    /// write it to the path given there.
    ///
    /// # Errors
    ///
    /// Returns an error if `start_har` was not called first, or if
    /// communication with the browser process fails.
    ///
    /// See: <https://playwright.dev/docs/api/class-tracing#tracing-stop-har>
    #[tracing::instrument(level = "info", skip_all, fields(guid = %self.guid()))]
    pub async fn stop_har(&self) -> Result<()> {
        let Some(recording) = self.har.lock().take() else {
            return Err(crate::error::Error::InvalidArgument(
                "stop_har called without a matching start_har".to_string(),
            ));
        };

        let mut params = serde_json::json!({ "mode": "archive" });
        if let Some(id) = &recording.har_id {
            params["harId"] = Value::String(id.clone());
        }

        let result: Value = self.channel().send("harExport", params).await?;

        let Some(artifact_guid) = result
            .get("artifact")
            .and_then(|a| a.get("guid"))
            .and_then(|g| g.as_str())
        else {
            return Ok(());
        };

        // harExport always yields a zip archive. A `.zip` destination takes it
        // verbatim; any other path gets the `.har` JSON extracted out of it.
        if recording.path.ends_with(".zip") {
            self.save_artifact(artifact_guid, &recording.path).await?;
        } else {
            let tmp_zip = format!("{}.tmp.zip", recording.path);
            self.save_artifact(artifact_guid, &tmp_zip).await?;
            let local_utils = self.find_local_utils()?;
            local_utils
                .har_unzip(
                    &tmp_zip,
                    &recording.path,
                    recording.resources_dir.as_deref(),
                )
                .await?;
            let _ = std::fs::remove_file(&tmp_zip);
        }

        Ok(())
    }

    /// Locate the connection's `LocalUtils` (used to extract a `.har` from the
    /// exported zip archive).
    fn find_local_utils(&self) -> Result<crate::protocol::LocalUtils> {
        let connection = self.connection();
        connection
            .all_objects_sync()
            .into_iter()
            .find(|o| o.type_name() == "LocalUtils")
            .and_then(|o| {
                o.as_any()
                    .downcast_ref::<crate::protocol::LocalUtils>()
                    .cloned()
            })
            .ok_or_else(|| {
                crate::error::Error::ProtocolError(
                    "stop_har: LocalUtils not found in connection registry".to_string(),
                )
            })
    }
}

impl ChannelOwner for Tracing {
    fn guid(&self) -> &str {
        self.base.guid()
    }

    fn type_name(&self) -> &str {
        self.base.type_name()
    }

    fn parent(&self) -> Option<Arc<dyn ChannelOwner>> {
        self.base.parent()
    }

    fn connection(&self) -> Arc<dyn ConnectionLike> {
        self.base.connection()
    }

    fn initializer(&self) -> &Value {
        self.base.initializer()
    }

    fn channel(&self) -> &Channel {
        self.base.channel()
    }

    fn dispose(&self, reason: DisposeReason) {
        self.base.dispose(reason)
    }

    fn adopt(&self, child: Arc<dyn ChannelOwner>) {
        self.base.adopt(child)
    }

    fn add_child(&self, guid: Arc<str>, child: Arc<dyn ChannelOwner>) {
        self.base.add_child(guid, child)
    }

    fn remove_child(&self, guid: &str) {
        self.base.remove_child(guid)
    }

    fn on_event(&self, method: &str, params: Value) {
        self.base.on_event(method, params)
    }

    fn was_collected(&self) -> bool {
        self.base.was_collected()
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

impl std::fmt::Debug for Tracing {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Tracing")
            .field("guid", &self.guid())
            .finish()
    }
}