Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Encapsulate Wcmd within a private wrapper #275

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

surajk-m
Copy link
Contributor

Closes #225

The change encapsulates Wcmd within a private wrapper.

Copy link

codecov bot commented Sep 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 62.33%. Comparing base (29bbfa7) to head (0dd402d).
Report is 1 commits behind head on main.

Additional details and impacted files
Files with missing lines Coverage Δ
src/session.rs 73.53% <100.00%> (-0.05%) ⬇️

... and 3 files with indirect coverage changes

Copy link
Owner

@jonhoo jonhoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking this on!

src/session.rs Outdated
cmd: impl WebDriverCompatibleCommand + Send + 'static,
) -> Result<Json, error::CmdError> {
self.issue(Cmd::WebDriver(Box::new(cmd))).await
pub async fn issue_cmd(&self, cmd: Wcmd) -> Result<Json, error::CmdError> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this specifically needs to not change, otherwise we're putting Wcmd back into the public API here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for the oversight. I'll revise it to use the WebDriverCompatibleCommand trait.

Cargo.toml Outdated
@@ -47,6 +48,7 @@ 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"] }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look right — how can fantoccini depend on itself with a path of ..?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback. I’ve removed the path reference, it was a misconfiguration introduced during testing test_helpers feature.

src/session.rs Outdated
@@ -334,9 +350,9 @@ impl Client {
/// Issue the specified [`WebDriverCompatibleCommand`] to the WebDriver instance.
pub async fn issue_cmd(
&self,
cmd: impl WebDriverCompatibleCommand + Send + 'static,
cmd: Box<dyn WebDriverCompatibleCommand + Send + 'static>,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about taking

Suggested change
cmd: Box<dyn WebDriverCompatibleCommand + Send + 'static>,
cmd: impl Into<Box<impl WebDriverCompatibleCommand + Send + 'static>>,

and then below doing:

Cmd::WebDriver(Box::from(cmd) as Box<_>)

so that callers can also provide a non-boxed type for convenience?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I’ve updated issue_cmd to use impl Into<Box<impl WebDriverCompatibleCommand + Send + 'static>>

tests/local.rs Outdated
Comment on lines 423 to 435
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(test_wrap_command(WebDriverCommand::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(test_wrap_command(WebDriverCommand::GetTitle))
.await?;
assert_eq!(title.as_str(), Some("Sample Page"));
Ok(())
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what we're really looking to demonstrate here is that users can provide their own custom commands via fantoccini. That command doesn't even have to come from the webdriver crate. So how about we do this:

struct GetTitle;
impl WebDriverCompatibleCommand for GetTitle {
  // ...
}

and then check that it's possible to pass GetTitle and Box<GetTitle> to issue_cmd? Then you can get rid of test_wrap_command entirely (and the test helper feature).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this change removes the need for test_wrap_command and demonstrates issue_cmd can accept both boxed and non-boxed types.

- 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`
tests/local.rs Outdated
}

// Implement `Into<Box<dyn WebDriverCompatibleCommand + Send>>` for `GetTitle`
impl Into<Box<dyn WebDriverCompatibleCommand + Send>> for GetTitle {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if you use exactly the version of issur_cmd I proposed, this impl becomes unnecessary

Copy link
Contributor Author

@surajk-m surajk-m Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to use cmd: impl Into<Box<impl WebDriverCompatibleCommand + Send + 'static>> as the parameter type, but I'm getting error[E0666]

error[E0666]: nested `impl Trait` is not allowed
   --> src/session.rs:341:28
    |
341 |         cmd: impl Into<Box<impl WebDriverCompatibleCommand + Send + 'static>>,
    |              --------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--
    |              |             |
    |              |             nested `impl Trait` here
    |              outer `impl Trait`

To avoid using nested impl Trait, we should flatten the types.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that just means you'll need to use "proper" generics instead:

<C, CC> where C: Into<Box<CC>>, CC: WebDriverCompatibleCommand + Send + 'static

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, got it. Using <C, CC> with C: Into<Box<CC>> and CC: WebDriverCompatibleCommand + Send + 'static works better. The only overhead is the conversion via .into(), which is a bit more than a direct Box::new call if the type isn’t already boxed. If we only prefer non-boxed type as input, should we manually box cmd with Box::new(cmd) as Box<_> as you suggested?

tests/local.rs Outdated
(http::Method::GET, None)
}

fn is_new_session(&self) -> bool {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add default implementations for the two is_ methods directly on the trait?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I'll make the update in the next commit.

@jonhoo
Copy link
Owner

jonhoo commented Oct 28, 2024

Just a heads up that I'll be on holiday for the next three weeks, so won't have a chance to land this until I get back :)

@surajk-m
Copy link
Contributor Author

No worries! Wishing you a great holiday. We’ll pick this up when you're back. 😊 Safe travels!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

webdriver might be part of public API
2 participants