Skip to content

Commit 08fc25d

Browse files
iouymrosolko
andauthored
Cross platform chrome matching support (#133)
Co-authored-by: Aliaksandr Rasolka <[email protected]>
1 parent 2415aeb commit 08fc25d

File tree

4 files changed

+119
-19
lines changed

4 files changed

+119
-19
lines changed

WebDriverManager/DriverConfigs/Impl/ChromeConfig.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ public class ChromeConfig : IDriverConfig
1414
private const string ExactReleaseVersionPatternUrl =
1515
"https://chromedriver.storage.googleapis.com/LATEST_RELEASE_<version>";
1616

17-
private const string BrowserExecutableFileName = "chrome.exe";
18-
1917
public virtual string GetName()
2018
{
2119
return "Chrome";
@@ -78,10 +76,30 @@ private static string GetLatestVersion(string url)
7876

7977
public virtual string GetMatchingBrowserVersion()
8078
{
81-
var rawChromeBrowserVersion = RegistryHelper.GetInstalledBrowserVersion(BrowserExecutableFileName);
79+
var rawChromeBrowserVersion = GetRawBrowserVersion();
8280
var chromeBrowserVersion = VersionHelper.GetVersionWithoutRevision(rawChromeBrowserVersion);
8381
var url = ExactReleaseVersionPatternUrl.Replace("<version>", chromeBrowserVersion);
8482
return GetLatestVersion(url);
8583
}
84+
85+
private string GetRawBrowserVersion()
86+
{
87+
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
88+
{
89+
return RegistryHelper.GetInstalledBrowserVersionOsx("Google Chrome", "--version");
90+
}
91+
92+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
93+
{
94+
return RegistryHelper.GetInstalledBrowserVersionLinux("google-chrome", "--product-version");
95+
}
96+
97+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
98+
{
99+
return RegistryHelper.GetInstalledBrowserVersionWin("chrome.exe");
100+
}
101+
102+
throw new PlatformNotSupportedException("Your operating system is not supported");
103+
}
86104
}
87105
}

WebDriverManager/DriverConfigs/Impl/EdgeConfig.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ public class EdgeConfig : IDriverConfig
1111
private const string BaseVersionPatternUrl = "https://msedgedriver.azureedge.net/<version>/";
1212
private const string LatestReleaseVersionUrl = "https://msedgedriver.azureedge.net/LATEST_STABLE";
1313

14-
private const string BrowserExecutableFileName = "msedge.exe";
15-
1614
public virtual string GetName()
1715
{
1816
return "Edge";
@@ -42,7 +40,7 @@ public virtual string GetLatestVersion()
4240
return GetLatestVersion(LatestReleaseVersionUrl);
4341
}
4442

45-
public virtual string GetLatestVersion(String url)
43+
public virtual string GetLatestVersion(string url)
4644
{
4745
var uri = new Uri(url);
4846
var webRequest = WebRequest.Create(uri);
@@ -62,8 +60,17 @@ public virtual string GetLatestVersion(String url)
6260

6361
public string GetMatchingBrowserVersion()
6462
{
65-
var rawEdgeBrowserVersion = RegistryHelper.GetInstalledBrowserVersion(BrowserExecutableFileName);
66-
return rawEdgeBrowserVersion;
63+
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
64+
{
65+
return RegistryHelper.GetInstalledBrowserVersionOsx("Microsoft Edge", "--version");
66+
}
67+
68+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
69+
{
70+
return RegistryHelper.GetInstalledBrowserVersionWin("msedge.exe");
71+
}
72+
73+
throw new PlatformNotSupportedException("Your operating system is not supported");
6774
}
6875
}
6976
}

WebDriverManager/Helpers/RegistryHelper.cs

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,60 @@
1+
using System;
12
using System.Diagnostics;
2-
using Microsoft.Win32;
3+
using System.Threading.Tasks;
34

45
namespace WebDriverManager.Helpers
56
{
67
public static class RegistryHelper
78
{
8-
private const string CurrentUserRegistryPathPattern = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\App Paths\<executableFileName>";
9-
private const string LocalMachineRegistryPathPattern = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\<executableFileName>";
9+
public static string GetInstalledBrowserVersionLinux(string executableFileName, string arguments)
10+
{
11+
try
12+
{
13+
var outputVersion = Task.Run(() => VersionHelper.GetVersionFromProcess(executableFileName, arguments))
14+
.Result;
15+
return outputVersion;
16+
}
17+
catch (Exception e)
18+
{
19+
throw new Exception(
20+
$"An error occured trying to locate installed browser version for runtime platform {Environment.OSVersion.Platform}",
21+
e);
22+
}
23+
}
24+
25+
public static string GetInstalledBrowserVersionOsx(string executableFileName, string arguments)
26+
{
27+
try
28+
{
29+
var executableFilePath = $"/Applications/{executableFileName}.app/Contents/MacOS/{executableFileName}";
30+
var outputVersion = Task.Run(() => VersionHelper.GetVersionFromProcess(executableFilePath, arguments))
31+
.Result;
32+
return outputVersion.Replace($"{executableFileName} ", "");
33+
}
34+
catch (Exception e)
35+
{
36+
throw new Exception(
37+
$"An error occured trying to locate installed browser version for runtime platform {Environment.OSVersion.Platform}",
38+
e);
39+
}
40+
}
1041

11-
public static string GetInstalledBrowserVersion(string executableFileName)
42+
public static string GetInstalledBrowserVersionWin(string executableFileName)
1243
{
13-
var currentUserPath = Registry.GetValue(CurrentUserRegistryPathPattern.Replace("<executableFileName>", executableFileName), "", null);
44+
const string currentUserRegistryPathPattern =
45+
@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\App Paths\<executableFileName>";
46+
const string localMachineRegistryPathPattern =
47+
@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\<executableFileName>";
48+
49+
var currentUserPath = Microsoft.Win32.Registry.GetValue(
50+
currentUserRegistryPathPattern.Replace("<executableFileName>", executableFileName), "", null);
1451
if (currentUserPath != null)
1552
{
1653
return FileVersionInfo.GetVersionInfo(currentUserPath.ToString()).FileVersion;
1754
}
1855

19-
var localMachinePath = Registry.GetValue(LocalMachineRegistryPathPattern.Replace("<executableFileName>", executableFileName), "", null);
56+
var localMachinePath = Microsoft.Win32.Registry.GetValue(
57+
localMachineRegistryPathPattern.Replace("<executableFileName>", executableFileName), "", null);
2058
if (localMachinePath != null)
2159
{
2260
return FileVersionInfo.GetVersionInfo(localMachinePath.ToString()).FileVersion;
Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,55 @@
11
using System;
2+
using System.Diagnostics;
3+
using System.Threading.Tasks;
24

35
namespace WebDriverManager.Helpers
46
{
57
public static class VersionHelper
68
{
7-
/*
8-
* Returns a version number without the revision part.
9-
*
10-
* Example: 85.0.4183.83 -> 85.0.4183
11-
*/
9+
/// <summary>
10+
/// Returns a version number without the revision part.
11+
/// Example: 85.0.4183.83 -> 85.0.4183
12+
/// </summary>
13+
/// <param name="version">Version to process</param>
14+
/// <returns>Processed version</returns>
1215
public static string GetVersionWithoutRevision(string version)
1316
{
1417
var parsedVersion = Version.Parse(version);
1518
return $"{parsedVersion.Major}.{parsedVersion.Minor}.{parsedVersion.Build}";
1619
}
20+
21+
/// <summary>
22+
/// Gets the current browser version on the executing system by running a process
23+
/// </summary>
24+
/// <param name="executableFileName">Browser executable file name</param>
25+
/// <param name="arguments">Execution command line arguments</param>
26+
/// <returns>Browser version received from browser started instance</returns>
27+
public static async Task<string> GetVersionFromProcess(string executableFileName, string arguments)
28+
{
29+
var process = Process.Start(
30+
new ProcessStartInfo
31+
{
32+
FileName = executableFileName,
33+
Arguments = arguments,
34+
UseShellExecute = false,
35+
CreateNoWindow = true,
36+
RedirectStandardOutput = true,
37+
RedirectStandardError = true
38+
}
39+
);
40+
if (process == null) throw new Exception("The process did not start");
41+
42+
var output = await process.StandardOutput.ReadToEndAsync();
43+
var error = await process.StandardError.ReadToEndAsync();
44+
process.WaitForExit();
45+
process.Kill();
46+
47+
if (!string.IsNullOrEmpty(error))
48+
{
49+
throw new Exception(error);
50+
}
51+
52+
return output;
53+
}
1754
}
1855
}

0 commit comments

Comments
 (0)