Skip to content

Commit 50f9252

Browse files
authored
[rust] Update PATH processing mechanism by Selenium Manager (#12291)
* [rust] Update PATH processing mechanism by Selenium Manager * [rust] Enhance warning message when incompatible driver in PATH is found
1 parent b94159e commit 50f9252

File tree

3 files changed

+97
-39
lines changed

3 files changed

+97
-39
lines changed

rust/src/lib.rs

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -317,27 +317,60 @@ pub trait SeleniumManager {
317317
}
318318

319319
fn resolve_driver(&mut self) -> Result<PathBuf, Box<dyn Error>> {
320-
if self.get_driver_version().is_empty() {
321-
let driver_version = self.discover_driver_version()?;
322-
self.set_driver_version(driver_version);
323-
}
320+
let mut driver_in_path = None;
321+
let mut driver_in_path_version = None;
324322

323+
// Try to find driver in PATH
325324
if !self.is_safari() && !self.is_grid() {
326-
if let (Some(version), Some(path)) = self.find_driver_in_path() {
327-
if version == self.get_driver_version() {
328-
self.get_logger().debug(format!(
329-
"Found {} {version} in PATH: {path}",
330-
self.get_driver_name()
331-
));
332-
return Ok(PathBuf::from(path));
333-
} else {
334-
self.get_logger().warn(format!(
335-
"Incompatible release of {} (version {version}) detected in PATH: {path}",
336-
self.get_driver_name()
337-
));
325+
(driver_in_path_version, driver_in_path) = self.find_driver_in_path();
326+
if let (Some(version), Some(path)) = (&driver_in_path_version, &driver_in_path) {
327+
self.get_logger().debug(format!(
328+
"Found {} {} in PATH: {}",
329+
self.get_driver_name(),
330+
version,
331+
path
332+
));
333+
}
334+
}
335+
336+
// Discover proper driver version
337+
if self.get_driver_version().is_empty() {
338+
match self.discover_driver_version() {
339+
Ok(driver_version) => {
340+
self.set_driver_version(driver_version);
341+
}
342+
Err(err) => {
343+
if driver_in_path_version.is_some() {
344+
self.get_logger().warn(format!(
345+
"Exception trying to discover {} version: {}",
346+
self.get_driver_name(),
347+
err
348+
));
349+
} else {
350+
return Err(err.into());
351+
}
338352
}
339353
}
340354
}
355+
356+
// If driver is in path, always use it
357+
if let (Some(version), Some(path)) = (&driver_in_path_version, &driver_in_path) {
358+
// If proper driver version is not the same as the driver in path, display warning
359+
if !self.get_driver_version().is_empty() && !version.eq(self.get_driver_version()) {
360+
self.get_logger().warn(format!(
361+
"The {} version ({}) detected in PATH at {} might not be compatible with the detected {} version ({}); it is recommended to delete the driver and retry",
362+
self.get_driver_name(),
363+
version,
364+
path,
365+
self.get_browser_name(),
366+
self.get_browser_version()
367+
));
368+
}
369+
self.set_driver_version(version.to_string());
370+
return Ok(PathBuf::from(path));
371+
}
372+
373+
// If driver was not in the PATH, try to find it in the cache
341374
let driver_path = self.get_driver_path_in_cache();
342375
if driver_path.exists() {
343376
if !self.is_safari() {
@@ -348,6 +381,7 @@ pub trait SeleniumManager {
348381
));
349382
}
350383
} else {
384+
// If driver is not in the cache, download it
351385
self.download_driver()?;
352386
}
353387
Ok(driver_path)

rust/tests/cli_tests.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
// under the License.
1717

1818
use assert_cmd::Command;
19-
2019
use rstest::rstest;
2120
use std::str;
2221

@@ -87,17 +86,30 @@ fn error_test(
8786
);
8887

8988
let mut cmd = Command::new(env!("CARGO_BIN_EXE_selenium-manager"));
90-
cmd.args([
91-
"--browser",
92-
&browser,
93-
"--browser-version",
94-
&browser_version,
95-
"--driver-version",
96-
&driver_version,
97-
])
98-
.assert()
99-
.failure()
100-
.code(error_code);
89+
let assert_result = cmd
90+
.args([
91+
"--debug",
92+
"--browser",
93+
&browser,
94+
"--browser-version",
95+
&browser_version,
96+
"--driver-version",
97+
&driver_version,
98+
])
99+
.assert()
100+
.try_success();
101+
102+
if assert_result.is_ok() {
103+
let stdout = &cmd.unwrap().stdout;
104+
let output = str::from_utf8(stdout).unwrap();
105+
assert!(output.contains("in PATH"));
106+
} else {
107+
assert!(assert_result
108+
.err()
109+
.unwrap()
110+
.to_string()
111+
.contains(&error_code.to_string()));
112+
}
101113
}
102114

103115
#[rstest]

rust/tests/proxy_tests.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,34 @@
1616
// under the License.
1717

1818
use assert_cmd::Command;
19-
2019
use exitcode::DATAERR;
20+
use std::str;
2121

2222
#[tokio::test]
2323
async fn wrong_proxy_test() {
2424
let mut cmd = Command::new(env!("CARGO_BIN_EXE_selenium-manager"));
25-
cmd.args([
26-
"--browser",
27-
"chrome",
28-
"--proxy",
29-
"http://localhost:12345",
30-
"--clear-cache",
31-
])
32-
.assert()
33-
.failure()
34-
.code(DATAERR);
25+
let assert_result = cmd
26+
.args([
27+
"--debug",
28+
"--browser",
29+
"chrome",
30+
"--proxy",
31+
"http://localhost:12345",
32+
"--clear-cache",
33+
])
34+
.assert()
35+
.try_success();
36+
if assert_result.is_ok() {
37+
let stdout = &cmd.unwrap().stdout;
38+
let output = str::from_utf8(stdout).unwrap();
39+
assert!(output.contains("in PATH"));
40+
} else {
41+
assert!(assert_result
42+
.err()
43+
.unwrap()
44+
.to_string()
45+
.contains(&DATAERR.to_string()));
46+
}
3547
}
3648

3749
#[test]

0 commit comments

Comments
 (0)