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

[rust] Selenium Manager errors when browser-path is wrong (#13352) #14381

Merged
merged 8 commits into from
Sep 17, 2024
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
19 changes: 14 additions & 5 deletions rust/tests/browser_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,6 @@ fn invalid_geckodriver_version_test() {
r"C:\Program Files\Google\Chrome\Application\chrome.exe"
)]
#[case("linux", "chrome", "/usr/bin/google-chrome")]
#[case(
"macos",
"chrome",
r"/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
)]
#[case(
"macos",
"chrome",
Expand All @@ -151,3 +146,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();
}
Loading