Skip to content

Commit

Permalink
[rust] Selenium Manager errors when browser-path is wrong (#13352)
Browse files Browse the repository at this point in the history
  • Loading branch information
bonigarcia committed Aug 12, 2024
1 parent 78fc8d9 commit 4ef6a30
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 19 deletions.
2 changes: 2 additions & 0 deletions rust/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub const CACHE_PATH_KEY: &str = "cache-path";

pub struct ManagerConfig {
pub cache_path: String,
pub fallback_driver_from_cache: bool,
pub browser_version: String,
pub driver_version: String,
pub browser_path: String,
Expand Down Expand Up @@ -97,6 +98,7 @@ impl ManagerConfig {

ManagerConfig {
cache_path,
fallback_driver_from_cache: true,
browser_version: StringKey(vec!["browser-version", &browser_version_label], "")
.get_value(),
driver_version: StringKey(vec!["driver-version", &driver_version_label], "")
Expand Down
14 changes: 14 additions & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,12 @@ pub trait SeleniumManager {
if let Some(path) = self.detect_browser_path() {
browser_path = path_to_string(&path);
}
} else if !Path::new(&browser_path).exists() {
self.set_fallback_driver_from_cache(false);
return Err(anyhow!(format_one_arg(
"Browser path does not exist: {}",
&browser_path,
)));
}
let escaped_browser_path = self.get_escaped_path(browser_path.to_string());

Expand Down Expand Up @@ -1466,6 +1472,14 @@ pub trait SeleniumManager {
self.get_config_mut().avoid_stats = true;
}
}

fn is_fallback_driver_from_cache(&self) -> bool {
self.get_config().fallback_driver_from_cache
}

fn set_fallback_driver_from_cache(&mut self, fallback_driver_from_cache: bool) {
self.get_config_mut().fallback_driver_from_cache = fallback_driver_from_cache;
}
}

// ----------------------------------------------------------
Expand Down
41 changes: 22 additions & 19 deletions rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,25 +243,28 @@ fn main() {
})
.unwrap_or_else(|err| {
let log = selenium_manager.get_logger();
if let Some(best_driver_from_cache) =
selenium_manager.find_best_driver_from_cache().unwrap()
{
log.debug_or_warn(
format!(
"There was an error managing {} ({}); using driver found in the cache",
selenium_manager.get_driver_name(),
err
),
selenium_manager.is_offline(),
);
log_driver_and_browser_path(
log,
&best_driver_from_cache,
&selenium_manager.get_browser_path_or_latest_from_cache(),
selenium_manager.get_receiver(),
);
flush_and_exit(OK, log, Some(err));
} else if selenium_manager.is_offline() {
if selenium_manager.is_fallback_driver_from_cache() {
if let Some(best_driver_from_cache) =
selenium_manager.find_best_driver_from_cache().unwrap()
{
log.debug_or_warn(
format!(
"There was an error managing {} ({}); using driver found in the cache",
selenium_manager.get_driver_name(),
err
),
selenium_manager.is_offline(),
);
log_driver_and_browser_path(
log,
&best_driver_from_cache,
&selenium_manager.get_browser_path_or_latest_from_cache(),
selenium_manager.get_receiver(),
);
flush_and_exit(OK, log, Some(err));
}
}
if selenium_manager.is_offline() {
log.warn(&err);
flush_and_exit(OK, log, Some(err));
} else {
Expand Down
14 changes: 14 additions & 0 deletions rust/tests/browser_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,17 @@ fn browser_path_test(#[case] os: String, #[case] browser: String, #[case] browse
assert!(!stdout.contains("WARN"));
}
}

#[test]
fn invalid_browser_path_test() {
let mut cmd = get_selenium_manager();
cmd.args([
"--browser",
"chrome",
"--browser-path",
"/bad/path/google-chrome-wrong",
])
.assert()
.code(DATAERR)
.failure();
}

0 comments on commit 4ef6a30

Please sign in to comment.