Skip to content

Commit

Permalink
Update issue_cmd for boxed and non-boxed command types
Browse files Browse the repository at this point in the history
- Remove `test_wrap_command` and `test_helpers` feature
- Refactor `issue_cmd` to accept impl `Into<Box<dyn WebDriverCompatibleCommand + Send + static>>`
- Introduce a `GetTitle` struct that implements `WebDriverCompatibleCommand`
  • Loading branch information
surajk-m committed Oct 28, 2024
1 parent f3d43da commit e26d58e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 29 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ license = "MIT OR Apache-2.0"
default = ["native-tls"]
native-tls = ["hyper-tls", "openssl"]
rustls-tls = ["hyper-rustls"]
test_helpers = []

[dependencies]
webdriver = { version = "0.50", default-features = false }
Expand All @@ -48,7 +47,6 @@ tokio = { version = "1", features = ["full"] }
hyper = { version = "1.1.0", features = ["server"] }
hyper-util = { version = "0.1.3", features = ["server", "http1"] }
serial_test = "3.0"
fantoccini = { path = "../fantoccini", features = ["test_helpers"] }

# for minimal-versions
[target.'cfg(any())'.dependencies]
Expand Down
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,3 @@ pub mod wait;
pub mod wd;
#[doc(inline)]
pub use wd::Locator;

#[cfg(any(test, feature = "test_helpers"))]
pub use crate::session::test_wrap_command;
16 changes: 2 additions & 14 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,6 @@ type Wcmd = WebDriverCommand<webdriver::command::VoidWebDriverExtensionCommand>;
#[derive(Debug)]
struct WcmdWrapper(Wcmd);

/// Wraps a WebDriverCommand inside a WcmdWrapper and returns it as a
/// Box<dyn WebDriverCompatibleCommand>.
///
/// This helper function is intended for use in tests where internal
/// WebDriver commands need to be wrapped in the private `WcmdWrapper`
#[cfg(any(test, feature = "test_helpers"))]
pub fn test_wrap_command(
cmd: WebDriverCommand<webdriver::command::VoidWebDriverExtensionCommand>,
) -> Box<dyn WebDriverCompatibleCommand + Send + 'static> {
Box::new(WcmdWrapper(cmd))
}

#[allow(clippy::large_enum_variant)]
#[derive(Debug)]
pub(crate) enum Cmd {
Expand Down Expand Up @@ -350,9 +338,9 @@ impl Client {
/// Issue the specified [`WebDriverCompatibleCommand`] to the WebDriver instance.
pub async fn issue_cmd(
&self,
cmd: Box<dyn WebDriverCompatibleCommand + Send + 'static>,
cmd: impl Into<Box<dyn WebDriverCompatibleCommand + Send + 'static>>,
) -> Result<Json, error::CmdError> {
self.issue(Cmd::WebDriver(cmd)).await
self.issue(Cmd::WebDriver(cmd.into())).await
}

pub(crate) fn is_legacy(&self) -> bool {
Expand Down
47 changes: 38 additions & 9 deletions tests/local.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,49 @@
//! Tests that don't make use of external websites.
use crate::common::{other_page_url, sample_page_url};
use fantoccini::wd::TimeoutConfiguration;
use fantoccini::{error, test_wrap_command, Client, Locator};
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<url::Url, url::ParseError> {
let base = base_url.join(&format!("session/{}/", session_id.unwrap()))?;
base.join("title")
}

fn method_and_body(&self, _: &url::Url) -> (http::Method, Option<String>) {
(http::Method::GET, None)
}

fn is_new_session(&self) -> bool {
false
}

fn is_legacy(&self) -> bool {
false
}
}

// Implement `Into<Box<dyn WebDriverCompatibleCommand + Send>>` for `GetTitle`
impl Into<Box<dyn WebDriverCompatibleCommand + Send>> for GetTitle {
fn into(self) -> Box<dyn WebDriverCompatibleCommand + Send> {
Box::new(self)
}
}

async fn goto(c: Client, port: u16) -> Result<(), error::CmdError> {
let url = sample_page_url(port);
c.goto(&url).await?;
Expand Down Expand Up @@ -423,13 +456,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(test_wrap_command(WebDriverCommand::GetTitle))
.await?;
let title = c.issue_cmd(GetTitle).await?;
assert_eq!(title.as_str(), Some("Sample Page"));
let title = c
.issue_cmd(test_wrap_command(WebDriverCommand::GetTitle))
.await?;
let title = c.issue_cmd(GetTitle).await?;
assert_eq!(title.as_str(), Some("Sample Page"));
Ok(())
}
Expand Down

0 comments on commit e26d58e

Please sign in to comment.