Skip to content

Commit

Permalink
GetMatchingBrowserVersion for other browsers (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahelsaig committed May 22, 2022
1 parent 9daaf8b commit b79f23f
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 6 deletions.
5 changes: 4 additions & 1 deletion WebDriverManager.Tests/FirefoxConfigTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public void DriverDownloadTest()
[Fact]
public void GetMatchingBrowserVersionTest()
{
Assert.Throws<NotImplementedException>(GetMatchingBrowserVersion);
var version = GetMatchingBrowserVersion();
var regex = new Regex(@"^\d+\.\d+(\.\d+)?$");
Assert.NotEmpty(version);
Assert.Matches(regex, version);
}
}
}
5 changes: 4 additions & 1 deletion WebDriverManager.Tests/InternetExplorerConfigTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public void DriverDownloadTest()
[Fact]
public void GetMatchingBrowserVersionTest()
{
Assert.Throws<NotImplementedException>(GetMatchingBrowserVersion);
var version = GetMatchingBrowserVersion();
var regex = new Regex(@"^\d+\.\d+\.\d+(\.\d+)?$");
Assert.NotEmpty(version);
Assert.Matches(regex, version);
}
}
}
4 changes: 3 additions & 1 deletion WebDriverManager/DriverConfigs/Impl/ChromeConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ private string GetRawBrowserVersion()

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return RegistryHelper.GetInstalledBrowserVersionLinux("google-chrome", "--product-version");
return RegistryHelper.GetInstalledBrowserVersionLinux(
"google-chrome", "--product-version",
"chromium", "--version");
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
Expand Down
22 changes: 21 additions & 1 deletion WebDriverManager/DriverConfigs/Impl/FirefoxConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Net;
using System.Runtime.InteropServices;
using AngleSharp.Html.Parser;
using WebDriverManager.Helpers;
using Architecture = WebDriverManager.Helpers.Architecture;

namespace WebDriverManager.DriverConfigs.Impl
Expand Down Expand Up @@ -53,7 +54,26 @@ public virtual string GetLatestVersion()

public virtual string GetMatchingBrowserVersion()
{
throw new NotImplementedException();
#if NETSTANDARD
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return RegistryHelper.GetInstalledBrowserVersionOsx("Firefox", "--version");
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return RegistryHelper.GetInstalledBrowserVersionLinux("firefox", "--version");
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return RegistryHelper.GetInstalledBrowserVersionWin("firefox.exe");
}

throw new PlatformNotSupportedException("Your operating system is not supported");
#else
return RegistryHelper.GetInstalledBrowserVersionWin("firefox.exe");
#endif
}

private static string GetUrl(Architecture architecture)
Expand Down
14 changes: 13 additions & 1 deletion WebDriverManager/DriverConfigs/Impl/InternetExplorerConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace WebDriverManager.DriverConfigs.Impl
{
Expand Down Expand Up @@ -31,7 +33,17 @@ public virtual string GetLatestVersion()

public virtual string GetMatchingBrowserVersion()
{
throw new NotImplementedException();
#if NETSTANDARD
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
throw new PlatformNotSupportedException("Your operating system is not supported");
}
#endif

return (string)Registry.GetValue(
@"HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer",
"svcVersion",
"Latest");
}
}
}
39 changes: 39 additions & 0 deletions WebDriverManager/Helpers/RegistryHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;

namespace WebDriverManager.Helpers
Expand All @@ -22,6 +23,24 @@ public static string GetInstalledBrowserVersionLinux(string executableFileName,
}
}

public static string GetInstalledBrowserVersionLinux(params string[] executableAndArgumentsPairs)
{
var length = executableAndArgumentsPairs.Length;
if (length % 2 == 1) throw new Exception("Please provide arguments for every executable!");

for (var i = 0; i < length; i += 2)
{
var executableFileName = executableAndArgumentsPairs[i];
var arguments = executableAndArgumentsPairs[i + 1];

var fullPath = GetFullPath(executableFileName);
if (fullPath != null) return GetInstalledBrowserVersionLinux(fullPath, arguments);
}

throw new Exception(
$"Unable to locate installed browser for runtime platform {Environment.OSVersion.Platform}");
}

public static string GetInstalledBrowserVersionOsx(string executableFileName, string arguments)
{
try
Expand Down Expand Up @@ -62,5 +81,25 @@ public static string GetInstalledBrowserVersionWin(string executableFileName)

return null;
}

/// <summary>
/// Checks if a provided file name can be found in either the current working directory or the <c>PATH</c>
/// environment variable.
/// </summary>
/// <param name="fileName">The file name of the executable, including extension on Windows.</param>
/// <returns>The full path of the executable or <see langword="null"/> if it doesn't exist.</returns>
private static string GetFullPath(string fileName)
{
if (File.Exists(fileName)) return Path.GetFullPath(fileName);

var paths = Environment.GetEnvironmentVariable("PATH")?.Split(Path.PathSeparator) ?? Array.Empty<string>();
foreach (var path in paths)
{
var fullPath = Path.Combine(path, fileName);
if (File.Exists(fullPath)) return fullPath;
}

return null;
}
}
}
5 changes: 4 additions & 1 deletion WebDriverManager/Helpers/VersionHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

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

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

0 comments on commit b79f23f

Please sign in to comment.