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
//! Browser configuration options.
use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
/// Configuration for browser sessions.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BrowserConfig {
/// WebDriver server URL (e.g., "http://localhost:4444")
pub webdriver_url: String,
/// Browser type to use
pub browser: BrowserType,
/// Run in headless mode (no visible window)
pub headless: bool,
/// Viewport width in pixels
pub viewport_width: u32,
/// Viewport height in pixels
pub viewport_height: u32,
/// Page load timeout in seconds
pub page_load_timeout_secs: u64,
/// Script execution timeout in seconds
pub script_timeout_secs: u64,
/// Implicit wait timeout in seconds
pub implicit_wait_secs: u64,
/// User agent string override
pub user_agent: Option<String>,
/// Additional browser arguments
pub browser_args: Vec<String>,
/// Require the host to start the session, and to start it again after it is lost,
/// instead of tools starting or recreating it on first use.
#[serde(default)]
pub require_explicit_start: bool,
/// Chrome options under `goog:chromeOptions`, such as `binary` and `prefs`, supplied by
/// the execution host. Chrome only. Command-line flags belong in `browser_args`; an
/// `args` entry here is rejected when the session starts.
#[serde(default)]
pub chrome_options: BTreeMap<String, serde_json::Value>,
}
/// Supported browser types.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum BrowserType {
Chrome,
Firefox,
Safari,
Edge,
}
impl Default for BrowserConfig {
fn default() -> Self {
Self {
webdriver_url: "http://localhost:4444".to_string(),
browser: BrowserType::Chrome,
headless: true,
viewport_width: 1920,
viewport_height: 1080,
page_load_timeout_secs: 30,
script_timeout_secs: 30,
implicit_wait_secs: 10,
user_agent: None,
browser_args: Vec::new(),
chrome_options: Default::default(),
require_explicit_start: false,
}
}
}
impl BrowserConfig {
/// Create a new config with default values.
pub fn new() -> Self {
Self::default()
}
/// Set the WebDriver URL.
pub fn webdriver_url(mut self, url: impl Into<String>) -> Self {
self.webdriver_url = url.into();
self
}
/// Set the browser type.
pub fn browser(mut self, browser: BrowserType) -> Self {
self.browser = browser;
self
}
/// Enable or disable headless mode.
pub fn headless(mut self, headless: bool) -> Self {
self.headless = headless;
self
}
/// Set the viewport size.
pub fn viewport(mut self, width: u32, height: u32) -> Self {
self.viewport_width = width;
self.viewport_height = height;
self
}
/// Set page load timeout.
pub fn page_load_timeout(mut self, secs: u64) -> Self {
self.page_load_timeout_secs = secs;
self
}
/// Set a custom user agent.
pub fn user_agent(mut self, ua: impl Into<String>) -> Self {
self.user_agent = Some(ua.into());
self
}
/// Add a browser argument.
pub fn add_arg(mut self, arg: impl Into<String>) -> Self {
self.browser_args.push(arg.into());
self
}
/// Require explicit `BrowserSession::start()` calls instead of automatic startup.
///
/// # Example
///
/// ```
/// use adk_browser::BrowserConfig;
///
/// let config = BrowserConfig::new().require_explicit_start(true);
/// assert!(config.require_explicit_start);
/// ```
pub fn require_explicit_start(mut self, required: bool) -> Self {
self.require_explicit_start = required;
self
}
/// Set a Chrome option under `goog:chromeOptions`, such as `binary` or `prefs`.
///
/// Command-line flags belong in [`add_arg`](Self::add_arg); an `args` option is
/// rejected when the session starts.
///
/// # Example
///
/// ```
/// use adk_browser::BrowserConfig;
///
/// let config = BrowserConfig::new()
/// .chrome_option("binary", serde_json::json!("/opt/chromium"))
/// .chrome_option("prefs", serde_json::json!({"download.default_directory": "/tmp/dl"}));
/// assert_eq!(config.chrome_options["binary"], "/opt/chromium");
/// ```
pub fn chrome_option(mut self, name: impl Into<String>, value: serde_json::Value) -> Self {
self.chrome_options.insert(name.into(), value);
self
}
/// Configure for observable mode (noVNC-compatible).
///
/// Sets `headless: false` and viewport to 1280x720 for noVNC viewing.
/// Use this when running with Selenium's noVNC viewer (port 7900).
pub fn observable(self) -> Self {
self.headless(false).viewport(1280, 720)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = BrowserConfig::default();
assert_eq!(config.webdriver_url, "http://localhost:4444");
assert!(config.headless);
assert_eq!(config.viewport_width, 1920);
}
#[test]
fn test_builder() {
let config =
BrowserConfig::new().browser(BrowserType::Firefox).headless(false).viewport(1280, 720);
assert_eq!(config.browser, BrowserType::Firefox);
assert!(!config.headless);
assert_eq!(config.viewport_width, 1280);
}
#[test]
fn test_observable() {
let config = BrowserConfig::new().observable();
assert!(!config.headless);
assert_eq!(config.viewport_width, 1280);
assert_eq!(config.viewport_height, 720);
}
}