Skip to content

Commit

Permalink
[rust] Fix webview2 support (#12966)
Browse files Browse the repository at this point in the history
[rust] Improve support for webview2
  • Loading branch information
bonigarcia authored Oct 18, 2023
1 parent b008e15 commit cfe7e1f
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 54 deletions.
115 changes: 70 additions & 45 deletions rust/src/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::metadata::{
};
use crate::{
create_http_client, get_binary_extension, path_to_string, Logger, SeleniumManager, BETA,
DASH_DASH_VERSION, DEV, ENV_PROGRAM_FILES_X86, NIGHTLY, OFFLINE_REQUEST_ERR_MSG,
DASH_DASH_VERSION, DEV, ENV_PROGRAM_FILES_X86, NIGHTLY, OFFLINE_REQUEST_ERR_MSG, REG_PV_ARG,
REG_VERSION_ARG, STABLE,
};
use anyhow::Error;
Expand Down Expand Up @@ -104,54 +104,79 @@ impl SeleniumManager for EdgeManager {
}

fn get_browser_path_map(&self) -> HashMap<BrowserPath, &str> {
HashMap::from([
(
if self.is_webview2() {
HashMap::from([(
BrowserPath::new(WINDOWS, STABLE),
r#"Microsoft\Edge\Application\msedge.exe"#,
),
(
BrowserPath::new(WINDOWS, BETA),
r#"Microsoft\Edge Beta\Application\msedge.exe"#,
),
(
BrowserPath::new(WINDOWS, DEV),
r#"Microsoft\Edge Dev\Application\msedge.exe"#,
),
(
BrowserPath::new(WINDOWS, NIGHTLY),
r#"Microsoft\Edge SxS\Application\msedge.exe"#,
),
(
BrowserPath::new(MACOS, STABLE),
r#"/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge"#,
),
(
BrowserPath::new(MACOS, BETA),
r#"/Applications/Microsoft Edge Beta.app/Contents/MacOS/Microsoft Edge Beta"#,
),
(
BrowserPath::new(MACOS, DEV),
r#"/Applications/Microsoft Edge Dev.app/Contents/MacOS/Microsoft Edge Dev"#,
),
(
BrowserPath::new(MACOS, NIGHTLY),
r#"/Applications/Microsoft Edge Canary.app/Contents/MacOS/Microsoft Edge Canary"#,
),
(BrowserPath::new(LINUX, STABLE), "/usr/bin/microsoft-edge"),
(
BrowserPath::new(LINUX, BETA),
"/usr/bin/microsoft-edge-beta",
),
(BrowserPath::new(LINUX, DEV), "/usr/bin/microsoft-edge-dev"),
])
r#"Microsoft\EdgeWebView\Application"#,
)])
} else {
HashMap::from([
(
BrowserPath::new(WINDOWS, STABLE),
r#"Microsoft\Edge\Application\msedge.exe"#,
),
(
BrowserPath::new(WINDOWS, BETA),
r#"Microsoft\Edge Beta\Application\msedge.exe"#,
),
(
BrowserPath::new(WINDOWS, DEV),
r#"Microsoft\Edge Dev\Application\msedge.exe"#,
),
(
BrowserPath::new(WINDOWS, NIGHTLY),
r#"Microsoft\Edge SxS\Application\msedge.exe"#,
),
(
BrowserPath::new(MACOS, STABLE),
r#"/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge"#,
),
(
BrowserPath::new(MACOS, BETA),
r#"/Applications/Microsoft Edge Beta.app/Contents/MacOS/Microsoft Edge Beta"#,
),
(
BrowserPath::new(MACOS, DEV),
r#"/Applications/Microsoft Edge Dev.app/Contents/MacOS/Microsoft Edge Dev"#,
),
(
BrowserPath::new(MACOS, NIGHTLY),
r#"/Applications/Microsoft Edge Canary.app/Contents/MacOS/Microsoft Edge Canary"#,
),
(BrowserPath::new(LINUX, STABLE), "/usr/bin/microsoft-edge"),
(
BrowserPath::new(LINUX, BETA),
"/usr/bin/microsoft-edge-beta",
),
(BrowserPath::new(LINUX, DEV), "/usr/bin/microsoft-edge-dev"),
])
}
}

fn discover_browser_version(&mut self) -> Result<Option<String>, Error> {
self.general_discover_browser_version(
r#"HKCU\Software\Microsoft\Edge\BLBeacon"#,
REG_VERSION_ARG,
DASH_DASH_VERSION,
)
let (reg_key, reg_version_arg, cmd_version_arg) = if self.is_webview2() {
let arch = self.get_arch();
if X32.is(arch) {
(
r#"HKLM\SOFTWARE\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}"#,
REG_PV_ARG,
"",
)
} else {
(
r#"HKLM\SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}"#,
REG_PV_ARG,
"",
)
}
} else {
(
r#"HKCU\Software\Microsoft\Edge\BLBeacon"#,
REG_VERSION_ARG,
DASH_DASH_VERSION,
)
};
self.general_discover_browser_version(reg_key, reg_version_arg, cmd_version_arg)
}

fn get_driver_name(&self) -> &str {
Expand Down
14 changes: 12 additions & 2 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub const WMIC_COMMAND: &str = r#"wmic datafile where name='{}' get Version /val
pub const WMIC_COMMAND_OS: &str = r#"wmic os get osarchitecture"#;
pub const REG_VERSION_ARG: &str = "version";
pub const REG_CURRENT_VERSION_ARG: &str = "CurrentVersion";
pub const REG_PV_ARG: &str = "pv";
pub const PLIST_COMMAND: &str =
r#"/usr/libexec/PlistBuddy -c "print :CFBundleShortVersionString" {}/Contents/Info.plist"#;
pub const PKGUTIL_COMMAND: &str = "pkgutil --expand-full {} {}";
Expand Down Expand Up @@ -483,6 +484,15 @@ pub trait SeleniumManager {
} else {
self.set_browser_version(discovered_version);
}
if self.is_webview2() {
let browser_path = format!(
r#"{}\{}\msedge{}"#,
self.get_browser_path(),
&self.get_browser_version(),
get_binary_extension(self.get_os())
);
self.set_browser_path(browser_path);
}
}
None => {
self.get_logger().debug(format!(
Expand Down Expand Up @@ -984,7 +994,7 @@ pub trait SeleniumManager {
let mut commands = Vec::new();

if WINDOWS.is(self.get_os()) {
if !escaped_browser_path.is_empty() {
if !escaped_browser_path.is_empty() && !self.is_webview2() {
let wmic_command =
Command::new_single(format_one_arg(WMIC_COMMAND, &escaped_browser_path));
commands.push(wmic_command);
Expand Down Expand Up @@ -1159,7 +1169,7 @@ pub trait SeleniumManager {
}

fn set_browser_path(&mut self, browser_path: String) {
if !browser_path.is_empty() && !self.is_webview2() {
if !browser_path.is_empty() {
self.get_config_mut().browser_path = browser_path;
}
}
Expand Down
7 changes: 0 additions & 7 deletions rust/tests/browser_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use crate::common::{assert_driver, assert_output};
use assert_cmd::Command;
use rstest::rstest;
use selenium_manager::logger::JsonOutput;
use std::env::consts::OS;

mod common;
Expand Down Expand Up @@ -157,10 +156,4 @@ fn webview2_test() {
.code(0);

assert_driver(&mut cmd);

let stdout = &cmd.unwrap().stdout;
let output = std::str::from_utf8(stdout).unwrap();
let json: JsonOutput = serde_json::from_str(output).unwrap();
let browser_path = json.result.browser_path;
assert!(browser_path.is_empty());
}

0 comments on commit cfe7e1f

Please sign in to comment.