Skip to content

Commit

Permalink
Merge branch 'trunk' into close-bidi-socket-connection
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani authored Sep 17, 2024
2 parents dfa8f8f + 0d42674 commit 878dbab
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 25 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
4 changes: 3 additions & 1 deletion rust/src/firefox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,11 @@ impl SeleniumManager for FirefoxManager {
_ => {
self.assert_online_or_err(OFFLINE_REQUEST_ERR_MSG)?;

let driver_version_url =
self.get_driver_mirror_versions_url_or_default(DRIVER_VERSIONS_URL);
let driver_version = match parse_json_from_url::<GeckodriverReleases>(
self.get_http_client(),
DRIVER_VERSIONS_URL,
&driver_version_url,
) {
Ok(driver_releases) => {
let major_browser_version_int =
Expand Down
34 changes: 34 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 @@ -1288,6 +1294,26 @@ pub trait SeleniumManager {
}
}

fn get_driver_mirror_versions_url_or_default<'a>(&'a self, default_url: &'a str) -> String {
let driver_mirror_url = self.get_driver_mirror_url();
if !driver_mirror_url.is_empty() {
let driver_versions_path = default_url.rfind('/').map(|i| &default_url[i + 1..]);
if let Some(path) = driver_versions_path {
let driver_mirror_versions_url = if driver_mirror_url.ends_with('/') {
format!("{}{}", driver_mirror_url, path)
} else {
format!("{}/{}", driver_mirror_url, path)
};
self.get_logger().debug(format!(
"Using mirror URL to discover driver versions: {}",
driver_mirror_versions_url
));
return driver_mirror_versions_url;
}
}
default_url.to_string()
}

fn get_driver_mirror_url_or_default<'a>(&'a self, default_url: &'a str) -> String {
self.get_url_or_default(self.get_driver_mirror_url(), default_url)
}
Expand Down Expand Up @@ -1504,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 878dbab

Please sign in to comment.