Skip to content

Commit b79f23f

Browse files
authored
GetMatchingBrowserVersion for other browsers (#186)
1 parent 9daaf8b commit b79f23f

File tree

7 files changed

+88
-6
lines changed

7 files changed

+88
-6
lines changed

WebDriverManager.Tests/FirefoxConfigTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ public void DriverDownloadTest()
2626
[Fact]
2727
public void GetMatchingBrowserVersionTest()
2828
{
29-
Assert.Throws<NotImplementedException>(GetMatchingBrowserVersion);
29+
var version = GetMatchingBrowserVersion();
30+
var regex = new Regex(@"^\d+\.\d+(\.\d+)?$");
31+
Assert.NotEmpty(version);
32+
Assert.Matches(regex, version);
3033
}
3134
}
3235
}

WebDriverManager.Tests/InternetExplorerConfigTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ public void DriverDownloadTest()
2626
[Fact]
2727
public void GetMatchingBrowserVersionTest()
2828
{
29-
Assert.Throws<NotImplementedException>(GetMatchingBrowserVersion);
29+
var version = GetMatchingBrowserVersion();
30+
var regex = new Regex(@"^\d+\.\d+\.\d+(\.\d+)?$");
31+
Assert.NotEmpty(version);
32+
Assert.Matches(regex, version);
3033
}
3134
}
3235
}

WebDriverManager/DriverConfigs/Impl/ChromeConfig.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ private string GetRawBrowserVersion()
102102

103103
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
104104
{
105-
return RegistryHelper.GetInstalledBrowserVersionLinux("google-chrome", "--product-version");
105+
return RegistryHelper.GetInstalledBrowserVersionLinux(
106+
"google-chrome", "--product-version",
107+
"chromium", "--version");
106108
}
107109

108110
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))

WebDriverManager/DriverConfigs/Impl/FirefoxConfig.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Net;
44
using System.Runtime.InteropServices;
55
using AngleSharp.Html.Parser;
6+
using WebDriverManager.Helpers;
67
using Architecture = WebDriverManager.Helpers.Architecture;
78

89
namespace WebDriverManager.DriverConfigs.Impl
@@ -53,7 +54,26 @@ public virtual string GetLatestVersion()
5354

5455
public virtual string GetMatchingBrowserVersion()
5556
{
56-
throw new NotImplementedException();
57+
#if NETSTANDARD
58+
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
59+
{
60+
return RegistryHelper.GetInstalledBrowserVersionOsx("Firefox", "--version");
61+
}
62+
63+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
64+
{
65+
return RegistryHelper.GetInstalledBrowserVersionLinux("firefox", "--version");
66+
}
67+
68+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
69+
{
70+
return RegistryHelper.GetInstalledBrowserVersionWin("firefox.exe");
71+
}
72+
73+
throw new PlatformNotSupportedException("Your operating system is not supported");
74+
#else
75+
return RegistryHelper.GetInstalledBrowserVersionWin("firefox.exe");
76+
#endif
5777
}
5878

5979
private static string GetUrl(Architecture architecture)

WebDriverManager/DriverConfigs/Impl/InternetExplorerConfig.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Runtime.InteropServices;
3+
using Microsoft.Win32;
24

35
namespace WebDriverManager.DriverConfigs.Impl
46
{
@@ -31,7 +33,17 @@ public virtual string GetLatestVersion()
3133

3234
public virtual string GetMatchingBrowserVersion()
3335
{
34-
throw new NotImplementedException();
36+
#if NETSTANDARD
37+
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
38+
{
39+
throw new PlatformNotSupportedException("Your operating system is not supported");
40+
}
41+
#endif
42+
43+
return (string)Registry.GetValue(
44+
@"HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer",
45+
"svcVersion",
46+
"Latest");
3547
}
3648
}
3749
}

WebDriverManager/Helpers/RegistryHelper.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics;
3+
using System.IO;
34
using System.Threading.Tasks;
45

56
namespace WebDriverManager.Helpers
@@ -22,6 +23,24 @@ public static string GetInstalledBrowserVersionLinux(string executableFileName,
2223
}
2324
}
2425

26+
public static string GetInstalledBrowserVersionLinux(params string[] executableAndArgumentsPairs)
27+
{
28+
var length = executableAndArgumentsPairs.Length;
29+
if (length % 2 == 1) throw new Exception("Please provide arguments for every executable!");
30+
31+
for (var i = 0; i < length; i += 2)
32+
{
33+
var executableFileName = executableAndArgumentsPairs[i];
34+
var arguments = executableAndArgumentsPairs[i + 1];
35+
36+
var fullPath = GetFullPath(executableFileName);
37+
if (fullPath != null) return GetInstalledBrowserVersionLinux(fullPath, arguments);
38+
}
39+
40+
throw new Exception(
41+
$"Unable to locate installed browser for runtime platform {Environment.OSVersion.Platform}");
42+
}
43+
2544
public static string GetInstalledBrowserVersionOsx(string executableFileName, string arguments)
2645
{
2746
try
@@ -62,5 +81,25 @@ public static string GetInstalledBrowserVersionWin(string executableFileName)
6281

6382
return null;
6483
}
84+
85+
/// <summary>
86+
/// Checks if a provided file name can be found in either the current working directory or the <c>PATH</c>
87+
/// environment variable.
88+
/// </summary>
89+
/// <param name="fileName">The file name of the executable, including extension on Windows.</param>
90+
/// <returns>The full path of the executable or <see langword="null"/> if it doesn't exist.</returns>
91+
private static string GetFullPath(string fileName)
92+
{
93+
if (File.Exists(fileName)) return Path.GetFullPath(fileName);
94+
95+
var paths = Environment.GetEnvironmentVariable("PATH")?.Split(Path.PathSeparator) ?? Array.Empty<string>();
96+
foreach (var path in paths)
97+
{
98+
var fullPath = Path.Combine(path, fileName);
99+
if (File.Exists(fullPath)) return fullPath;
100+
}
101+
102+
return null;
103+
}
65104
}
66105
}

WebDriverManager/Helpers/VersionHelper.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics;
3+
using System.Linq;
34
using System.Threading.Tasks;
45

56
namespace WebDriverManager.Helpers
@@ -49,7 +50,9 @@ public static async Task<string> GetVersionFromProcess(string executableFileName
4950
throw new Exception(error);
5051
}
5152

52-
return output;
53+
// Tries to pick out just the version string, e.g. from "Chromium 101.0.4951.64 Arch Linux" pick
54+
// "101.0.4951.64" and from "Mozilla Firefox 100.0" pick "100.0".
55+
return output.Split().LastOrDefault(word => Version.TryParse(word, out _)) ?? output;
5356
}
5457
}
5558
}

0 commit comments

Comments
 (0)