Skip to content

Commit 3887639

Browse files
authored
[rust] Use escaped browser path (required by wmic commands) in Selenium Manager (#12304)
1 parent 054467a commit 3887639

File tree

9 files changed

+45
-9
lines changed

9 files changed

+45
-9
lines changed

rust/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Options:
3232
--browser-version <BROWSER_VERSION>
3333
Major browser version (e.g., 105, 106, etc. Also: beta, dev, canary -or nightly- is accepted)
3434
--browser-path <BROWSER_PATH>
35-
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")
35+
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")
3636
--output <OUTPUT>
3737
Output type: LOGGER (using INFO, WARN, etc.), JSON (custom JSON notation), or SHELL (Unix-like) [default: LOGGER]
3838
--proxy <PROXY>

rust/src/chrome.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ impl SeleniumManager for ChromeManager {
261261

262262
fn discover_browser_version(&self) -> Option<String> {
263263
let mut commands;
264-
let mut browser_path = self.get_browser_path();
264+
let escaped_browser_path = self.get_escaped_browser_path();
265+
let mut browser_path = escaped_browser_path.as_str();
265266
if browser_path.is_empty() {
266267
match self.detect_browser_path() {
267268
Some(path) => {

rust/src/edge.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ impl SeleniumManager for EdgeManager {
120120

121121
fn discover_browser_version(&self) -> Option<String> {
122122
let mut commands;
123-
let mut browser_path = self.get_browser_path();
123+
let escaped_browser_path = self.get_escaped_browser_path();
124+
let mut browser_path = escaped_browser_path.as_str();
124125
if browser_path.is_empty() {
125126
match self.detect_browser_path() {
126127
Some(path) => {

rust/src/firefox.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ impl SeleniumManager for FirefoxManager {
120120

121121
fn discover_browser_version(&self) -> Option<String> {
122122
let mut commands;
123-
let mut browser_path = self.get_browser_path();
123+
let escaped_browser_path = self.get_escaped_browser_path();
124+
let mut browser_path = escaped_browser_path.as_str();
124125
if browser_path.is_empty() {
125126
match self.detect_browser_path() {
126127
Some(path) => {

rust/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,22 @@ pub trait SeleniumManager {
482482
self.get_config().browser_path.as_str()
483483
}
484484

485+
fn get_escaped_browser_path(&self) -> String {
486+
let mut browser_path = self.get_browser_path().to_string();
487+
let path = Path::new(&browser_path);
488+
if path.exists() && WINDOWS.is(self.get_os()) {
489+
browser_path = Path::new(path)
490+
.canonicalize()
491+
.unwrap()
492+
.to_str()
493+
.unwrap()
494+
.to_string()
495+
.replace("\\\\?\\", "")
496+
.replace("\\", "\\\\");
497+
}
498+
browser_path
499+
}
500+
485501
fn set_browser_path(&mut self, browser_path: String) {
486502
if !browser_path.is_empty() {
487503
let mut config = self.get_config_mut();

rust/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ struct Cli {
6464

6565
/// Browser path (absolute) for browser version detection (e.g., /usr/bin/google-chrome,
6666
/// "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome",
67-
/// "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe")
67+
/// "C:\Program Files\Google\Chrome\Application\chrome.exe")
6868
#[clap(long, value_parser)]
6969
browser_path: Option<String>,
7070

rust/src/safari.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ impl SeleniumManager for SafariManager {
7676
}
7777

7878
fn discover_browser_version(&self) -> Option<String> {
79-
let mut browser_path = self.get_browser_path();
79+
let escaped_browser_path = self.get_escaped_browser_path();
80+
let mut browser_path = escaped_browser_path.as_str();
8081
if browser_path.is_empty() {
8182
match self.detect_browser_path() {
8283
Some(path) => {

rust/src/safaritp.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ impl SeleniumManager for SafariTPManager {
8181
}
8282

8383
fn discover_browser_version(&self) -> Option<String> {
84-
let mut browser_path = self.get_browser_path();
84+
let escaped_browser_path = self.get_escaped_browser_path();
85+
let mut browser_path = escaped_browser_path.as_str();
8586
if browser_path.is_empty() {
8687
match self.detect_browser_path() {
8788
Some(path) => {

rust/tests/cli_tests.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
use assert_cmd::Command;
1919
use rstest::rstest;
20+
use std::env::consts::OS;
2021
use std::str;
2122

2223
#[rstest]
@@ -132,15 +133,22 @@ fn beta_test(#[case] browser: String, #[case] driver_name: String) {
132133

133134
#[rstest]
134135
#[case(
136+
"windows",
135137
"chrome",
136138
r#"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"#
137139
)]
138-
#[case("chrome", "/usr/bin/google-chrome")]
139140
#[case(
141+
"windows",
142+
"chrome",
143+
r#"C:\Program Files\Google\Chrome\Application\chrome.exe"#
144+
)]
145+
#[case("linux", "chrome", "/usr/bin/google-chrome")]
146+
#[case(
147+
"macos",
140148
"chrome",
141149
r#"/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"#
142150
)]
143-
fn path_test(#[case] browser: String, #[case] browser_path: String) {
151+
fn path_test(#[case] os: String, #[case] browser: String, #[case] browser_path: String) {
144152
println!(
145153
"Path test browser={} -- browser_path={}",
146154
browser, browser_path
@@ -151,4 +159,11 @@ fn path_test(#[case] browser: String, #[case] browser_path: String) {
151159
.assert()
152160
.success()
153161
.code(0);
162+
163+
if OS.eq(&os) {
164+
let stdout = &cmd.unwrap().stdout;
165+
let output = str::from_utf8(stdout).unwrap();
166+
println!("output {:?}", output);
167+
assert!(!output.contains("WARN"));
168+
}
154169
}

0 commit comments

Comments
 (0)