Skip to content

Commit a2c6c41

Browse files
RenderMichaelsandeepsuryaprasad
authored andcommitted
[dotnet] Simplify and nullable annotate DriverFinder (SeleniumHQ#15232)
* [dotnet] Simplify and nullable annotate `DriverFinder` * Minimize diffs * Maintain previous logic * Better local name * Minimize diffs * Minimize diffs further * Better code style * Modernate minimal diffs with code style * Go back to dictionary style * Use more consistent consts * Make name the same * Use single path * minimize more diffs
1 parent 8a37ed2 commit a2c6c41

File tree

4 files changed

+53
-22
lines changed

4 files changed

+53
-22
lines changed

dotnet/src/webdriver/Chromium/ChromiumDriver.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ private static ICommandExecutor GenerateDriverServiceCommandExecutor(DriverServi
168168
string fullServicePath = finder.GetDriverPath();
169169
service.DriverServicePath = Path.GetDirectoryName(fullServicePath);
170170
service.DriverServiceExecutableName = Path.GetFileName(fullServicePath);
171-
if (finder.HasBrowserPath())
171+
if (finder.TryGetBrowserPath(out string browserPath))
172172
{
173-
options.BinaryLocation = finder.GetBrowserPath();
173+
options.BinaryLocation = browserPath;
174174
options.BrowserVersion = null;
175175
}
176176
}

dotnet/src/webdriver/DriverFinder.cs

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919

2020
using System;
2121
using System.Collections.Generic;
22+
using System.Diagnostics.CodeAnalysis;
2223
using System.Globalization;
2324
using System.IO;
2425
using System.Text;
2526

27+
#nullable enable
28+
2629
namespace OpenQA.Selenium
2730
{
2831
/// <summary>
@@ -31,17 +34,16 @@ namespace OpenQA.Selenium
3134
/// </summary>
3235
public class DriverFinder
3336
{
34-
private DriverOptions options;
37+
private readonly DriverOptions options;
3538
private Dictionary<string, string> paths = new Dictionary<string, string>();
36-
private const string BrowserPathKey = "browser_path";
37-
private const string DriverPathKey = "driver_path";
3839

3940
/// <summary>
4041
/// Initializes a new instance of the <see cref="DriverFinder"/> class.
4142
/// </summary>
43+
/// <exception cref="ArgumentNullException">If <paramref name="options"/> is <see langword="null"/>.</exception>
4244
public DriverFinder(DriverOptions options)
4345
{
44-
this.options = options;
46+
this.options = options ?? throw new ArgumentNullException(nameof(options));
4547
}
4648

4749
/// <summary>
@@ -52,7 +54,7 @@ public DriverFinder(DriverOptions options)
5254
/// </returns>
5355
public string GetBrowserPath()
5456
{
55-
return BinaryPaths()[BrowserPathKey];
57+
return BinaryPaths()[SeleniumManager.BrowserPathKey];
5658
}
5759

5860
/// <summary>
@@ -63,14 +65,36 @@ public string GetBrowserPath()
6365
/// </returns>
6466
public string GetDriverPath()
6567
{
66-
return BinaryPaths()[DriverPathKey];
68+
return BinaryPaths()[SeleniumManager.DriverPathKey];
6769
}
6870

71+
/// <summary>
72+
/// Gets whether there is a browser path for the given browser on this platform.
73+
/// </summary>
74+
/// <returns><see langword="true"/> if a browser path exists; otherwise, <see langword="false"/>.</returns>
6975
public bool HasBrowserPath()
7076
{
7177
return !string.IsNullOrWhiteSpace(GetBrowserPath());
7278
}
7379

80+
/// <summary>
81+
/// Tries to get the browser path, as retrieved by Selenium Manager.
82+
/// </summary>
83+
/// <param name="browserPath">If the method returns <see langword="true"/>, the full browser path.</param>
84+
/// <returns><see langword="true"/> if a browser path exists; otherwise, <see langword="false"/>.</returns>
85+
public bool TryGetBrowserPath([NotNullWhen(true)] out string? browserPath)
86+
{
87+
string? path = GetBrowserPath();
88+
if (!string.IsNullOrWhiteSpace(path))
89+
{
90+
browserPath = path;
91+
return true;
92+
}
93+
94+
browserPath = null;
95+
return false;
96+
}
97+
7498
/// <summary>
7599
/// Invokes Selenium Manager to get the binaries paths and validates if they exist.
76100
/// </summary>
@@ -80,29 +104,33 @@ public bool HasBrowserPath()
80104
/// <exception cref="NoSuchDriverException">If one of the paths does not exist.</exception>
81105
private Dictionary<string, string> BinaryPaths()
82106
{
83-
if (paths.ContainsKey(DriverPathKey) && !string.IsNullOrWhiteSpace(paths[DriverPathKey]))
107+
if (paths.ContainsKey(SeleniumManager.DriverPathKey) && !string.IsNullOrWhiteSpace(paths[SeleniumManager.DriverPathKey]))
84108
{
85109
return paths;
86110
}
111+
87112
Dictionary<string, string> binaryPaths = SeleniumManager.BinaryPaths(CreateArguments());
88-
string driverPath = binaryPaths[DriverPathKey];
89-
string browserPath = binaryPaths[BrowserPathKey];
113+
string driverPath = binaryPaths[SeleniumManager.DriverPathKey];
114+
string browserPath = binaryPaths[SeleniumManager.BrowserPathKey];
115+
90116
if (File.Exists(driverPath))
91117
{
92-
paths.Add(DriverPathKey, driverPath);
118+
paths.Add(SeleniumManager.DriverPathKey, driverPath);
93119
}
94120
else
95121
{
96122
throw new NoSuchDriverException($"The driver path is not a valid file: {driverPath}");
97123
}
124+
98125
if (File.Exists(browserPath))
99126
{
100-
paths.Add(BrowserPathKey, browserPath);
127+
paths.Add(SeleniumManager.BrowserPathKey, browserPath);
101128
}
102129
else
103130
{
104131
throw new NoSuchDriverException($"The browser path is not a valid file: {browserPath}");
105132
}
133+
106134
return paths;
107135
}
108136

@@ -123,7 +151,7 @@ private string CreateArguments()
123151
argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --browser-version {0}", options.BrowserVersion);
124152
}
125153

126-
string browserBinary = options.BinaryLocation;
154+
string? browserBinary = options.BinaryLocation;
127155
if (!string.IsNullOrEmpty(browserBinary))
128156
{
129157
argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --browser-path \"{0}\"", browserBinary);

dotnet/src/webdriver/Firefox/FirefoxDriver.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,9 @@ private static ICommandExecutor GenerateDriverServiceCommandExecutor(DriverServi
221221
string fullServicePath = finder.GetDriverPath();
222222
service.DriverServicePath = Path.GetDirectoryName(fullServicePath);
223223
service.DriverServiceExecutableName = Path.GetFileName(fullServicePath);
224-
if (finder.HasBrowserPath())
224+
if (finder.TryGetBrowserPath(out string browserPath))
225225
{
226-
options.BinaryLocation = finder.GetBrowserPath();
226+
options.BinaryLocation = browserPath;
227227
options.BrowserVersion = null;
228228
}
229229
}

dotnet/src/webdriver/SeleniumManager.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ namespace OpenQA.Selenium
3838
/// </summary>
3939
public static class SeleniumManager
4040
{
41+
internal const string DriverPathKey = "driver_path";
42+
internal const string BrowserPathKey = "browser_path";
43+
4144
private static readonly ILogger _logger = Log.GetLogger(typeof(SeleniumManager));
4245

4346
private static readonly Lazy<string> _lazyBinaryFullPath = new(() =>
@@ -93,14 +96,14 @@ public static Dictionary<string, string> BinaryPaths(string arguments)
9396
var smCommandResult = RunCommand(_lazyBinaryFullPath.Value, argsBuilder.ToString());
9497
Dictionary<string, string> binaryPaths = new()
9598
{
96-
{ "browser_path", smCommandResult.BrowserPath },
97-
{ "driver_path", smCommandResult.DriverPath }
99+
{ BrowserPathKey, smCommandResult.BrowserPath },
100+
{ DriverPathKey, smCommandResult.DriverPath }
98101
};
99102

100103
if (_logger.IsEnabled(LogEventLevel.Trace))
101104
{
102-
_logger.Trace($"Driver path: {binaryPaths["driver_path"]}");
103-
_logger.Trace($"Browser path: {binaryPaths["browser_path"]}");
105+
_logger.Trace($"Driver path: {binaryPaths[DriverPathKey]}");
106+
_logger.Trace($"Browser path: {binaryPaths[BrowserPathKey]}");
104107
}
105108

106109
return binaryPaths;
@@ -214,9 +217,9 @@ public sealed record LogEntryResponse(string Level, string Message);
214217

215218
public sealed record ResultResponse
216219
(
217-
[property: JsonPropertyName("driver_path")]
220+
[property: JsonPropertyName(SeleniumManager.DriverPathKey)]
218221
string DriverPath,
219-
[property: JsonPropertyName("browser_path")]
222+
[property: JsonPropertyName(SeleniumManager.BrowserPathKey)]
220223
string BrowserPath
221224
);
222225
}

0 commit comments

Comments
 (0)