Skip to content

Commit 01ce896

Browse files
committed
[rust] Selenium Manager errors when browser-path is wrong (#13352)
1 parent 9ddaa79 commit 01ce896

File tree

4 files changed

+52
-19
lines changed

4 files changed

+52
-19
lines changed

rust/src/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub const CACHE_PATH_KEY: &str = "cache-path";
4242

4343
pub struct ManagerConfig {
4444
pub cache_path: String,
45+
pub fallback_driver_from_cache: bool,
4546
pub browser_version: String,
4647
pub driver_version: String,
4748
pub browser_path: String,
@@ -97,6 +98,7 @@ impl ManagerConfig {
9798

9899
ManagerConfig {
99100
cache_path,
101+
fallback_driver_from_cache: true,
100102
browser_version: StringKey(vec!["browser-version", &browser_version_label], "")
101103
.get_value(),
102104
driver_version: StringKey(vec!["driver-version", &driver_version_label], "")

rust/src/lib.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,12 @@ pub trait SeleniumManager {
10651065
if let Some(path) = self.detect_browser_path() {
10661066
browser_path = path_to_string(&path);
10671067
}
1068+
} else if !Path::new(&browser_path).exists() {
1069+
self.set_fallback_driver_from_cache(false);
1070+
return Err(anyhow!(format_one_arg(
1071+
"Browser path does not exist: {}",
1072+
&browser_path,
1073+
)));
10681074
}
10691075
let escaped_browser_path = self.get_escaped_path(browser_path.to_string());
10701076

@@ -1466,6 +1472,14 @@ pub trait SeleniumManager {
14661472
self.get_config_mut().avoid_stats = true;
14671473
}
14681474
}
1475+
1476+
fn is_fallback_driver_from_cache(&self) -> bool {
1477+
self.get_config().fallback_driver_from_cache
1478+
}
1479+
1480+
fn set_fallback_driver_from_cache(&mut self, fallback_driver_from_cache: bool) {
1481+
self.get_config_mut().fallback_driver_from_cache = fallback_driver_from_cache;
1482+
}
14691483
}
14701484

14711485
// ----------------------------------------------------------

rust/src/main.rs

+22-19
Original file line numberDiff line numberDiff line change
@@ -243,25 +243,28 @@ fn main() {
243243
})
244244
.unwrap_or_else(|err| {
245245
let log = selenium_manager.get_logger();
246-
if let Some(best_driver_from_cache) =
247-
selenium_manager.find_best_driver_from_cache().unwrap()
248-
{
249-
log.debug_or_warn(
250-
format!(
251-
"There was an error managing {} ({}); using driver found in the cache",
252-
selenium_manager.get_driver_name(),
253-
err
254-
),
255-
selenium_manager.is_offline(),
256-
);
257-
log_driver_and_browser_path(
258-
log,
259-
&best_driver_from_cache,
260-
&selenium_manager.get_browser_path_or_latest_from_cache(),
261-
selenium_manager.get_receiver(),
262-
);
263-
flush_and_exit(OK, log, Some(err));
264-
} else if selenium_manager.is_offline() {
246+
if selenium_manager.is_fallback_driver_from_cache() {
247+
if let Some(best_driver_from_cache) =
248+
selenium_manager.find_best_driver_from_cache().unwrap()
249+
{
250+
log.debug_or_warn(
251+
format!(
252+
"There was an error managing {} ({}); using driver found in the cache",
253+
selenium_manager.get_driver_name(),
254+
err
255+
),
256+
selenium_manager.is_offline(),
257+
);
258+
log_driver_and_browser_path(
259+
log,
260+
&best_driver_from_cache,
261+
&selenium_manager.get_browser_path_or_latest_from_cache(),
262+
selenium_manager.get_receiver(),
263+
);
264+
flush_and_exit(OK, log, Some(err));
265+
}
266+
}
267+
if selenium_manager.is_offline() {
265268
log.warn(&err);
266269
flush_and_exit(OK, log, Some(err));
267270
} else {

rust/tests/browser_tests.rs

+14
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,17 @@ fn browser_path_test(#[case] os: String, #[case] browser: String, #[case] browse
151151
assert!(!stdout.contains("WARN"));
152152
}
153153
}
154+
155+
#[test]
156+
fn invalid_browser_path_test() {
157+
let mut cmd = get_selenium_manager();
158+
cmd.args([
159+
"--browser",
160+
"chrome",
161+
"--browser-path",
162+
"/bad/path/google-chrome-wrong",
163+
])
164+
.assert()
165+
.code(DATAERR)
166+
.failure();
167+
}

0 commit comments

Comments
 (0)