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
…14381)

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

* [rust] Remove test data with incorrect browser path in macOS

* Revert "[rust] Remove test data with incorrect browser path in macOS"

This reverts commit 79c22d6.

* [rust] Escape browser path before checking existence

* Revert "[rust] Escape browser path before checking existence"

This reverts commit b876e22.

* [rust] Remove test data with incorrect browser path in macOS

* [rust] Force window-sys crate version in Windows

* Revert "[rust] Force window-sys crate version in Windows"

This reverts commit a968a40.
  • Loading branch information
bonigarcia committed Sep 17, 2024
1 parent 6459008 commit 0d42674
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 24 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 @@ -99,6 +100,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 @@ -1083,6 +1083,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 @@ -1524,6 +1530,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 @@ -253,25 +253,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();
}

0 comments on commit 0d42674

Please sign in to comment.