Skip to content

Commit 298497d

Browse files
RenderMichaelgryznar
authored andcommitted
[dotnet] Annotate nullability on FirefoxDriverService (SeleniumHQ#15220)
* [dotnet] Annotate nullability on `FirefoxDriverService` * Replace null suppress with exception * Bring back null suppression
1 parent 1f99a66 commit 298497d

File tree

1 file changed

+29
-59
lines changed

1 file changed

+29
-59
lines changed

dotnet/src/webdriver/Firefox/FirefoxDriverService.cs

Lines changed: 29 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
using System.IO;
2424
using System.Text;
2525

26+
#nullable enable
27+
2628
namespace OpenQA.Selenium.Firefox
2729
{
2830
/// <summary>
@@ -32,21 +34,13 @@ public sealed class FirefoxDriverService : DriverService
3234
{
3335
private const string DefaultFirefoxDriverServiceFileName = "geckodriver";
3436

35-
private bool connectToRunningBrowser;
36-
private bool openBrowserToolbox;
37-
private int browserCommunicationPort = -1;
38-
private string browserBinaryPath = string.Empty;
39-
private string host = string.Empty;
40-
private string browserCommunicationHost = string.Empty;
41-
private FirefoxDriverLogLevel loggingLevel = FirefoxDriverLogLevel.Default;
42-
4337
/// <summary>
4438
/// Initializes a new instance of the <see cref="FirefoxDriverService"/> class.
4539
/// </summary>
4640
/// <param name="executablePath">The full path to the Firefox driver executable.</param>
4741
/// <param name="executableFileName">The file name of the Firefox driver executable.</param>
4842
/// <param name="port">The port on which the Firefox driver executable should listen.</param>
49-
private FirefoxDriverService(string executablePath, string executableFileName, int port)
43+
private FirefoxDriverService(string? executablePath, string? executableFileName, int port)
5044
: base(executablePath, port, executableFileName)
5145
{
5246
}
@@ -60,60 +54,40 @@ protected override DriverOptions GetDefaultDriverOptions()
6054
/// <summary>
6155
/// Gets or sets the location of the Firefox binary executable.
6256
/// </summary>
63-
public string FirefoxBinaryPath
64-
{
65-
get { return this.browserBinaryPath; }
66-
set { this.browserBinaryPath = value; }
67-
}
57+
/// <remarks> A <see langword="null"/> or <see cref="string.Empty"/> value indicates no binary executable path to specify.</remarks>
58+
public string? FirefoxBinaryPath { get; set; }
6859

6960
/// <summary>
7061
/// Gets or sets the port used by the driver executable to communicate with the browser.
7162
/// </summary>
72-
public int BrowserCommunicationPort
73-
{
74-
get { return this.browserCommunicationPort; }
75-
set { this.browserCommunicationPort = value; }
76-
}
63+
/// <remarks>A negative or zero value indicates no port value to specify.</remarks>
64+
public int BrowserCommunicationPort { get; set; } = -1;
7765

7866
/// <summary>
7967
/// Gets or sets the value of the IP address of the host adapter used by the driver
8068
/// executable to communicate with the browser.
8169
/// </summary>
82-
public string BrowserCommunicationHost
83-
{
84-
get { return this.browserCommunicationHost; }
85-
set { this.browserCommunicationHost = value; }
86-
}
70+
/// <remarks> A <see langword="null"/> or <see cref="string.Empty"/> value indicates no marionette host adapter to specify.</remarks>
71+
public string? BrowserCommunicationHost { get; set; }
8772

8873
/// <summary>
8974
/// Gets or sets the value of the IP address of the host adapter on which the
9075
/// service should listen for connections.
9176
/// </summary>
92-
public string Host
93-
{
94-
get { return this.host; }
95-
set { this.host = value; }
96-
}
77+
/// <remarks> A <see langword="null"/> or <see cref="string.Empty"/> value indicates no host to specify.</remarks>
78+
public string? Host { get; set; }
9779

9880
/// <summary>
9981
/// Gets or sets a value indicating whether to connect to an already-running
10082
/// instance of Firefox.
10183
/// </summary>
102-
public bool ConnectToRunningBrowser
103-
{
104-
get { return this.connectToRunningBrowser; }
105-
set { this.connectToRunningBrowser = value; }
106-
}
84+
public bool ConnectToRunningBrowser { get; set; }
10785

10886
/// <summary>
10987
/// Gets or sets a value indicating whether to open the Firefox Browser Toolbox
11088
/// when Firefox is launched.
11189
/// </summary>
112-
public bool OpenBrowserToolbox
113-
{
114-
get { return this.openBrowserToolbox; }
115-
set { this.openBrowserToolbox = value; }
116-
}
90+
public bool OpenBrowserToolbox { get; set; }
11791

11892
/// <summary>
11993
/// Gets or sets the level at which log output is displayed.
@@ -124,11 +98,7 @@ public bool OpenBrowserToolbox
12498
/// when the browser is launched, meaning that initial driver logging before
12599
/// initiation of a session can be controlled.
126100
/// </remarks>
127-
public FirefoxDriverLogLevel LogLevel
128-
{
129-
get { return this.loggingLevel; }
130-
set { this.loggingLevel = value; }
131-
}
101+
public FirefoxDriverLogLevel LogLevel { get; set; } = FirefoxDriverLogLevel.Default;
132102

133103
/// <summary>
134104
/// Gets a value indicating the time to wait for the service to terminate before forcing it to terminate.
@@ -139,7 +109,7 @@ protected override TimeSpan TerminationTimeout
139109
// because the executable does not have a clean shutdown command,
140110
// which means we have to kill the process. Using a short timeout
141111
// gets us to the termination point much faster.
142-
get { return TimeSpan.FromMilliseconds(100); }
112+
get => TimeSpan.FromMilliseconds(100);
143113
}
144114

145115
/// <summary>
@@ -150,7 +120,7 @@ protected override bool HasShutdown
150120
{
151121
// The Firefox driver executable does not have a clean shutdown command,
152122
// which means we have to kill the process.
153-
get { return false; }
123+
get => false;
154124
}
155125

156126
/// <summary>
@@ -161,7 +131,7 @@ protected override string CommandLineArguments
161131
get
162132
{
163133
StringBuilder argsBuilder = new StringBuilder();
164-
if (this.connectToRunningBrowser)
134+
if (this.ConnectToRunningBrowser)
165135
{
166136
argsBuilder.Append(" --connect-existing");
167137
}
@@ -170,37 +140,37 @@ protected override string CommandLineArguments
170140
argsBuilder.Append(string.Format(CultureInfo.InvariantCulture, " --websocket-port {0}", PortUtilities.FindFreePort()));
171141
}
172142

173-
if (this.browserCommunicationPort > 0)
143+
if (this.BrowserCommunicationPort > 0)
174144
{
175-
argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --marionette-port {0}", this.browserCommunicationPort);
145+
argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --marionette-port {0}", this.BrowserCommunicationPort);
176146
}
177147

178-
if (!string.IsNullOrEmpty(this.browserCommunicationHost))
148+
if (!string.IsNullOrEmpty(this.BrowserCommunicationHost))
179149
{
180-
argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --marionette-host \"{0}\"", this.browserCommunicationHost);
150+
argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --marionette-host \"{0}\"", this.BrowserCommunicationHost);
181151
}
182152

183153
if (this.Port > 0)
184154
{
185155
argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --port {0}", this.Port);
186156
}
187157

188-
if (!string.IsNullOrEmpty(this.browserBinaryPath))
158+
if (!string.IsNullOrEmpty(this.FirefoxBinaryPath))
189159
{
190-
argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --binary \"{0}\"", this.browserBinaryPath);
160+
argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --binary \"{0}\"", this.FirefoxBinaryPath);
191161
}
192162

193-
if (!string.IsNullOrEmpty(this.host))
163+
if (!string.IsNullOrEmpty(this.Host))
194164
{
195-
argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --host \"{0}\"", this.host);
165+
argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --host \"{0}\"", this.Host);
196166
}
197167

198-
if (this.loggingLevel != FirefoxDriverLogLevel.Default)
168+
if (this.LogLevel != FirefoxDriverLogLevel.Default)
199169
{
200-
argsBuilder.Append(string.Format(CultureInfo.InvariantCulture, " --log {0}", this.loggingLevel.ToString().ToLowerInvariant()));
170+
argsBuilder.Append(string.Format(CultureInfo.InvariantCulture, " --log {0}", this.LogLevel.ToString().ToLowerInvariant()));
201171
}
202172

203-
if (this.openBrowserToolbox)
173+
if (this.OpenBrowserToolbox)
204174
{
205175
argsBuilder.Append(" --jsdebugger");
206176
}
@@ -230,7 +200,7 @@ public static FirefoxDriverService CreateDefaultService(string driverPath)
230200
if (File.Exists(driverPath))
231201
{
232202
fileName = Path.GetFileName(driverPath);
233-
driverPath = Path.GetDirectoryName(driverPath);
203+
driverPath = Path.GetDirectoryName(driverPath)!;
234204
}
235205
else
236206
{

0 commit comments

Comments
 (0)