From 23d2ca1264906523369854de4f62339da8441da4 Mon Sep 17 00:00:00 2001 From: Boni Garcia Date: Mon, 3 Jul 2023 12:59:04 +0200 Subject: [PATCH] [rust] Use escaped browser path (required by wmic commands) in Selenium Manager --- rust/README.md | 2 +- rust/src/chrome.rs | 3 ++- rust/src/edge.rs | 3 ++- rust/src/firefox.rs | 3 ++- rust/src/lib.rs | 16 ++++++++++++++++ rust/src/main.rs | 2 +- rust/src/safari.rs | 3 ++- rust/src/safaritp.rs | 3 ++- rust/tests/cli_tests.rs | 19 +++++++++++++++++-- 9 files changed, 45 insertions(+), 9 deletions(-) diff --git a/rust/README.md b/rust/README.md index 4df408ddc375d..6f657e8789709 100644 --- a/rust/README.md +++ b/rust/README.md @@ -32,7 +32,7 @@ Options: --browser-version Major browser version (e.g., 105, 106, etc. Also: beta, dev, canary -or nightly- is accepted) --browser-path - Browser path (absolute) for browser version detection (e.g., /usr/bin/google-chrome, "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome", "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe") + Browser path (absolute) for browser version detection (e.g., /usr/bin/google-chrome, "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome", "C:\Program Files\Google\Chrome\Application\chrome.exe") --output Output type: LOGGER (using INFO, WARN, etc.), JSON (custom JSON notation), or SHELL (Unix-like) [default: LOGGER] --proxy diff --git a/rust/src/chrome.rs b/rust/src/chrome.rs index bbe88f654addd..5f1026ee2f2fd 100644 --- a/rust/src/chrome.rs +++ b/rust/src/chrome.rs @@ -261,7 +261,8 @@ impl SeleniumManager for ChromeManager { fn discover_browser_version(&self) -> Option { let mut commands; - let mut browser_path = self.get_browser_path(); + let escaped_browser_path = self.get_escaped_browser_path(); + let mut browser_path = escaped_browser_path.as_str(); if browser_path.is_empty() { match self.detect_browser_path() { Some(path) => { diff --git a/rust/src/edge.rs b/rust/src/edge.rs index 95de9584de098..da43bb0237fc4 100644 --- a/rust/src/edge.rs +++ b/rust/src/edge.rs @@ -120,7 +120,8 @@ impl SeleniumManager for EdgeManager { fn discover_browser_version(&self) -> Option { let mut commands; - let mut browser_path = self.get_browser_path(); + let escaped_browser_path = self.get_escaped_browser_path(); + let mut browser_path = escaped_browser_path.as_str(); if browser_path.is_empty() { match self.detect_browser_path() { Some(path) => { diff --git a/rust/src/firefox.rs b/rust/src/firefox.rs index 09cbeef7ca556..f5bbcf476a763 100644 --- a/rust/src/firefox.rs +++ b/rust/src/firefox.rs @@ -120,7 +120,8 @@ impl SeleniumManager for FirefoxManager { fn discover_browser_version(&self) -> Option { let mut commands; - let mut browser_path = self.get_browser_path(); + let escaped_browser_path = self.get_escaped_browser_path(); + let mut browser_path = escaped_browser_path.as_str(); if browser_path.is_empty() { match self.detect_browser_path() { Some(path) => { diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 52b4a31ca1f5c..4573965e79006 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -482,6 +482,22 @@ pub trait SeleniumManager { self.get_config().browser_path.as_str() } + fn get_escaped_browser_path(&self) -> String { + let mut browser_path = self.get_browser_path().to_string(); + let path = Path::new(&browser_path); + if path.exists() && WINDOWS.is(self.get_os()) { + browser_path = Path::new(path) + .canonicalize() + .unwrap() + .to_str() + .unwrap() + .to_string() + .replace("\\\\?\\", "") + .replace("\\", "\\\\"); + } + browser_path + } + fn set_browser_path(&mut self, browser_path: String) { if !browser_path.is_empty() { let mut config = self.get_config_mut(); diff --git a/rust/src/main.rs b/rust/src/main.rs index e85365df4af60..6add4ac0568c2 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -64,7 +64,7 @@ struct Cli { /// Browser path (absolute) for browser version detection (e.g., /usr/bin/google-chrome, /// "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome", - /// "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe") + /// "C:\Program Files\Google\Chrome\Application\chrome.exe") #[clap(long, value_parser)] browser_path: Option, diff --git a/rust/src/safari.rs b/rust/src/safari.rs index 13970c2a4c2e2..1ef13264381a0 100644 --- a/rust/src/safari.rs +++ b/rust/src/safari.rs @@ -76,7 +76,8 @@ impl SeleniumManager for SafariManager { } fn discover_browser_version(&self) -> Option { - let mut browser_path = self.get_browser_path(); + let escaped_browser_path = self.get_escaped_browser_path(); + let mut browser_path = escaped_browser_path.as_str(); if browser_path.is_empty() { match self.detect_browser_path() { Some(path) => { diff --git a/rust/src/safaritp.rs b/rust/src/safaritp.rs index 5de9475a609b3..6a4b40eea17cb 100644 --- a/rust/src/safaritp.rs +++ b/rust/src/safaritp.rs @@ -81,7 +81,8 @@ impl SeleniumManager for SafariTPManager { } fn discover_browser_version(&self) -> Option { - let mut browser_path = self.get_browser_path(); + let escaped_browser_path = self.get_escaped_browser_path(); + let mut browser_path = escaped_browser_path.as_str(); if browser_path.is_empty() { match self.detect_browser_path() { Some(path) => { diff --git a/rust/tests/cli_tests.rs b/rust/tests/cli_tests.rs index 5b3006a575796..26307db8c689e 100644 --- a/rust/tests/cli_tests.rs +++ b/rust/tests/cli_tests.rs @@ -17,6 +17,7 @@ use assert_cmd::Command; use rstest::rstest; +use std::env::consts::OS; use std::str; #[rstest] @@ -132,15 +133,22 @@ fn beta_test(#[case] browser: String, #[case] driver_name: String) { #[rstest] #[case( + "windows", "chrome", r#"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"# )] -#[case("chrome", "/usr/bin/google-chrome")] #[case( + "windows", + "chrome", + 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"# )] -fn path_test(#[case] browser: String, #[case] browser_path: String) { +fn path_test(#[case] os: String, #[case] browser: String, #[case] browser_path: String) { println!( "Path test browser={} -- browser_path={}", browser, browser_path @@ -151,4 +159,11 @@ fn path_test(#[case] browser: String, #[case] browser_path: String) { .assert() .success() .code(0); + + if OS.eq(&os) { + let stdout = &cmd.unwrap().stdout; + let output = str::from_utf8(stdout).unwrap(); + println!("output {:?}", output); + assert!(!output.contains("WARN")); + } }