diff --git a/src/session.rs b/src/session.rs index 4af1b7e..cbb8faa 100644 --- a/src/session.rs +++ b/src/session.rs @@ -26,6 +26,10 @@ type Ack = oneshot::Sender>; type Wcmd = WebDriverCommand; +// Wrapper to hide Wcmd from the public API. +#[derive(Debug)] +struct WcmdWrapper(Wcmd); + #[allow(clippy::large_enum_variant)] #[derive(Debug)] pub(crate) enum Cmd { @@ -43,7 +47,7 @@ pub(crate) enum Cmd { WebDriver(Box), } -impl WebDriverCompatibleCommand for Wcmd { +impl WebDriverCompatibleCommand for WcmdWrapper { /// Helper for determining what URL endpoint to use for various requests. /// /// This mapping is essentially that of . @@ -52,16 +56,16 @@ impl WebDriverCompatibleCommand for Wcmd { base_url: &url::Url, session_id: Option<&str>, ) -> Result { - if let WebDriverCommand::NewSession(..) = self { + if let WebDriverCommand::NewSession(..) = self.0 { return base_url.join("session"); } - if let WebDriverCommand::Status = self { + if let WebDriverCommand::Status = self.0 { return base_url.join("status"); } let base = { base_url.join(&format!("session/{}/", session_id.as_ref().unwrap()))? }; - match self { + match self.0 { WebDriverCommand::NewSession(..) => unreachable!(), WebDriverCommand::DeleteSession => unreachable!(), WebDriverCommand::Get(..) | WebDriverCommand::GetCurrentUrl => base.join("url"), @@ -159,7 +163,7 @@ impl WebDriverCompatibleCommand for Wcmd { let mut body = None; // but some have a request body - match self { + match self.0 { WebDriverCommand::NewSession(command::NewSessionParameters::Spec(ref conf)) => { // TODO: awful hacks let mut also = String::new(); @@ -280,12 +284,12 @@ impl WebDriverCompatibleCommand for Wcmd { } fn is_new_session(&self) -> bool { - matches!(self, WebDriverCommand::NewSession(..)) + matches!(self.0, WebDriverCommand::NewSession(..)) } fn is_legacy(&self) -> bool { matches!( - self, + self.0, WebDriverCommand::NewSession(webdriver::command::NewSessionParameters::Legacy(..)), ) } @@ -293,7 +297,7 @@ impl WebDriverCompatibleCommand for Wcmd { impl From for Cmd { fn from(o: Wcmd) -> Self { - Cmd::WebDriver(Box::new(o)) + Cmd::WebDriver(Box::new(WcmdWrapper(o))) } } @@ -332,11 +336,12 @@ impl Client { } /// Issue the specified [`WebDriverCompatibleCommand`] to the WebDriver instance. - pub async fn issue_cmd( - &self, - cmd: impl WebDriverCompatibleCommand + Send + 'static, - ) -> Result { - self.issue(Cmd::WebDriver(Box::new(cmd))).await + pub async fn issue_cmd(&self, cmd: C) -> Result + where + C: Into>, + CC: WebDriverCompatibleCommand + Send + 'static, + { + self.issue(Cmd::WebDriver(cmd.into())).await } pub(crate) fn is_legacy(&self) -> bool { diff --git a/tests/local.rs b/tests/local.rs index 3611cb7..1fe26af 100644 --- a/tests/local.rs +++ b/tests/local.rs @@ -1,16 +1,34 @@ //! Tests that don't make use of external websites. use crate::common::{other_page_url, sample_page_url}; -use fantoccini::wd::TimeoutConfiguration; +use fantoccini::wd::{TimeoutConfiguration, WebDriverCompatibleCommand}; use fantoccini::{error, Client, Locator}; use http_body_util::BodyExt; use hyper::Method; use serial_test::serial; use std::time::Duration; use url::Url; -use webdriver::command::WebDriverCommand; mod common; +#[derive(Debug)] +struct GetTitle; + +// Implement `WebDriverCompatibleCommand` for `GetTitle` +impl WebDriverCompatibleCommand for GetTitle { + fn endpoint( + &self, + base_url: &url::Url, + session_id: Option<&str>, + ) -> Result { + let base = base_url.join(&format!("session/{}/", session_id.unwrap()))?; + base.join("title") + } + + fn method_and_body(&self, _: &url::Url) -> (http::Method, Option) { + (http::Method::GET, None) + } +} + async fn goto(c: Client, port: u16) -> Result<(), error::CmdError> { let url = sample_page_url(port); c.goto(&url).await?; @@ -423,9 +441,9 @@ async fn timeouts(c: Client, _: u16) -> Result<(), error::CmdError> { async fn dynamic_commands(c: Client, port: u16) -> Result<(), error::CmdError> { let sample_url = sample_page_url(port); c.goto(&sample_url).await?; - let title = c.issue_cmd(WebDriverCommand::GetTitle).await?; + let title = c.issue_cmd(GetTitle).await?; assert_eq!(title.as_str(), Some("Sample Page")); - let title = c.issue_cmd(Box::new(WebDriverCommand::GetTitle)).await?; + let title = c.issue_cmd(GetTitle).await?; assert_eq!(title.as_str(), Some("Sample Page")); Ok(()) }