firefox-webdriver 0.2.1

High-performance Firefox WebDriver in Rust
Documentation
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
//! Error types for Firefox WebDriver.
//!
//! This module defines all error types used throughout the crate.
//! Error codes follow ARCHITECTURE.md Section 6.2.
//!
//! # Usage
//!
//! All fallible operations return [`Result<T>`] which uses [`Error`]:
//!
//! ```ignore
//! use firefox_webdriver::{Result, Error};
//!
//! async fn example(tab: &Tab) -> Result<()> {
//!     let element = tab.find_element("#submit").await?;
//!     element.click().await?;
//!     Ok(())
//! }
//! ```
//!
//! # Error Categories
//!
//! | Category | Variants |
//! |----------|----------|
//! | Configuration | [`Error::Config`], [`Error::Profile`] |
//! | Connection | [`Error::Connection`], [`Error::ConnectionTimeout`], [`Error::ConnectionClosed`] |
//! | Protocol | [`Error::UnknownCommand`], [`Error::InvalidArgument`], [`Error::Protocol`] |
//! | Element | [`Error::ElementNotFound`], [`Error::StaleElement`] |
//! | Navigation | [`Error::FrameNotFound`], [`Error::TabNotFound`] |
//! | Execution | [`Error::ScriptError`], [`Error::Timeout`], [`Error::RequestTimeout`] |
//! | External | [`Error::Io`], [`Error::Json`], [`Error::WebSocket`] |

// ============================================================================
// Imports
// ============================================================================

use std::io::Error as IoError;
use std::path::PathBuf;
use std::result::Result as StdResult;

use thiserror::Error;
use tokio::sync::oneshot::error::RecvError;
use tokio_tungstenite::tungstenite::Error as WsError;

use crate::identifiers::{ElementId, FrameId, RequestId, SessionId, TabId};

// ============================================================================
// Result Alias
// ============================================================================

/// Result type alias using crate [`enum@Error`].
///
/// All fallible operations in this crate return this type.
pub type Result<T> = StdResult<T, Error>;

// ============================================================================
// Error Enum
// ============================================================================

/// Main error type for the crate.
///
/// Each variant includes relevant context for debugging.
/// Error codes match ARCHITECTURE.md Section 6.2.
#[derive(Error, Debug)]
pub enum Error {
    // ========================================================================
    // Configuration Errors
    // ========================================================================
    /// Configuration error.
    ///
    /// Returned when driver configuration is invalid.
    #[error("Configuration error: {message}")]
    Config {
        /// Description of the configuration error.
        message: String,
    },

    /// Profile error.
    ///
    /// Returned when Firefox profile creation or setup fails.
    #[error("Profile error: {message}")]
    Profile {
        /// Description of the profile error.
        message: String,
    },

    /// Firefox binary not found at path.
    ///
    /// Returned when the specified Firefox binary does not exist.
    #[error("Firefox not found at: {path}")]
    FirefoxNotFound {
        /// Path where Firefox was expected.
        path: PathBuf,
    },

    /// Failed to launch Firefox process.
    ///
    /// Returned when Firefox process fails to start.
    #[error("Failed to launch Firefox: {message}")]
    ProcessLaunchFailed {
        /// Description of the launch failure.
        message: String,
    },

    // ========================================================================
    // Connection Errors
    // ========================================================================
    /// WebSocket connection failed.
    ///
    /// Returned when WebSocket connection cannot be established.
    #[error("Connection failed: {message}")]
    Connection {
        /// Description of the connection error.
        message: String,
    },

    /// Connection timeout waiting for extension.
    ///
    /// Returned when extension does not connect within timeout period.
    #[error("Connection timeout after {timeout_ms}ms")]
    ConnectionTimeout {
        /// Milliseconds waited before timeout.
        timeout_ms: u64,
    },

    /// WebSocket connection closed unexpectedly.
    ///
    /// Returned when connection is lost during operation.
    #[error("Connection closed")]
    ConnectionClosed,

    // ========================================================================
    // Protocol Errors
    // ========================================================================
    /// Unknown command method.
    ///
    /// Returned when extension receives unrecognized command.
    #[error("Unknown command: {command}")]
    UnknownCommand {
        /// The unrecognized command method.
        command: String,
    },

    /// Invalid argument in command params.
    ///
    /// Returned when command parameters are invalid.
    #[error("Invalid argument: {message}")]
    InvalidArgument {
        /// Description of the invalid argument.
        message: String,
    },

    /// Protocol violation or unexpected response.
    ///
    /// Returned when protocol message format is invalid.
    #[error("Protocol error: {message}")]
    Protocol {
        /// Description of the protocol violation.
        message: String,
    },

    // ========================================================================
    // Element Errors
    // ========================================================================
    /// Element not found by selector.
    ///
    /// Returned when CSS selector matches no elements.
    #[error("Element not found: selector={selector}, tab={tab_id}, frame={frame_id}")]
    ElementNotFound {
        /// CSS selector used.
        selector: String,
        /// Tab where search was performed.
        tab_id: TabId,
        /// Frame where search was performed.
        frame_id: FrameId,
    },

    /// Element is stale (no longer in DOM).
    ///
    /// Returned when element reference is no longer valid.
    #[error("Stale element: {element_id}")]
    StaleElement {
        /// The stale element's ID.
        element_id: ElementId,
    },

    // ========================================================================
    // Navigation Errors
    // ========================================================================
    /// Frame not found.
    ///
    /// Returned when frame ID does not exist.
    #[error("Frame not found: {frame_id}")]
    FrameNotFound {
        /// The missing frame ID.
        frame_id: FrameId,
    },

    /// Tab not found.
    ///
    /// Returned when tab ID does not exist.
    #[error("Tab not found: {tab_id}")]
    TabNotFound {
        /// The missing tab ID.
        tab_id: TabId,
    },

    // ========================================================================
    // Execution Errors
    // ========================================================================
    /// JavaScript execution error.
    ///
    /// Returned when script execution fails in browser.
    #[error("Script error: {message}")]
    ScriptError {
        /// Error message from script execution.
        message: String,
    },

    /// Operation timeout.
    ///
    /// Returned when operation exceeds timeout duration.
    #[error("Timeout after {timeout_ms}ms: {operation}")]
    Timeout {
        /// Description of the operation that timed out.
        operation: String,
        /// Milliseconds waited before timeout.
        timeout_ms: u64,
    },

    /// Command request timeout.
    ///
    /// Returned when WebSocket request times out.
    #[error("Request {request_id} timed out after {timeout_ms}ms")]
    RequestTimeout {
        /// The request ID that timed out.
        request_id: RequestId,
        /// Milliseconds waited before timeout.
        timeout_ms: u64,
    },

    // ========================================================================
    // Network Errors
    // ========================================================================
    /// Network intercept not found.
    ///
    /// Returned when intercept ID does not exist.
    #[error("Intercept not found: {intercept_id}")]
    InterceptNotFound {
        /// The missing intercept ID.
        intercept_id: String,
    },

    /// Session not found in connection pool.
    ///
    /// Returned when session ID does not exist in the pool.
    #[error("Session not found: {session_id}")]
    SessionNotFound {
        /// The missing session ID.
        session_id: SessionId,
    },

    // ========================================================================
    // External Errors
    // ========================================================================
    /// IO error.
    #[error("IO error: {0}")]
    Io(#[from] IoError),

    /// JSON serialization error.
    #[error("JSON error: {0}")]
    Json(#[from] serde_json::Error),

    /// WebSocket error.
    #[error("WebSocket error: {0}")]
    WebSocket(#[from] WsError),

    /// Channel receive error.
    #[error("Channel closed")]
    ChannelClosed(#[from] RecvError),
}

// ============================================================================
// Error Constructors
// ============================================================================

impl Error {
    /// Creates a configuration error.
    #[inline]
    pub fn config(message: impl Into<String>) -> Self {
        Self::Config {
            message: message.into(),
        }
    }

    /// Creates a profile error.
    #[inline]
    pub fn profile(message: impl Into<String>) -> Self {
        Self::Profile {
            message: message.into(),
        }
    }

    /// Creates a Firefox not found error.
    #[inline]
    pub fn firefox_not_found(path: impl Into<PathBuf>) -> Self {
        Self::FirefoxNotFound { path: path.into() }
    }

    /// Creates a process launch failed error.
    #[inline]
    pub fn process_launch_failed(err: IoError) -> Self {
        Self::ProcessLaunchFailed {
            message: err.to_string(),
        }
    }

    /// Creates a connection error.
    #[inline]
    pub fn connection(message: impl Into<String>) -> Self {
        Self::Connection {
            message: message.into(),
        }
    }

    /// Creates a connection timeout error.
    #[inline]
    pub fn connection_timeout(timeout_ms: u64) -> Self {
        Self::ConnectionTimeout { timeout_ms }
    }

    /// Creates a protocol error.
    #[inline]
    pub fn protocol(message: impl Into<String>) -> Self {
        Self::Protocol {
            message: message.into(),
        }
    }

    /// Creates an invalid argument error.
    #[inline]
    pub fn invalid_argument(message: impl Into<String>) -> Self {
        Self::InvalidArgument {
            message: message.into(),
        }
    }

    /// Creates an element not found error.
    #[inline]
    pub fn element_not_found(
        selector: impl Into<String>,
        tab_id: TabId,
        frame_id: FrameId,
    ) -> Self {
        Self::ElementNotFound {
            selector: selector.into(),
            tab_id,
            frame_id,
        }
    }

    /// Creates a stale element error.
    #[inline]
    pub fn stale_element(element_id: ElementId) -> Self {
        Self::StaleElement { element_id }
    }

    /// Creates a frame not found error.
    #[inline]
    pub fn frame_not_found(frame_id: FrameId) -> Self {
        Self::FrameNotFound { frame_id }
    }

    /// Creates a tab not found error.
    #[inline]
    pub fn tab_not_found(tab_id: TabId) -> Self {
        Self::TabNotFound { tab_id }
    }

    /// Creates a script error.
    #[inline]
    pub fn script_error(message: impl Into<String>) -> Self {
        Self::ScriptError {
            message: message.into(),
        }
    }

    /// Creates a timeout error.
    #[inline]
    pub fn timeout(operation: impl Into<String>, timeout_ms: u64) -> Self {
        Self::Timeout {
            operation: operation.into(),
            timeout_ms,
        }
    }

    /// Creates a request timeout error.
    #[inline]
    pub fn request_timeout(request_id: RequestId, timeout_ms: u64) -> Self {
        Self::RequestTimeout {
            request_id,
            timeout_ms,
        }
    }

    /// Creates an intercept not found error.
    #[inline]
    pub fn intercept_not_found(intercept_id: impl Into<String>) -> Self {
        Self::InterceptNotFound {
            intercept_id: intercept_id.into(),
        }
    }

    /// Creates a session not found error.
    #[inline]
    pub fn session_not_found(session_id: SessionId) -> Self {
        Self::SessionNotFound { session_id }
    }
}

// ============================================================================
// Error Predicates
// ============================================================================

impl Error {
    /// Returns `true` if this is a timeout error.
    #[inline]
    #[must_use]
    pub fn is_timeout(&self) -> bool {
        matches!(
            self,
            Self::ConnectionTimeout { .. } | Self::Timeout { .. } | Self::RequestTimeout { .. }
        )
    }

    /// Returns `true` if this is an element error.
    #[inline]
    #[must_use]
    pub fn is_element_error(&self) -> bool {
        matches!(
            self,
            Self::ElementNotFound { .. } | Self::StaleElement { .. }
        )
    }

    /// Returns `true` if this is a connection error.
    #[inline]
    #[must_use]
    pub fn is_connection_error(&self) -> bool {
        matches!(
            self,
            Self::Connection { .. }
                | Self::ConnectionTimeout { .. }
                | Self::ConnectionClosed
                | Self::WebSocket(_)
        )
    }

    /// Returns `true` if this error is recoverable.
    ///
    /// Recoverable errors may succeed on retry.
    #[inline]
    #[must_use]
    pub fn is_recoverable(&self) -> bool {
        matches!(
            self,
            Self::ConnectionTimeout { .. }
                | Self::Timeout { .. }
                | Self::RequestTimeout { .. }
                | Self::StaleElement { .. }
        )
    }
}

// ============================================================================
// Tests
// ============================================================================

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

    use std::io::ErrorKind;

    #[test]
    fn test_error_display() {
        let err = Error::connection("failed to connect");
        assert_eq!(err.to_string(), "Connection failed: failed to connect");
    }

    #[test]
    fn test_config_error() {
        let err = Error::config("missing binary path");
        assert_eq!(err.to_string(), "Configuration error: missing binary path");
    }

    #[test]
    fn test_is_timeout() {
        let timeout_err = Error::ConnectionTimeout { timeout_ms: 5000 };
        let other_err = Error::connection("test");

        assert!(timeout_err.is_timeout());
        assert!(!other_err.is_timeout());
    }

    #[test]
    fn test_is_connection_error() {
        let conn_err = Error::connection("test");
        let timeout_err = Error::ConnectionTimeout { timeout_ms: 1000 };
        let closed_err = Error::ConnectionClosed;
        let other_err = Error::config("test");

        assert!(conn_err.is_connection_error());
        assert!(timeout_err.is_connection_error());
        assert!(closed_err.is_connection_error());
        assert!(!other_err.is_connection_error());
    }

    #[test]
    fn test_is_recoverable() {
        let timeout_err = Error::Timeout {
            operation: "test".into(),
            timeout_ms: 1000,
        };
        let config_err = Error::config("test");

        assert!(timeout_err.is_recoverable());
        assert!(!config_err.is_recoverable());
    }

    #[test]
    fn test_from_io_error() {
        let io_err = IoError::new(ErrorKind::NotFound, "file not found");
        let err: Error = io_err.into();
        assert!(matches!(err, Error::Io(_)));
    }

    #[test]
    fn test_from_json_error() {
        let json_err = serde_json::from_str::<String>("invalid").unwrap_err();
        let err: Error = json_err.into();
        assert!(matches!(err, Error::Json(_)));
    }
}