-
Notifications
You must be signed in to change notification settings - Fork 125
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
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files
|
There was a problem hiding this 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> { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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"] } |
There was a problem hiding this comment.
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 ..
?
There was a problem hiding this comment.
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>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about taking
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?
There was a problem hiding this comment.
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
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(()) | ||
} |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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 :) |
No worries! Wishing you a great holiday. We’ll pick this up when you're back. 😊 Safe travels! |
Closes #225
The change encapsulates
Wcmd
within a private wrapper.