diff --git a/dotnet/src/webdriver/SeleniumManager.cs b/dotnet/src/webdriver/SeleniumManager.cs index d57b13f278180..e9060a8984e5e 100644 --- a/dotnet/src/webdriver/SeleniumManager.cs +++ b/dotnet/src/webdriver/SeleniumManager.cs @@ -25,6 +25,9 @@ using System.Text; using System.Text.Json; using System.Text.Json.Serialization; +using static OpenQA.Selenium.SeleniumManagerResponse; + +#nullable enable namespace OpenQA.Selenium { @@ -36,27 +39,25 @@ public static class SeleniumManager { private static readonly ILogger _logger = Log.GetLogger(typeof(SeleniumManager)); - private static readonly string BinaryFullPath = Environment.GetEnvironmentVariable("SE_MANAGER_PATH"); - private static readonly JsonSerializerOptions _serializerOptions = new() { PropertyNameCaseInsensitive = true, TypeInfoResolver = SeleniumManagerSerializerContext.Default }; - static SeleniumManager() + private static readonly Lazy _lazyBinaryFullPath = new(() => { - - if (BinaryFullPath == null) + string? binaryFullPath = Environment.GetEnvironmentVariable("SE_MANAGER_PATH"); + if (binaryFullPath == null) { var currentDirectory = AppContext.BaseDirectory; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { - BinaryFullPath = Path.Combine(currentDirectory, "selenium-manager", "windows", "selenium-manager.exe"); + binaryFullPath = Path.Combine(currentDirectory, "selenium-manager", "windows", "selenium-manager.exe"); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { - BinaryFullPath = Path.Combine(currentDirectory, "selenium-manager", "linux", "selenium-manager"); + binaryFullPath = Path.Combine(currentDirectory, "selenium-manager", "linux", "selenium-manager"); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { - BinaryFullPath = Path.Combine(currentDirectory, "selenium-manager", "macos", "selenium-manager"); + binaryFullPath = Path.Combine(currentDirectory, "selenium-manager", "macos", "selenium-manager"); } else { @@ -65,11 +66,13 @@ static SeleniumManager() } } - if (!File.Exists(BinaryFullPath)) + if (!File.Exists(binaryFullPath)) { - throw new WebDriverException($"Unable to locate or obtain Selenium Manager binary at {BinaryFullPath}"); + throw new WebDriverException($"Unable to locate or obtain Selenium Manager binary at {binaryFullPath}"); } - } + + return binaryFullPath; + }); /// /// Determines the location of the browser and driver binaries. @@ -88,7 +91,7 @@ public static Dictionary BinaryPaths(string arguments) argsBuilder.Append(" --debug"); } - var smCommandResult = RunCommand(BinaryFullPath, argsBuilder.ToString()); + var smCommandResult = RunCommand(_lazyBinaryFullPath.Value, argsBuilder.ToString()); Dictionary binaryPaths = new() { { "browser_path", smCommandResult.BrowserPath }, @@ -112,10 +115,10 @@ public static Dictionary BinaryPaths(string arguments) /// /// the standard output of the execution. /// - private static SeleniumManagerResponse.ResultResponse RunCommand(string fileName, string arguments) + private static ResultResponse RunCommand(string fileName, string arguments) { Process process = new Process(); - process.StartInfo.FileName = BinaryFullPath; + process.StartInfo.FileName = _lazyBinaryFullPath.Value; process.StartInfo.Arguments = arguments; process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; @@ -183,7 +186,7 @@ private static SeleniumManagerResponse.ResultResponse RunCommand(string fileName try { - jsonResponse = JsonSerializer.Deserialize(output, _serializerOptions); + jsonResponse = JsonSerializer.Deserialize(output, _serializerOptions)!; } catch (Exception ex) { @@ -222,32 +225,22 @@ private static SeleniumManagerResponse.ResultResponse RunCommand(string fileName } } - internal class SeleniumManagerResponse + internal record SeleniumManagerResponse(IReadOnlyList Logs, ResultResponse Result) { - public IReadOnlyList Logs { get; set; } - - public ResultResponse Result { get; set; } + public record LogEntryResponse(string Level, string Message); - public class LogEntryResponse - { - public string Level { get; set; } - - public string Message { get; set; } - } - - public class ResultResponse + public record ResultResponse { [JsonPropertyName("driver_path")] - public string DriverPath { get; set; } + [JsonRequired] + public string DriverPath { get; init; } = null!; [JsonPropertyName("browser_path")] - public string BrowserPath { get; set; } + [JsonRequired] + public string BrowserPath { get; init; } = null!; } } [JsonSerializable(typeof(SeleniumManagerResponse))] - internal partial class SeleniumManagerSerializerContext : JsonSerializerContext - { - - } + internal partial class SeleniumManagerSerializerContext : JsonSerializerContext; }