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 30000 (30 seconds). Pass 0 to disable timeout.
19    pub timeout: Option<f64>,
20    /// Disables Playwright's default overrides (download behavior, focus/media
21    /// emulation) when attaching to an already-running browser — for "attach
22    /// without disturbing state" workflows.
23    pub no_defaults: Option<bool>,
24    /// Directory where traces and downloads captured over this CDP connection
25    /// are stored.
26    pub artifacts_dir: Option<String>,
27}
28
29impl ConnectOverCdpOptions {
30    /// Creates a new `ConnectOverCdpOptions` with default values.
31    pub fn new() -> Self {
32        Self::default()
33    }
34
35    /// Disable Playwright's default overrides when attaching to a running browser.
36    pub fn no_defaults(mut self, no_defaults: bool) -> Self {
37        self.no_defaults = Some(no_defaults);
38        self
39    }
40
41    /// Set the directory for traces and downloads captured over this CDP
42    /// connection.
43    pub fn artifacts_dir(mut self, artifacts_dir: impl Into<String>) -> Self {
44        self.artifacts_dir = Some(artifacts_dir.into());
45        self
46    }
47
48    /// Set additional HTTP headers to send with the connection request.
49    pub fn headers(mut self, headers: HashMap<String, String>) -> Self {
50        self.headers = Some(headers);
51        self
52    }
53
54    /// Set slow mo delay in milliseconds.
55    pub fn slow_mo(mut self, slow_mo: f64) -> Self {
56        self.slow_mo = Some(slow_mo);
57        self
58    }
59
60    /// Set connection timeout in milliseconds.
61    pub fn timeout(mut self, timeout: f64) -> Self {
62        self.timeout = Some(timeout);
63        self
64    }
65}
66
67/// Options for `BrowserType::connect`.
68#[derive(Debug, Clone, Default)]
69#[non_exhaustive]
70pub struct ConnectOptions {
71    /// Additional HTTP headers to send with the WebSocket handshake.
72    pub headers: Option<HashMap<String, String>>,
73    /// Slows down Playwright operations by the specified amount of milliseconds.
74    /// Useful so that you can see what is going on.
75    pub slow_mo: Option<f64>,
76    /// Maximum time in milliseconds to wait for the connection to be established.
77    /// Defaults to 30000 (30 seconds). Pass 0 to disable timeout.
78    pub timeout: Option<f64>,
79}
80
81impl ConnectOptions {
82    /// Creates a new `ConnectOptions` with default values.
83    pub fn new() -> Self {
84        Self::default()
85    }
86
87    /// Set additional HTTP headers to send with the WebSocket handshake.
88    pub fn headers(mut self, headers: HashMap<String, String>) -> Self {
89        self.headers = Some(headers);
90        self
91    }
92
93    /// Set slow mo delay in milliseconds.
94    pub fn slow_mo(mut self, slow_mo: f64) -> Self {
95        self.slow_mo = Some(slow_mo);
96        self
97    }
98
99    /// Set connection timeout in milliseconds.
100    pub fn timeout(mut self, timeout: f64) -> Self {
101        self.timeout = Some(timeout);
102        self
103    }
104}