Skip to main content

playwright_rs/api/
connect_options.rs

1use std::collections::HashMap;
2
3/// Options for `BrowserType::connect_over_cdp`.
4///
5/// Only supported for Chromium. Allows connecting to a Chrome DevTools Protocol endpoint,
6/// such as those provided by browserless, Chrome with `--remote-debugging-port`, or other
7/// CDP-compatible services.
8///
9/// See: <https://playwright.dev/docs/api/class-browsertype#browser-type-connect-over-cdp>
10#[derive(Debug, Clone, Default)]
11#[non_exhaustive]
12pub struct ConnectOverCdpOptions {
13    /// Additional HTTP headers to be sent with the connection request.
14    pub headers: Option<HashMap<String, String>>,
15    /// Slows down Playwright operations by the specified amount of milliseconds.
16    pub slow_mo: Option<f64>,
17    /// Maximum time in milliseconds to wait for the connection to be established.
18    /// Defaults to [`DEFAULT_LAUNCH_TIMEOUT_MS`](crate::DEFAULT_LAUNCH_TIMEOUT_MS)
19    /// (three minutes). Pass 0 to disable timeout.
20    pub timeout: Option<f64>,
21    /// Disables Playwright's default overrides (download behavior, focus/media
22    /// emulation) when attaching to an already-running browser — for "attach
23    /// without disturbing state" workflows.
24    pub no_defaults: Option<bool>,
25    /// Directory where traces and downloads captured over this CDP connection
26    /// are stored.
27    pub artifacts_dir: Option<String>,
28}
29
30impl ConnectOverCdpOptions {
31    /// Creates a new `ConnectOverCdpOptions` with default values.
32    pub fn new() -> Self {
33        Self::default()
34    }
35
36    /// Disable Playwright's default overrides when attaching to a running browser.
37    pub fn no_defaults(mut self, no_defaults: bool) -> Self {
38        self.no_defaults = Some(no_defaults);
39        self
40    }
41
42    /// Set the directory for traces and downloads captured over this CDP
43    /// connection.
44    pub fn artifacts_dir(mut self, artifacts_dir: impl Into<String>) -> Self {
45        self.artifacts_dir = Some(artifacts_dir.into());
46        self
47    }
48
49    /// Set additional HTTP headers to send with the connection request.
50    pub fn headers(mut self, headers: HashMap<String, String>) -> Self {
51        self.headers = Some(headers);
52        self
53    }
54
55    /// Set slow mo delay in milliseconds.
56    pub fn slow_mo(mut self, slow_mo: f64) -> Self {
57        self.slow_mo = Some(slow_mo);
58        self
59    }
60
61    /// Set connection timeout in milliseconds.
62    pub fn timeout(mut self, timeout: f64) -> Self {
63        self.timeout = Some(timeout);
64        self
65    }
66}
67
68/// Options for `BrowserType::connect`.
69#[derive(Debug, Clone, Default)]
70#[non_exhaustive]
71pub struct ConnectOptions {
72    /// Additional HTTP headers to send with the WebSocket handshake.
73    pub headers: Option<HashMap<String, String>>,
74    /// Slows down Playwright operations by the specified amount of milliseconds.
75    /// Useful so that you can see what is going on.
76    pub slow_mo: Option<f64>,
77    /// Maximum time in milliseconds to wait for the connection to be established.
78    /// Defaults to 30000 (30 seconds). Pass 0 to disable timeout.
79    pub timeout: Option<f64>,
80}
81
82impl ConnectOptions {
83    /// Creates a new `ConnectOptions` with default values.
84    pub fn new() -> Self {
85        Self::default()
86    }
87
88    /// Set additional HTTP headers to send with the WebSocket handshake.
89    pub fn headers(mut self, headers: HashMap<String, String>) -> Self {
90        self.headers = Some(headers);
91        self
92    }
93
94    /// Set slow mo delay in milliseconds.
95    pub fn slow_mo(mut self, slow_mo: f64) -> Self {
96        self.slow_mo = Some(slow_mo);
97        self
98    }
99
100    /// Set connection timeout in milliseconds.
101    pub fn timeout(mut self, timeout: f64) -> Self {
102        self.timeout = Some(timeout);
103        self
104    }
105}