Skip to main content

spider/features/
webdriver_common.rs

1use std::time::Duration;
2
3/// The supported WebDriver browser types.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, strum::EnumString, strum::Display)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum WebDriverBrowser {
7    #[default]
8    #[cfg_attr(feature = "serde", serde(rename = "chrome"))]
9    /// Google Chrome browser.
10    Chrome,
11    #[cfg_attr(feature = "serde", serde(rename = "firefox"))]
12    /// Mozilla Firefox browser.
13    Firefox,
14    #[cfg_attr(feature = "serde", serde(rename = "edge"))]
15    /// Microsoft Edge browser.
16    Edge,
17}
18
19/// Configuration for WebDriver connections.
20#[derive(Debug, Clone, PartialEq)]
21#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
22pub struct WebDriverConfig {
23    /// The WebDriver server URL (e.g., "http://localhost:4444").
24    pub server_url: String,
25    /// The browser to use for WebDriver sessions.
26    pub browser: WebDriverBrowser,
27    /// Run the browser in headless mode.
28    pub headless: bool,
29    /// Custom browser arguments.
30    pub browser_args: Option<Vec<String>>,
31    /// Request timeout for WebDriver commands.
32    pub timeout: Option<Duration>,
33    /// Proxy server URL.
34    pub proxy: Option<String>,
35    /// User agent string to use.
36    pub user_agent: Option<String>,
37    /// Viewport width.
38    pub viewport_width: Option<u32>,
39    /// Viewport height.
40    pub viewport_height: Option<u32>,
41    /// Accept insecure certificates.
42    pub accept_insecure_certs: bool,
43    /// Page load strategy (normal, eager, none).
44    pub page_load_strategy: Option<String>,
45}
46
47impl Default for WebDriverConfig {
48    fn default() -> Self {
49        Self {
50            server_url: "http://localhost:4444".to_string(),
51            browser: WebDriverBrowser::Chrome,
52            headless: true,
53            browser_args: None,
54            timeout: Some(Duration::from_secs(60)),
55            proxy: None,
56            user_agent: None,
57            viewport_width: None,
58            viewport_height: None,
59            accept_insecure_certs: false,
60            page_load_strategy: None,
61        }
62    }
63}
64
65impl WebDriverConfig {
66    /// Create a new WebDriverConfig with default values.
67    pub fn new() -> Self {
68        Self::default()
69    }
70
71    /// Set the WebDriver server URL.
72    pub fn with_server_url(mut self, server_url: impl Into<String>) -> Self {
73        self.server_url = server_url.into();
74        self
75    }
76
77    /// Set the browser type.
78    pub fn with_browser(mut self, browser: WebDriverBrowser) -> Self {
79        self.browser = browser;
80        self
81    }
82
83    /// Set whether to run in headless mode.
84    pub fn with_headless(mut self, headless: bool) -> Self {
85        self.headless = headless;
86        self
87    }
88
89    /// Set custom browser arguments.
90    pub fn with_browser_args(mut self, args: Vec<String>) -> Self {
91        self.browser_args = Some(args);
92        self
93    }
94
95    /// Set the request timeout.
96    pub fn with_timeout(mut self, timeout: Duration) -> Self {
97        self.timeout = Some(timeout);
98        self
99    }
100
101    /// Set the proxy server URL.
102    pub fn with_proxy(mut self, proxy: impl Into<String>) -> Self {
103        self.proxy = Some(proxy.into());
104        self
105    }
106
107    /// Set the user agent string.
108    pub fn with_user_agent(mut self, user_agent: impl Into<String>) -> Self {
109        self.user_agent = Some(user_agent.into());
110        self
111    }
112
113    /// Set the viewport dimensions.
114    pub fn with_viewport(mut self, width: u32, height: u32) -> Self {
115        self.viewport_width = Some(width);
116        self.viewport_height = Some(height);
117        self
118    }
119
120    /// Set whether to accept insecure certificates.
121    pub fn with_accept_insecure_certs(mut self, accept: bool) -> Self {
122        self.accept_insecure_certs = accept;
123        self
124    }
125
126    /// Set the page load strategy.
127    pub fn with_page_load_strategy(mut self, strategy: impl Into<String>) -> Self {
128        self.page_load_strategy = Some(strategy.into());
129        self
130    }
131
132    /// Build the configuration.
133    pub fn build(self) -> Self {
134        self
135    }
136}
137
138/// WebDriver intercept configuration (limited compared to CDP).
139#[derive(Debug, Clone, Default, PartialEq)]
140#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
141pub struct WebDriverInterceptConfiguration {
142    /// Enable interception (limited in WebDriver).
143    pub enabled: bool,
144}
145
146impl WebDriverInterceptConfiguration {
147    /// Create a new intercept configuration.
148    pub fn new(enabled: bool) -> Self {
149        Self { enabled }
150    }
151}