diff --git a/dotnet/src/webdriver/Chromium/ChromiumDriver.cs b/dotnet/src/webdriver/Chromium/ChromiumDriver.cs index d6fbebd953d84..f1fc0503f8b0f 100644 --- a/dotnet/src/webdriver/Chromium/ChromiumDriver.cs +++ b/dotnet/src/webdriver/Chromium/ChromiumDriver.cs @@ -168,9 +168,9 @@ private static ICommandExecutor GenerateDriverServiceCommandExecutor(DriverServi string fullServicePath = finder.GetDriverPath(); service.DriverServicePath = Path.GetDirectoryName(fullServicePath); service.DriverServiceExecutableName = Path.GetFileName(fullServicePath); - if (finder.HasBrowserPath()) + if (finder.TryGetBrowserPath(out string browserPath)) { - options.BinaryLocation = finder.GetBrowserPath(); + options.BinaryLocation = browserPath; options.BrowserVersion = null; } } diff --git a/dotnet/src/webdriver/DriverFinder.cs b/dotnet/src/webdriver/DriverFinder.cs index 2f768af71414f..901784a51cb29 100644 --- a/dotnet/src/webdriver/DriverFinder.cs +++ b/dotnet/src/webdriver/DriverFinder.cs @@ -19,10 +19,13 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.IO; using System.Text; +#nullable enable + namespace OpenQA.Selenium { /// @@ -31,17 +34,16 @@ namespace OpenQA.Selenium /// public class DriverFinder { - private DriverOptions options; + private readonly DriverOptions options; private Dictionary paths = new Dictionary(); - private const string BrowserPathKey = "browser_path"; - private const string DriverPathKey = "driver_path"; /// /// Initializes a new instance of the class. /// + /// If is . public DriverFinder(DriverOptions options) { - this.options = options; + this.options = options ?? throw new ArgumentNullException(nameof(options)); } /// @@ -52,7 +54,7 @@ public DriverFinder(DriverOptions options) /// public string GetBrowserPath() { - return BinaryPaths()[BrowserPathKey]; + return BinaryPaths()[SeleniumManager.BrowserPathKey]; } /// @@ -63,14 +65,36 @@ public string GetBrowserPath() /// public string GetDriverPath() { - return BinaryPaths()[DriverPathKey]; + return BinaryPaths()[SeleniumManager.DriverPathKey]; } + /// + /// Gets whether there is a browser path for the given browser on this platform. + /// + /// if a browser path exists; otherwise, . public bool HasBrowserPath() { return !string.IsNullOrWhiteSpace(GetBrowserPath()); } + /// + /// Tries to get the browser path, as retrieved by Selenium Manager. + /// + /// If the method returns , the full browser path. + /// if a browser path exists; otherwise, . + public bool TryGetBrowserPath([NotNullWhen(true)] out string? browserPath) + { + string? path = GetBrowserPath(); + if (!string.IsNullOrWhiteSpace(path)) + { + browserPath = path; + return true; + } + + browserPath = null; + return false; + } + /// /// Invokes Selenium Manager to get the binaries paths and validates if they exist. /// @@ -80,29 +104,33 @@ public bool HasBrowserPath() /// If one of the paths does not exist. private Dictionary BinaryPaths() { - if (paths.ContainsKey(DriverPathKey) && !string.IsNullOrWhiteSpace(paths[DriverPathKey])) + if (paths.ContainsKey(SeleniumManager.DriverPathKey) && !string.IsNullOrWhiteSpace(paths[SeleniumManager.DriverPathKey])) { return paths; } + Dictionary binaryPaths = SeleniumManager.BinaryPaths(CreateArguments()); - string driverPath = binaryPaths[DriverPathKey]; - string browserPath = binaryPaths[BrowserPathKey]; + string driverPath = binaryPaths[SeleniumManager.DriverPathKey]; + string browserPath = binaryPaths[SeleniumManager.BrowserPathKey]; + if (File.Exists(driverPath)) { - paths.Add(DriverPathKey, driverPath); + paths.Add(SeleniumManager.DriverPathKey, driverPath); } else { throw new NoSuchDriverException($"The driver path is not a valid file: {driverPath}"); } + if (File.Exists(browserPath)) { - paths.Add(BrowserPathKey, browserPath); + paths.Add(SeleniumManager.BrowserPathKey, browserPath); } else { throw new NoSuchDriverException($"The browser path is not a valid file: {browserPath}"); } + return paths; } @@ -123,7 +151,7 @@ private string CreateArguments() argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --browser-version {0}", options.BrowserVersion); } - string browserBinary = options.BinaryLocation; + string? browserBinary = options.BinaryLocation; if (!string.IsNullOrEmpty(browserBinary)) { argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --browser-path \"{0}\"", browserBinary); diff --git a/dotnet/src/webdriver/Firefox/FirefoxDriver.cs b/dotnet/src/webdriver/Firefox/FirefoxDriver.cs index e2b809791a835..98a1e43ce056a 100644 --- a/dotnet/src/webdriver/Firefox/FirefoxDriver.cs +++ b/dotnet/src/webdriver/Firefox/FirefoxDriver.cs @@ -221,9 +221,9 @@ private static ICommandExecutor GenerateDriverServiceCommandExecutor(DriverServi string fullServicePath = finder.GetDriverPath(); service.DriverServicePath = Path.GetDirectoryName(fullServicePath); service.DriverServiceExecutableName = Path.GetFileName(fullServicePath); - if (finder.HasBrowserPath()) + if (finder.TryGetBrowserPath(out string browserPath)) { - options.BinaryLocation = finder.GetBrowserPath(); + options.BinaryLocation = browserPath; options.BrowserVersion = null; } } diff --git a/dotnet/src/webdriver/SeleniumManager.cs b/dotnet/src/webdriver/SeleniumManager.cs index 5e2f99c46fa98..324cfa5b34957 100644 --- a/dotnet/src/webdriver/SeleniumManager.cs +++ b/dotnet/src/webdriver/SeleniumManager.cs @@ -38,6 +38,9 @@ namespace OpenQA.Selenium /// public static class SeleniumManager { + internal const string DriverPathKey = "driver_path"; + internal const string BrowserPathKey = "browser_path"; + private static readonly ILogger _logger = Log.GetLogger(typeof(SeleniumManager)); private static readonly Lazy _lazyBinaryFullPath = new(() => @@ -93,14 +96,14 @@ public static Dictionary BinaryPaths(string arguments) var smCommandResult = RunCommand(_lazyBinaryFullPath.Value, argsBuilder.ToString()); Dictionary binaryPaths = new() { - { "browser_path", smCommandResult.BrowserPath }, - { "driver_path", smCommandResult.DriverPath } + { BrowserPathKey, smCommandResult.BrowserPath }, + { DriverPathKey, smCommandResult.DriverPath } }; if (_logger.IsEnabled(LogEventLevel.Trace)) { - _logger.Trace($"Driver path: {binaryPaths["driver_path"]}"); - _logger.Trace($"Browser path: {binaryPaths["browser_path"]}"); + _logger.Trace($"Driver path: {binaryPaths[DriverPathKey]}"); + _logger.Trace($"Browser path: {binaryPaths[BrowserPathKey]}"); } return binaryPaths; @@ -214,9 +217,9 @@ public sealed record LogEntryResponse(string Level, string Message); public sealed record ResultResponse ( - [property: JsonPropertyName("driver_path")] + [property: JsonPropertyName(SeleniumManager.DriverPathKey)] string DriverPath, - [property: JsonPropertyName("browser_path")] + [property: JsonPropertyName(SeleniumManager.BrowserPathKey)] string BrowserPath ); }