Skip to main content

chrome_for_testing_manager/
session.rs

1use thiserror::Error;
2
3/// A browser session. Used to control the browser.
4///
5/// When using `thirtyfour` (feature), this has a `Deref` impl to `thirtyfour::WebDriver`, so this
6/// session can be seen as the `driver`.
7#[derive(Debug)]
8pub struct Session {
9    #[cfg(feature = "thirtyfour")]
10    pub(crate) driver: thirtyfour::WebDriver,
11}
12
13#[derive(Debug, Error)]
14pub enum SessionError {
15    #[error("The user code panicked:\n{reason}")]
16    Panic { reason: String },
17
18    #[cfg(feature = "thirtyfour")]
19    #[error("thirtyfour WebDriverError")]
20    Thirtyfour {
21        #[from]
22        source: thirtyfour::error::WebDriverError,
23    },
24}
25
26impl Session {
27    /// Quit the browser session.
28    ///
29    /// # Errors
30    ///
31    /// Returns a [`SessionError`] if the underlying `WebDriver` session cannot be closed.
32    pub async fn quit(self) -> Result<(), SessionError> {
33        #[cfg(feature = "thirtyfour")]
34        {
35            self.driver.quit().await.map_err(Into::into)
36        }
37
38        #[cfg(not(feature = "thirtyfour"))]
39        {
40            Ok(())
41        }
42    }
43}
44
45#[cfg(feature = "thirtyfour")]
46impl std::ops::Deref for Session {
47    type Target = thirtyfour::WebDriver;
48
49    fn deref(&self) -> &Self::Target {
50        &self.driver
51    }
52}