rpage 1.0.0

A Rust browser automation library inspired by DrissionPage
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Configuration types for rpage

use std::path::PathBuf;
use std::time::Duration;

/// Default timeout in seconds
const DEFAULT_TIMEOUT_SECS: u64 = 10;

/// The debug port `ChromiumPage::new()` uses for its PID-derived, per-process
/// browser session. Exposed publicly so other in-process consumers that want
/// to attach to that same browser (rather than launch a separate one) can
/// compute the identical port instead of guessing or hardcoding one.
pub fn default_debug_port() -> u16 {
    9300 + (std::process::id() as u16 % 700)
}

/// Viewport dimensions
#[derive(Debug, Clone)]
pub struct Viewport {
    pub width: u32,
    pub height: u32,
}

impl Default for Viewport {
    fn default() -> Self {
        Self {
            width: 1920,
            height: 1080,
        }
    }
}

/// Options for ChromiumPage (browser mode)
#[derive(Debug, Clone)]
pub struct ChromiumOptions {
    /// Global timeout for operations
    pub timeout: Duration,
    /// User-Agent string (empty = use browser default)
    pub user_agent: String,
    /// Viewport size
    pub viewport: Viewport,
    /// Run in headless mode
    pub headless: bool,
    /// Proxy URL, e.g. "http://127.0.0.1:7890"
    pub proxy: Option<String>,
    /// Proxy authentication credentials: (username, password)
    pub proxy_auth: Option<(String, String)>,
    /// Path to Chrome/Chromium binary
    pub browser_path: Option<PathBuf>,
    /// User data directory for persistent profiles
    pub user_data_dir: Option<PathBuf>,
    /// Extension directories to load
    pub extension_dirs: Vec<PathBuf>,
    /// Disable GPU
    pub disable_gpu: bool,
    /// Disable sandbox (needed in some CI)
    pub no_sandbox: bool,
    /// Additional Chrome arguments
    pub extra_args: Vec<String>,
    /// Debug port for CDP connection (default: 9222)
    pub debug_port: u16,
    /// Enable Network/Runtime CDP domains and their event listeners
    /// (request/download/console/exception/WebSocket-frame monitoring).
    ///
    /// This costs a handful of extra CDP round-trips on connect, and —
    /// because enabling the `Network` domain makes Chrome start streaming
    /// every network sub-event over the same connection — is the dominant
    /// source of chromiumoxide's harmless-but-noisy "WS Invalid message"
    /// warnings when chromiumoxide's CDP schema doesn't cover some sub-event
    /// shape the running Chrome version emits (see f33/the network/download
    /// monitor fields). Defaults to `true` to preserve existing behavior for
    /// callers using `with_options()`; `ChromiumPage::new()` (`创建浏览器()`)
    /// turns it off, since its whole point is a fast, low-overhead, visible
    /// debug session, not background telemetry.
    pub enable_monitoring: bool,
}

impl Default for ChromiumOptions {
    fn default() -> Self {
        Self {
            timeout: Duration::from_secs(DEFAULT_TIMEOUT_SECS),
            user_agent: String::new(),
            viewport: Viewport::default(),
            headless: true,
            proxy: None,
            proxy_auth: None,
            browser_path: None,
            user_data_dir: None,
            extension_dirs: Vec::new(),
            disable_gpu: true,
            no_sandbox: false,
            extra_args: Vec::new(),
            debug_port: 9222,
            enable_monitoring: true,
        }
    }
}

impl ChromiumOptions {
    pub fn builder() -> ChromiumOptionsBuilder {
        ChromiumOptionsBuilder::default()
    }
}

/// Builder for ChromiumOptions
#[derive(Default)]
pub struct ChromiumOptionsBuilder {
    opts: ChromiumOptions,
}

impl ChromiumOptionsBuilder {
    pub fn timeout(mut self, d: Duration) -> Self {
        self.opts.timeout = d;
        self
    }

    pub fn user_agent(mut self, ua: impl Into<String>) -> Self {
        self.opts.user_agent = ua.into();
        self
    }

    pub fn viewport(mut self, w: u32, h: u32) -> Self {
        self.opts.viewport = Viewport {
            width: w,
            height: h,
        };
        self
    }

    pub fn headless(mut self, v: bool) -> Self {
        self.opts.headless = v;
        self
    }

    pub fn proxy(mut self, p: impl Into<String>) -> Self {
        self.opts.proxy = Some(p.into());
        self
    }

    pub fn proxy_auth(mut self, user: impl Into<String>, pass: impl Into<String>) -> Self {
        self.opts.proxy_auth = Some((user.into(), pass.into()));
        self
    }

    pub fn browser_path(mut self, p: impl Into<PathBuf>) -> Self {
        self.opts.browser_path = Some(p.into());
        self
    }

    pub fn user_data_dir(mut self, p: impl Into<PathBuf>) -> Self {
        self.opts.user_data_dir = Some(p.into());
        self
    }

    pub fn extension_dir(mut self, p: impl Into<PathBuf>) -> Self {
        self.opts.extension_dirs.push(p.into());
        self
    }

    pub fn no_sandbox(mut self, v: bool) -> Self {
        self.opts.no_sandbox = v;
        self
    }

    pub fn disable_gpu(mut self, v: bool) -> Self {
        self.opts.disable_gpu = v;
        self
    }

    pub fn debug_port(mut self, port: u16) -> Self {
        self.opts.debug_port = port;
        self
    }

    pub fn arg(mut self, a: impl Into<String>) -> Self {
        self.opts.extra_args.push(a.into());
        self
    }

    pub fn enable_monitoring(mut self, v: bool) -> Self {
        self.opts.enable_monitoring = v;
        self
    }

    pub fn build(self) -> ChromiumOptions {
        self.opts
    }
}

/// Options for SessionPage (HTTP mode)
#[derive(Debug, Clone)]
pub struct SessionOptions {
    /// Global timeout for HTTP requests
    pub timeout: Duration,
    /// User-Agent string
    pub user_agent: String,
    /// Proxy URL
    pub proxy: Option<String>,
    /// Accept invalid TLS certificates
    pub accept_invalid_certs: bool,
    /// Follow redirects
    pub follow_redirects: bool,
    /// Max redirects
    pub max_redirects: usize,
}

impl Default for SessionOptions {
    fn default() -> Self {
        Self {
            timeout: Duration::from_secs(DEFAULT_TIMEOUT_SECS),
            user_agent: String::from(
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 \
                 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
            ),
            proxy: None,
            accept_invalid_certs: false,
            follow_redirects: true,
            max_redirects: 10,
        }
    }
}

impl SessionOptions {
    pub fn builder() -> SessionOptionsBuilder {
        SessionOptionsBuilder::default()
    }
}

/// Builder for SessionOptions
#[derive(Default)]
pub struct SessionOptionsBuilder {
    opts: SessionOptions,
}

impl SessionOptionsBuilder {
    pub fn timeout(mut self, d: Duration) -> Self {
        self.opts.timeout = d;
        self
    }

    pub fn user_agent(mut self, ua: impl Into<String>) -> Self {
        self.opts.user_agent = ua.into();
        self
    }

    pub fn proxy(mut self, p: impl Into<String>) -> Self {
        self.opts.proxy = Some(p.into());
        self
    }

    pub fn accept_invalid_certs(mut self, v: bool) -> Self {
        self.opts.accept_invalid_certs = v;
        self
    }

    pub fn build(self) -> SessionOptions {
        self.opts
    }
}

/// Unified options for WebPage (wraps both Chromium and Session options)
#[derive(Debug, Clone)]
pub struct WebPageOptions {
    /// Chromium-specific options
    pub chromium: ChromiumOptions,
    /// Session-specific options
    pub session: SessionOptions,
    /// Initial mode (defaults to Chromium)
    pub initial_mode: crate::web_page::PageMode,
}

impl Default for WebPageOptions {
    fn default() -> Self {
        Self {
            chromium: ChromiumOptions::default(),
            session: SessionOptions::default(),
            initial_mode: crate::web_page::PageMode::Chromium,
        }
    }
}

impl WebPageOptions {
    pub fn builder() -> WebPageOptionsBuilder {
        WebPageOptionsBuilder::default()
    }
}

/// Builder for WebPageOptions
#[derive(Default)]
pub struct WebPageOptionsBuilder {
    opts: WebPageOptions,
}

impl WebPageOptionsBuilder {
    pub fn chromium(mut self, c: ChromiumOptions) -> Self {
        self.opts.chromium = c;
        self
    }

    pub fn session(mut self, s: SessionOptions) -> Self {
        self.opts.session = s;
        self
    }

    pub fn initial_mode(mut self, m: super::web_page::PageMode) -> Self {
        self.opts.initial_mode = m;
        self
    }

    pub fn build(self) -> WebPageOptions {
        self.opts
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_chromium_options_default_values() {
        let opts = ChromiumOptions::default();
        assert_eq!(opts.timeout, Duration::from_secs(10));
        assert!(opts.user_agent.is_empty());
        assert!(opts.headless);
        assert!(opts.proxy.is_none());
        assert!(opts.proxy_auth.is_none());
        assert!(opts.browser_path.is_none());
        assert!(opts.user_data_dir.is_none());
        assert!(opts.extension_dirs.is_empty());
        assert!(opts.disable_gpu);
        assert!(!opts.no_sandbox);
        assert!(opts.extra_args.is_empty());
        assert_eq!(opts.debug_port, 9222);
    }

    #[test]
    fn test_chromium_options_default_viewport() {
        let opts = ChromiumOptions::default();
        assert_eq!(opts.viewport.width, 1920);
        assert_eq!(opts.viewport.height, 1080);
    }

    #[test]
    fn test_viewport_default() {
        let vp = Viewport::default();
        assert_eq!(vp.width, 1920);
        assert_eq!(vp.height, 1080);
    }

    #[test]
    fn test_session_options_default_values() {
        let opts = SessionOptions::default();
        assert_eq!(opts.timeout, Duration::from_secs(10));
        assert!(opts.user_agent.contains("Chrome"));
        assert!(opts.proxy.is_none());
        assert!(!opts.accept_invalid_certs);
        assert!(opts.follow_redirects);
        assert_eq!(opts.max_redirects, 10);
    }

    #[test]
    fn test_chromium_options_builder_custom() {
        let opts = ChromiumOptions::builder()
            .timeout(Duration::from_secs(30))
            .headless(false)
            .no_sandbox(true)
            .disable_gpu(false)
            .proxy("http://127.0.0.1:7890")
            .proxy_auth("user", "pass")
            .browser_path("/usr/bin/chromium")
            .user_data_dir("/tmp/profile")
            .extension_dir("/path/to/ext")
            .arg("--disable-extensions")
            .build();
        assert_eq!(opts.timeout, Duration::from_secs(30));
        assert!(!opts.headless);
        assert!(opts.no_sandbox);
        assert!(!opts.disable_gpu);
        assert_eq!(opts.proxy.as_deref(), Some("http://127.0.0.1:7890"));
        assert_eq!(
            opts.proxy_auth.as_ref(),
            Some(&(String::from("user"), String::from("pass")))
        );
        assert_eq!(
            opts.browser_path.as_deref(),
            Some(std::path::Path::new("/usr/bin/chromium"))
        );
        assert_eq!(opts.extension_dirs.len(), 1);
        assert_eq!(opts.extra_args.len(), 1);
        assert_eq!(opts.extra_args[0], "--disable-extensions");
    }

    #[test]
    fn test_session_options_builder_custom() {
        let opts = SessionOptions::builder()
            .timeout(Duration::from_secs(60))
            .user_agent("CustomAgent/1.0")
            .proxy("socks5://localhost:9050")
            .accept_invalid_certs(true)
            .build();
        assert_eq!(opts.timeout, Duration::from_secs(60));
        assert_eq!(opts.user_agent, "CustomAgent/1.0");
        assert_eq!(opts.proxy.as_deref(), Some("socks5://localhost:9050"));
        assert!(opts.accept_invalid_certs);
    }

    #[test]
    fn test_web_page_options_default() {
        let opts = WebPageOptions::default();
        assert_eq!(opts.initial_mode, crate::web_page::PageMode::Chromium);
    }

    #[test]
    fn test_web_page_options_builder_session_mode() {
        let opts = WebPageOptions::builder()
            .initial_mode(crate::web_page::PageMode::Session)
            .build();
        assert_eq!(opts.initial_mode, crate::web_page::PageMode::Session);
    }
}