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}
25
26impl ConnectOverCdpOptions {
27    /// Creates a new `ConnectOverCdpOptions` with default values.
28    pub fn new() -> Self {
29        Self::default()
30    }
31
32    /// Disable Playwright's default overrides when attaching to a running browser.
33    pub fn no_defaults(mut self, no_defaults: bool) -> Self {
34        self.no_defaults = Some(no_defaults);
35        self
36    }
37
38    /// Set additional HTTP headers to send with the connection request.
39    pub fn headers(mut self, headers: HashMap<String, String>) -> Self {
40        self.headers = Some(headers);
41        self
42    }
43
44    /// Set slow mo delay in milliseconds.
45    pub fn slow_mo(mut self, slow_mo: f64) -> Self {
46        self.slow_mo = Some(slow_mo);
47        self
48    }
49
50    /// Set connection timeout in milliseconds.
51    pub fn timeout(mut self, timeout: f64) -> Self {
52        self.timeout = Some(timeout);
53        self
54    }
55}
56
57/// Options for `BrowserType::connect`.
58#[derive(Debug, Clone, Default)]
59#[non_exhaustive]
60pub struct ConnectOptions {
61    /// Additional HTTP headers to send with the WebSocket handshake.
62    pub headers: Option<HashMap<String, String>>,
63    /// Slows down Playwright operations by the specified amount of milliseconds.
64    /// Useful so that you can see what is going on.
65    pub slow_mo: Option<f64>,
66    /// Maximum time in milliseconds to wait for the connection to be established.
67    /// Defaults to 30000 (30 seconds). Pass 0 to disable timeout.
68    pub timeout: Option<f64>,
69}
70
71impl ConnectOptions {
72    /// Creates a new `ConnectOptions` with default values.
73    pub fn new() -> Self {
74        Self::default()
75    }
76
77    /// Set additional HTTP headers to send with the WebSocket handshake.
78    pub fn headers(mut self, headers: HashMap<String, String>) -> Self {
79        self.headers = Some(headers);
80        self
81    }
82
83    /// Set slow mo delay in milliseconds.
84    pub fn slow_mo(mut self, slow_mo: f64) -> Self {
85        self.slow_mo = Some(slow_mo);
86        self
87    }
88
89    /// Set connection timeout in milliseconds.
90    pub fn timeout(mut self, timeout: f64) -> Self {
91        self.timeout = Some(timeout);
92        self
93    }
94}