From 1b7dfb7a0e93fc912d9cee36c0820091541a8472 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Tue, 4 Feb 2025 15:44:47 -0500 Subject: [PATCH 01/13] [dotnet] Simplify and nullable annotate `DriverFinder` --- .../src/webdriver/Chromium/ChromiumDriver.cs | 4 +- dotnet/src/webdriver/DriverFinder.cs | 68 ++++++++++++------- dotnet/src/webdriver/Firefox/FirefoxDriver.cs | 4 +- dotnet/src/webdriver/SeleniumManager.cs | 34 +++++----- 4 files changed, 64 insertions(+), 46 deletions(-) diff --git a/dotnet/src/webdriver/Chromium/ChromiumDriver.cs b/dotnet/src/webdriver/Chromium/ChromiumDriver.cs index 489109d34f2d4..36c35463e375f 100644 --- a/dotnet/src/webdriver/Chromium/ChromiumDriver.cs +++ b/dotnet/src/webdriver/Chromium/ChromiumDriver.cs @@ -156,9 +156,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..4187c5378221f 100644 --- a/dotnet/src/webdriver/DriverFinder.cs +++ b/dotnet/src/webdriver/DriverFinder.cs @@ -18,11 +18,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 +33,18 @@ namespace OpenQA.Selenium /// public class DriverFinder { - private DriverOptions options; - private Dictionary paths = new Dictionary(); + private readonly DriverOptions options; + private SeleniumManagerPaths? paths; 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 +55,7 @@ public DriverFinder(DriverOptions options) /// public string GetBrowserPath() { - return BinaryPaths()[BrowserPathKey]; + return BinaryPaths().BrowserPath; } /// @@ -63,14 +66,36 @@ public string GetBrowserPath() /// public string GetDriverPath() { - return BinaryPaths()[DriverPathKey]; + return BinaryPaths().DriverPath; } + /// + /// 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. /// @@ -78,32 +103,25 @@ public bool HasBrowserPath() /// A Dictionary with the validated browser and driver path. /// /// If one of the paths does not exist. - private Dictionary BinaryPaths() + private SeleniumManagerPaths BinaryPaths() { - if (paths.ContainsKey(DriverPathKey) && !string.IsNullOrWhiteSpace(paths[DriverPathKey])) + if (paths is not null) { return paths; } - Dictionary binaryPaths = SeleniumManager.BinaryPaths(CreateArguments()); - string driverPath = binaryPaths[DriverPathKey]; - string browserPath = binaryPaths[BrowserPathKey]; - if (File.Exists(driverPath)) - { - paths.Add(DriverPathKey, driverPath); - } - else - { - throw new NoSuchDriverException($"The driver path is not a valid file: {driverPath}"); - } - if (File.Exists(browserPath)) + + SeleniumManagerPaths binaryPaths = SeleniumManager.BinaryPaths(CreateArguments()); + if (!File.Exists(binaryPaths.DriverPath)) { - paths.Add(BrowserPathKey, browserPath); + throw new NoSuchDriverException($"The driver path is not a valid file: {binaryPaths.DriverPath}"); } - else + + if (!File.Exists(binaryPaths.BrowserPath)) { - throw new NoSuchDriverException($"The browser path is not a valid file: {browserPath}"); + throw new NoSuchDriverException($"The browser path is not a valid file: {binaryPaths.BrowserPath}"); } - return paths; + + return paths = binaryPaths; } /// @@ -123,7 +141,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 256d6f2dac55b..20880a80562ae 100644 --- a/dotnet/src/webdriver/Firefox/FirefoxDriver.cs +++ b/dotnet/src/webdriver/Firefox/FirefoxDriver.cs @@ -203,9 +203,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..14b491c0e026f 100644 --- a/dotnet/src/webdriver/SeleniumManager.cs +++ b/dotnet/src/webdriver/SeleniumManager.cs @@ -80,7 +80,7 @@ public static class SeleniumManager /// /// An array with two entries, one for the driver path, and another one for the browser path. /// - public static Dictionary BinaryPaths(string arguments) + public static SeleniumManagerPaths BinaryPaths(string arguments) { StringBuilder argsBuilder = new StringBuilder(arguments); argsBuilder.Append(" --language-binding csharp"); @@ -91,19 +91,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 } - }; if (_logger.IsEnabled(LogEventLevel.Trace)) { - _logger.Trace($"Driver path: {binaryPaths["driver_path"]}"); - _logger.Trace($"Browser path: {binaryPaths["browser_path"]}"); + _logger.Trace($"Driver path: {smCommandResult.DriverPath}"); + _logger.Trace($"Browser path: {smCommandResult.BrowserPath}"); } - return binaryPaths; + return smCommandResult; } /// @@ -114,7 +109,7 @@ public static Dictionary BinaryPaths(string arguments) /// /// the standard output of the execution. /// - private static ResultResponse RunCommand(string fileName, string arguments) + private static SeleniumManagerPaths RunCommand(string fileName, string arguments) { Process process = new Process(); process.StartInfo.FileName = _lazyBinaryFullPath.Value; @@ -208,18 +203,23 @@ private static ResultResponse RunCommand(string fileName, string arguments) } } - internal sealed record SeleniumManagerResponse(IReadOnlyList Logs, ResultResponse Result) + internal sealed record SeleniumManagerResponse(IReadOnlyList Logs, SeleniumManagerPaths Result) { public sealed record LogEntryResponse(string Level, string Message); + } - public sealed record ResultResponse - ( - [property: JsonPropertyName("driver_path")] + /// + /// Response from Selenium Manager about the paths and their locations. + /// + /// The path to the driver binary. + /// The path to the browser binary, or if none exists. + public sealed record SeleniumManagerPaths + ( + [property: JsonPropertyName("driver_path")] string DriverPath, - [property: JsonPropertyName("browser_path")] + [property: JsonPropertyName("browser_path")] string BrowserPath - ); - } + ); [JsonSerializable(typeof(SeleniumManagerResponse))] [JsonSourceGenerationOptions(PropertyNameCaseInsensitive = true)] From 8e7b8b2c14244cd6aa308207f833c9957b85e2f7 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Fri, 28 Feb 2025 15:11:43 -0500 Subject: [PATCH 02/13] Minimize diffs --- dotnet/src/webdriver/DriverFinder.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/dotnet/src/webdriver/DriverFinder.cs b/dotnet/src/webdriver/DriverFinder.cs index 4187c5378221f..89de81686a839 100644 --- a/dotnet/src/webdriver/DriverFinder.cs +++ b/dotnet/src/webdriver/DriverFinder.cs @@ -111,12 +111,18 @@ private SeleniumManagerPaths BinaryPaths() } SeleniumManagerPaths binaryPaths = SeleniumManager.BinaryPaths(CreateArguments()); - if (!File.Exists(binaryPaths.DriverPath)) + if (File.Exists(binaryPaths.DriverPath)) + { + } + else { throw new NoSuchDriverException($"The driver path is not a valid file: {binaryPaths.DriverPath}"); } - if (!File.Exists(binaryPaths.BrowserPath)) + if (File.Exists(binaryPaths.BrowserPath)) + { + } + else { throw new NoSuchDriverException($"The browser path is not a valid file: {binaryPaths.BrowserPath}"); } From 26714e58c52551cb274b772c34d14fc185609b47 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Fri, 28 Feb 2025 15:12:56 -0500 Subject: [PATCH 03/13] Maintain previous logic --- dotnet/src/webdriver/DriverFinder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/src/webdriver/DriverFinder.cs b/dotnet/src/webdriver/DriverFinder.cs index 89de81686a839..34388de1be9e6 100644 --- a/dotnet/src/webdriver/DriverFinder.cs +++ b/dotnet/src/webdriver/DriverFinder.cs @@ -105,7 +105,7 @@ public bool TryGetBrowserPath([NotNullWhen(true)] out string? browserPath) /// If one of the paths does not exist. private SeleniumManagerPaths BinaryPaths() { - if (paths is not null) + if (paths is not null && !string.IsNullOrWhiteSpace(paths.DriverPath)) { return paths; } From f276b391f515f9ffe102cad1000064c88534d42c Mon Sep 17 00:00:00 2001 From: Michael Render Date: Fri, 28 Feb 2025 15:15:56 -0500 Subject: [PATCH 04/13] Better local name --- dotnet/src/webdriver/SeleniumManager.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dotnet/src/webdriver/SeleniumManager.cs b/dotnet/src/webdriver/SeleniumManager.cs index 14b491c0e026f..b7bba8eac52e8 100644 --- a/dotnet/src/webdriver/SeleniumManager.cs +++ b/dotnet/src/webdriver/SeleniumManager.cs @@ -90,15 +90,15 @@ public static SeleniumManagerPaths BinaryPaths(string arguments) argsBuilder.Append(" --debug"); } - var smCommandResult = RunCommand(_lazyBinaryFullPath.Value, argsBuilder.ToString()); + var binaryPaths = RunCommand(_lazyBinaryFullPath.Value, argsBuilder.ToString()); if (_logger.IsEnabled(LogEventLevel.Trace)) { - _logger.Trace($"Driver path: {smCommandResult.DriverPath}"); - _logger.Trace($"Browser path: {smCommandResult.BrowserPath}"); + _logger.Trace($"Driver path: {binaryPaths.DriverPath}"); + _logger.Trace($"Browser path: {binaryPaths.BrowserPath}"); } - return smCommandResult; + return binaryPaths; } /// From b0630187bb3198407952b2fd1297d8751f1e9317 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Fri, 28 Feb 2025 15:37:34 -0500 Subject: [PATCH 05/13] Minimize diffs --- dotnet/src/webdriver/DriverFinder.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/dotnet/src/webdriver/DriverFinder.cs b/dotnet/src/webdriver/DriverFinder.cs index 34388de1be9e6..0ea9fafe9600d 100644 --- a/dotnet/src/webdriver/DriverFinder.cs +++ b/dotnet/src/webdriver/DriverFinder.cs @@ -111,6 +111,7 @@ private SeleniumManagerPaths BinaryPaths() } SeleniumManagerPaths binaryPaths = SeleniumManager.BinaryPaths(CreateArguments()); + if (File.Exists(binaryPaths.DriverPath)) { } From 77f35ea789f91487771641ff785ff078413b65ef Mon Sep 17 00:00:00 2001 From: Michael Render Date: Fri, 28 Feb 2025 15:39:27 -0500 Subject: [PATCH 06/13] Minimize diffs further --- dotnet/src/webdriver/DriverFinder.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/dotnet/src/webdriver/DriverFinder.cs b/dotnet/src/webdriver/DriverFinder.cs index 0ea9fafe9600d..9e58e9e7f1553 100644 --- a/dotnet/src/webdriver/DriverFinder.cs +++ b/dotnet/src/webdriver/DriverFinder.cs @@ -111,21 +111,23 @@ private SeleniumManagerPaths BinaryPaths() } SeleniumManagerPaths binaryPaths = SeleniumManager.BinaryPaths(CreateArguments()); + string driverPath = binaryPaths.DriverPath; + string browserPath = binaryPaths.BrowserPath; - if (File.Exists(binaryPaths.DriverPath)) + if (File.Exists(driverPath)) { } else { - throw new NoSuchDriverException($"The driver path is not a valid file: {binaryPaths.DriverPath}"); + throw new NoSuchDriverException($"The driver path is not a valid file: {driverPath}"); } - if (File.Exists(binaryPaths.BrowserPath)) + if (File.Exists(browserPath)) { } else { - throw new NoSuchDriverException($"The browser path is not a valid file: {binaryPaths.BrowserPath}"); + throw new NoSuchDriverException($"The browser path is not a valid file: {browserPath}"); } return paths = binaryPaths; From 4914e89b695847d14908e2364866be0e4fc8a34b Mon Sep 17 00:00:00 2001 From: Michael Render Date: Fri, 28 Feb 2025 15:40:10 -0500 Subject: [PATCH 07/13] Better code style --- dotnet/src/webdriver/DriverFinder.cs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/dotnet/src/webdriver/DriverFinder.cs b/dotnet/src/webdriver/DriverFinder.cs index 9e58e9e7f1553..3484d1e40cd56 100644 --- a/dotnet/src/webdriver/DriverFinder.cs +++ b/dotnet/src/webdriver/DriverFinder.cs @@ -114,18 +114,12 @@ private SeleniumManagerPaths BinaryPaths() string driverPath = binaryPaths.DriverPath; string browserPath = binaryPaths.BrowserPath; - if (File.Exists(driverPath)) - { - } - else + if (!File.Exists(driverPath)) { throw new NoSuchDriverException($"The driver path is not a valid file: {driverPath}"); } - if (File.Exists(browserPath)) - { - } - else + if (!File.Exists(browserPath)) { throw new NoSuchDriverException($"The browser path is not a valid file: {browserPath}"); } From 312eab78e00c2ff889c42529aba575b0e2b869ca Mon Sep 17 00:00:00 2001 From: Michael Render Date: Fri, 28 Feb 2025 15:41:11 -0500 Subject: [PATCH 08/13] Modernate minimal diffs with code style --- dotnet/src/webdriver/DriverFinder.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dotnet/src/webdriver/DriverFinder.cs b/dotnet/src/webdriver/DriverFinder.cs index 3484d1e40cd56..f926d5180b770 100644 --- a/dotnet/src/webdriver/DriverFinder.cs +++ b/dotnet/src/webdriver/DriverFinder.cs @@ -114,7 +114,10 @@ private SeleniumManagerPaths BinaryPaths() string driverPath = binaryPaths.DriverPath; string browserPath = binaryPaths.BrowserPath; - if (!File.Exists(driverPath)) + if (File.Exists(driverPath)) + { + } + else { throw new NoSuchDriverException($"The driver path is not a valid file: {driverPath}"); } From 02ad8d017a51eaafa6ee0af7171019e3a35db456 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Fri, 28 Feb 2025 15:48:20 -0500 Subject: [PATCH 09/13] Go back to dictionary style --- dotnet/src/webdriver/DriverFinder.cs | 17 +++++------ dotnet/src/webdriver/SeleniumManager.cs | 38 ++++++++++++++----------- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/dotnet/src/webdriver/DriverFinder.cs b/dotnet/src/webdriver/DriverFinder.cs index f926d5180b770..5e99b29987d31 100644 --- a/dotnet/src/webdriver/DriverFinder.cs +++ b/dotnet/src/webdriver/DriverFinder.cs @@ -18,6 +18,7 @@ // using System; +using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.IO; @@ -34,7 +35,7 @@ namespace OpenQA.Selenium public class DriverFinder { private readonly DriverOptions options; - private SeleniumManagerPaths? paths; + private Dictionary? paths; private const string BrowserPathKey = "browser_path"; private const string DriverPathKey = "driver_path"; @@ -55,7 +56,7 @@ public DriverFinder(DriverOptions options) /// public string GetBrowserPath() { - return BinaryPaths().BrowserPath; + return BinaryPaths()[SeleniumManager.BrowserPath]; } /// @@ -66,7 +67,7 @@ public string GetBrowserPath() /// public string GetDriverPath() { - return BinaryPaths().DriverPath; + return BinaryPaths()[SeleniumManager.DriverPath]; } /// @@ -103,16 +104,16 @@ public bool TryGetBrowserPath([NotNullWhen(true)] out string? browserPath) /// A Dictionary with the validated browser and driver path. /// /// If one of the paths does not exist. - private SeleniumManagerPaths BinaryPaths() + private Dictionary BinaryPaths() { - if (paths is not null && !string.IsNullOrWhiteSpace(paths.DriverPath)) + if (paths is not null && !string.IsNullOrWhiteSpace(paths[SeleniumManager.DriverPath])) { return paths; } - SeleniumManagerPaths binaryPaths = SeleniumManager.BinaryPaths(CreateArguments()); - string driverPath = binaryPaths.DriverPath; - string browserPath = binaryPaths.BrowserPath; + Dictionary binaryPaths = SeleniumManager.BinaryPaths(CreateArguments()); + string driverPath = binaryPaths[SeleniumManager.DriverPath]; + string browserPath = binaryPaths[SeleniumManager.BrowserPath]; if (File.Exists(driverPath)) { diff --git a/dotnet/src/webdriver/SeleniumManager.cs b/dotnet/src/webdriver/SeleniumManager.cs index b7bba8eac52e8..dc899c886c257 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 DriverPath = "driver_path"; + internal const string BrowserPath = "browser_path"; + private static readonly ILogger _logger = Log.GetLogger(typeof(SeleniumManager)); private static readonly Lazy _lazyBinaryFullPath = new(() => @@ -80,7 +83,7 @@ public static class SeleniumManager /// /// An array with two entries, one for the driver path, and another one for the browser path. /// - public static SeleniumManagerPaths BinaryPaths(string arguments) + public static Dictionary BinaryPaths(string arguments) { StringBuilder argsBuilder = new StringBuilder(arguments); argsBuilder.Append(" --language-binding csharp"); @@ -90,12 +93,17 @@ public static SeleniumManagerPaths BinaryPaths(string arguments) argsBuilder.Append(" --debug"); } - var binaryPaths = RunCommand(_lazyBinaryFullPath.Value, argsBuilder.ToString()); + var smCommandResult = RunCommand(_lazyBinaryFullPath.Value, argsBuilder.ToString()); + Dictionary binaryPaths = new() + { + { BrowserPath, smCommandResult.BrowserPath }, + { DriverPath, smCommandResult.DriverPath } + }; if (_logger.IsEnabled(LogEventLevel.Trace)) { - _logger.Trace($"Driver path: {binaryPaths.DriverPath}"); - _logger.Trace($"Browser path: {binaryPaths.BrowserPath}"); + _logger.Trace($"Driver path: {binaryPaths[DriverPath]}"); + _logger.Trace($"Browser path: {binaryPaths[BrowserPath]}"); } return binaryPaths; @@ -109,7 +117,7 @@ public static SeleniumManagerPaths BinaryPaths(string arguments) /// /// the standard output of the execution. /// - private static SeleniumManagerPaths RunCommand(string fileName, string arguments) + private static ResultResponse RunCommand(string fileName, string arguments) { Process process = new Process(); process.StartInfo.FileName = _lazyBinaryFullPath.Value; @@ -203,23 +211,19 @@ private static SeleniumManagerPaths RunCommand(string fileName, string arguments } } - internal sealed record SeleniumManagerResponse(IReadOnlyList Logs, SeleniumManagerPaths Result) + internal sealed record SeleniumManagerResponse(IReadOnlyList Logs, ResultResponse Result) { public sealed record LogEntryResponse(string Level, string Message); - } - /// - /// Response from Selenium Manager about the paths and their locations. - /// - /// The path to the driver binary. - /// The path to the browser binary, or if none exists. - public sealed record SeleniumManagerPaths - ( - [property: JsonPropertyName("driver_path")] + internal sealed record ResultResponse + ( + [property: JsonPropertyName("driver_path")] string DriverPath, - [property: JsonPropertyName("browser_path")] + [property: JsonPropertyName("browser_path")] string BrowserPath - ); + ); + } + [JsonSerializable(typeof(SeleniumManagerResponse))] [JsonSourceGenerationOptions(PropertyNameCaseInsensitive = true)] From defff98356e946be0d77af90ee6f2f7dd3e7b8fe Mon Sep 17 00:00:00 2001 From: Michael Render Date: Fri, 28 Feb 2025 15:49:02 -0500 Subject: [PATCH 10/13] Use more consistent consts --- dotnet/src/webdriver/DriverFinder.cs | 2 -- dotnet/src/webdriver/SeleniumManager.cs | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/dotnet/src/webdriver/DriverFinder.cs b/dotnet/src/webdriver/DriverFinder.cs index 5e99b29987d31..9f1570f252bb3 100644 --- a/dotnet/src/webdriver/DriverFinder.cs +++ b/dotnet/src/webdriver/DriverFinder.cs @@ -36,8 +36,6 @@ public class DriverFinder { private readonly DriverOptions options; private Dictionary? paths; - private const string BrowserPathKey = "browser_path"; - private const string DriverPathKey = "driver_path"; /// /// Initializes a new instance of the class. diff --git a/dotnet/src/webdriver/SeleniumManager.cs b/dotnet/src/webdriver/SeleniumManager.cs index dc899c886c257..6cff6fb4ec97b 100644 --- a/dotnet/src/webdriver/SeleniumManager.cs +++ b/dotnet/src/webdriver/SeleniumManager.cs @@ -217,9 +217,9 @@ public sealed record LogEntryResponse(string Level, string Message); internal sealed record ResultResponse ( - [property: JsonPropertyName("driver_path")] + [property: JsonPropertyName(SeleniumManager.DriverPath)] string DriverPath, - [property: JsonPropertyName("browser_path")] + [property: JsonPropertyName(SeleniumManager.BrowserPath)] string BrowserPath ); } From ac2e3a5ce68f716d7614f2dd3bda1e77ac625b7a Mon Sep 17 00:00:00 2001 From: Michael Render Date: Fri, 28 Feb 2025 15:49:48 -0500 Subject: [PATCH 11/13] Make name the same --- dotnet/src/webdriver/DriverFinder.cs | 10 +++++----- dotnet/src/webdriver/SeleniumManager.cs | 16 ++++++++-------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/dotnet/src/webdriver/DriverFinder.cs b/dotnet/src/webdriver/DriverFinder.cs index 9f1570f252bb3..3e9b194e62a2e 100644 --- a/dotnet/src/webdriver/DriverFinder.cs +++ b/dotnet/src/webdriver/DriverFinder.cs @@ -54,7 +54,7 @@ public DriverFinder(DriverOptions options) /// public string GetBrowserPath() { - return BinaryPaths()[SeleniumManager.BrowserPath]; + return BinaryPaths()[SeleniumManager.BrowserPathKey]; } /// @@ -65,7 +65,7 @@ public string GetBrowserPath() /// public string GetDriverPath() { - return BinaryPaths()[SeleniumManager.DriverPath]; + return BinaryPaths()[SeleniumManager.DriverPathKey]; } /// @@ -104,14 +104,14 @@ public bool TryGetBrowserPath([NotNullWhen(true)] out string? browserPath) /// If one of the paths does not exist. private Dictionary BinaryPaths() { - if (paths is not null && !string.IsNullOrWhiteSpace(paths[SeleniumManager.DriverPath])) + if (paths is not null && !string.IsNullOrWhiteSpace(paths[SeleniumManager.DriverPathKey])) { return paths; } Dictionary binaryPaths = SeleniumManager.BinaryPaths(CreateArguments()); - string driverPath = binaryPaths[SeleniumManager.DriverPath]; - string browserPath = binaryPaths[SeleniumManager.BrowserPath]; + string driverPath = binaryPaths[SeleniumManager.DriverPathKey]; + string browserPath = binaryPaths[SeleniumManager.BrowserPathKey]; if (File.Exists(driverPath)) { diff --git a/dotnet/src/webdriver/SeleniumManager.cs b/dotnet/src/webdriver/SeleniumManager.cs index 6cff6fb4ec97b..8c62e790d2804 100644 --- a/dotnet/src/webdriver/SeleniumManager.cs +++ b/dotnet/src/webdriver/SeleniumManager.cs @@ -38,8 +38,8 @@ namespace OpenQA.Selenium /// public static class SeleniumManager { - internal const string DriverPath = "driver_path"; - internal const string BrowserPath = "browser_path"; + internal const string DriverPathKey = "driver_path"; + internal const string BrowserPathKey = "browser_path"; private static readonly ILogger _logger = Log.GetLogger(typeof(SeleniumManager)); @@ -96,14 +96,14 @@ public static Dictionary BinaryPaths(string arguments) var smCommandResult = RunCommand(_lazyBinaryFullPath.Value, argsBuilder.ToString()); Dictionary binaryPaths = new() { - { BrowserPath, smCommandResult.BrowserPath }, - { DriverPath, smCommandResult.DriverPath } + { BrowserPathKey, smCommandResult.BrowserPath }, + { DriverPathKey, smCommandResult.DriverPath } }; if (_logger.IsEnabled(LogEventLevel.Trace)) { - _logger.Trace($"Driver path: {binaryPaths[DriverPath]}"); - _logger.Trace($"Browser path: {binaryPaths[BrowserPath]}"); + _logger.Trace($"Driver path: {binaryPaths[DriverPathKey]}"); + _logger.Trace($"Browser path: {binaryPaths[BrowserPathKey]}"); } return binaryPaths; @@ -217,9 +217,9 @@ public sealed record LogEntryResponse(string Level, string Message); internal sealed record ResultResponse ( - [property: JsonPropertyName(SeleniumManager.DriverPath)] + [property: JsonPropertyName(SeleniumManager.DriverPathKey)] string DriverPath, - [property: JsonPropertyName(SeleniumManager.BrowserPath)] + [property: JsonPropertyName(SeleniumManager.BrowserPathKey)] string BrowserPath ); } From b349ef476381e7def4c06eb53b4f86b9a8d90d98 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Fri, 28 Feb 2025 15:52:20 -0500 Subject: [PATCH 12/13] Use single path --- dotnet/src/webdriver/DriverFinder.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/dotnet/src/webdriver/DriverFinder.cs b/dotnet/src/webdriver/DriverFinder.cs index 3e9b194e62a2e..901784a51cb29 100644 --- a/dotnet/src/webdriver/DriverFinder.cs +++ b/dotnet/src/webdriver/DriverFinder.cs @@ -35,7 +35,7 @@ namespace OpenQA.Selenium public class DriverFinder { private readonly DriverOptions options; - private Dictionary? paths; + private Dictionary paths = new Dictionary(); /// /// Initializes a new instance of the class. @@ -104,7 +104,7 @@ public bool TryGetBrowserPath([NotNullWhen(true)] out string? browserPath) /// If one of the paths does not exist. private Dictionary BinaryPaths() { - if (paths is not null && !string.IsNullOrWhiteSpace(paths[SeleniumManager.DriverPathKey])) + if (paths.ContainsKey(SeleniumManager.DriverPathKey) && !string.IsNullOrWhiteSpace(paths[SeleniumManager.DriverPathKey])) { return paths; } @@ -115,18 +115,23 @@ private Dictionary BinaryPaths() if (File.Exists(driverPath)) { + paths.Add(SeleniumManager.DriverPathKey, driverPath); } else { throw new NoSuchDriverException($"The driver path is not a valid file: {driverPath}"); } - if (!File.Exists(browserPath)) + if (File.Exists(browserPath)) + { + paths.Add(SeleniumManager.BrowserPathKey, browserPath); + } + else { throw new NoSuchDriverException($"The browser path is not a valid file: {browserPath}"); } - return paths = binaryPaths; + return paths; } /// From 352750bb975a34734ac1ad8d9000dcfdb51ca2aa Mon Sep 17 00:00:00 2001 From: Michael Render Date: Fri, 28 Feb 2025 15:54:04 -0500 Subject: [PATCH 13/13] minimize more diffs --- dotnet/src/webdriver/SeleniumManager.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dotnet/src/webdriver/SeleniumManager.cs b/dotnet/src/webdriver/SeleniumManager.cs index 8c62e790d2804..324cfa5b34957 100644 --- a/dotnet/src/webdriver/SeleniumManager.cs +++ b/dotnet/src/webdriver/SeleniumManager.cs @@ -215,7 +215,7 @@ internal sealed record SeleniumManagerResponse(IReadOnlyList L { public sealed record LogEntryResponse(string Level, string Message); - internal sealed record ResultResponse + public sealed record ResultResponse ( [property: JsonPropertyName(SeleniumManager.DriverPathKey)] string DriverPath, @@ -224,7 +224,6 @@ string BrowserPath ); } - [JsonSerializable(typeof(SeleniumManagerResponse))] [JsonSourceGenerationOptions(PropertyNameCaseInsensitive = true)] internal sealed partial class SeleniumManagerSerializerContext : JsonSerializerContext;