browser_use/browser/
config.rs

1use std::path::PathBuf;
2
3/// Options for launching a new browser instance
4#[derive(Debug, Clone)]
5pub struct LaunchOptions {
6    /// Whether to run browser in headless mode (default: true)
7    pub headless: bool,
8
9    /// Custom Chrome/Chromium binary path
10    pub chrome_path: Option<PathBuf>,
11
12    /// Browser window width (default: 1280)
13    pub window_width: u32,
14
15    /// Browser window height (default: 720)
16    pub window_height: u32,
17
18    /// User data directory for browser profile
19    pub user_data_dir: Option<PathBuf>,
20
21    /// Enable sandbox mode (default: true)
22    pub sandbox: bool,
23
24    /// Timeout for browser launch in milliseconds (default: 30000)
25    pub launch_timeout: u64,
26}
27
28impl Default for LaunchOptions {
29    fn default() -> Self {
30        Self {
31            headless: true,
32            chrome_path: None,
33            window_width: 1280,
34            window_height: 720,
35            user_data_dir: None,
36            sandbox: true,
37            launch_timeout: 30000,
38        }
39    }
40}
41
42impl LaunchOptions {
43    /// Create new LaunchOptions with default values
44    pub fn new() -> Self {
45        Self::default()
46    }
47
48    /// Builder method: set headless mode
49    pub fn headless(mut self, headless: bool) -> Self {
50        self.headless = headless;
51        self
52    }
53
54    /// Builder method: set Chrome binary path
55    pub fn chrome_path(mut self, path: PathBuf) -> Self {
56        self.chrome_path = Some(path);
57        self
58    }
59
60    /// Builder method: set window dimensions
61    pub fn window_size(mut self, width: u32, height: u32) -> Self {
62        self.window_width = width;
63        self.window_height = height;
64        self
65    }
66
67    /// Builder method: set user data directory
68    pub fn user_data_dir(mut self, dir: PathBuf) -> Self {
69        self.user_data_dir = Some(dir);
70        self
71    }
72
73    /// Builder method: enable/disable sandbox
74    pub fn sandbox(mut self, sandbox: bool) -> Self {
75        self.sandbox = sandbox;
76        self
77    }
78
79    /// Builder method: set launch timeout
80    pub fn launch_timeout(mut self, timeout_ms: u64) -> Self {
81        self.launch_timeout = timeout_ms;
82        self
83    }
84}
85
86/// Options for connecting to an existing browser instance
87#[derive(Debug, Clone)]
88pub struct ConnectionOptions {
89    /// WebSocket URL for Chrome DevTools Protocol
90    pub ws_url: String,
91
92    /// Connection timeout in milliseconds (default: 10000)
93    pub timeout: u64,
94}
95
96impl ConnectionOptions {
97    /// Create new ConnectionOptions with WebSocket URL
98    pub fn new<S: Into<String>>(ws_url: S) -> Self {
99        Self {
100            ws_url: ws_url.into(),
101            timeout: 10000,
102        }
103    }
104
105    /// Builder method: set connection timeout
106    pub fn timeout(mut self, timeout_ms: u64) -> Self {
107        self.timeout = timeout_ms;
108        self
109    }
110}
111
112#[cfg(test)]
113mod tests {
114    use super::*;
115
116    #[test]
117    fn test_launch_options_default() {
118        let opts = LaunchOptions::default();
119        assert!(opts.headless);
120        assert_eq!(opts.window_width, 1280);
121        assert_eq!(opts.window_height, 720);
122        assert!(opts.sandbox);
123        assert_eq!(opts.launch_timeout, 30000);
124    }
125
126    #[test]
127    fn test_launch_options_builder() {
128        let opts = LaunchOptions::new()
129            .headless(false)
130            .window_size(1920, 1080)
131            .sandbox(false)
132            .launch_timeout(60000);
133
134        assert!(!opts.headless);
135        assert_eq!(opts.window_width, 1920);
136        assert_eq!(opts.window_height, 1080);
137        assert!(!opts.sandbox);
138        assert_eq!(opts.launch_timeout, 60000);
139    }
140
141    #[test]
142    fn test_connection_options() {
143        let opts = ConnectionOptions::new("ws://localhost:9222").timeout(5000);
144
145        assert_eq!(opts.ws_url, "ws://localhost:9222");
146        assert_eq!(opts.timeout, 5000);
147    }
148}