Skip to content

Commit 76b9cc4

Browse files
committed
[dotnet] Annotate nullability on firefox and chromium options
1 parent b0ee450 commit 76b9cc4

File tree

3 files changed

+59
-95
lines changed

3 files changed

+59
-95
lines changed

dotnet/src/webdriver/Chrome/ChromeOptions.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
using System;
2222
using System.Globalization;
2323

24+
#nullable enable
25+
2426
namespace OpenQA.Selenium.Chrome
2527
{
2628
/// <summary>
@@ -64,19 +66,13 @@ public ChromeOptions() : base()
6466
/// <summary>
6567
/// Gets the vendor prefix to apply to Chromium-specific capability names.
6668
/// </summary>
67-
protected override string VendorPrefix
68-
{
69-
get { return "goog"; }
70-
}
69+
protected override string VendorPrefix => "goog";
7170

7271
/// <summary>
7372
/// Gets the name of the capability used to store Chromium options in
7473
/// an <see cref="ICapabilities"/> object.
7574
/// </summary>
76-
public override string CapabilityName
77-
{
78-
get { return string.Format(CultureInfo.InvariantCulture, "{0}:{1}", this.VendorPrefix, ChromeOptionsCapabilityName); }
79-
}
75+
public override string CapabilityName => string.Format(CultureInfo.InvariantCulture, "{0}:{1}", this.VendorPrefix, ChromeOptionsCapabilityName);
8076

8177
/// <summary>
8278
/// Provides a means to add additional capabilities not yet added as type safe options
@@ -92,7 +88,7 @@ public override string CapabilityName
9288
/// where <paramref name="optionName"/> has already been added will overwrite the
9389
/// existing value with the new value in <paramref name="optionValue"/>.
9490
/// Calling this method adds capabilities to the Chrome-specific options object passed to
95-
/// webdriver executable (property name 'goog:chromeOptions').</remarks>
91+
/// WebDriver executable (property name 'goog:chromeOptions').</remarks>
9692
public void AddAdditionalChromeOption(string optionName, object optionValue)
9793
{
9894
this.AddAdditionalChromiumOption(optionName, optionValue);

dotnet/src/webdriver/Edge/EdgeOptions.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
using System;
2222
using System.Globalization;
2323

24+
#nullable enable
25+
2426
namespace OpenQA.Selenium.Edge
2527
{
2628
/// <summary>
@@ -60,27 +62,21 @@ public EdgeOptions() : base()
6062
/// <summary>
6163
/// Gets the vendor prefix to apply to Chromium-specific capability names.
6264
/// </summary>
63-
protected override string VendorPrefix
64-
{
65-
get { return "ms"; }
66-
}
65+
protected override string VendorPrefix => "ms";
6766

6867
/// <summary>
6968
/// Gets the name of the capability used to store Chromium options in
7069
/// an <see cref="ICapabilities"/> object.
7170
/// </summary>
72-
public override string CapabilityName
73-
{
74-
get { return string.Format(CultureInfo.InvariantCulture, "{0}:{1}", this.VendorPrefix, EdgeOptionsCapabilityName); }
75-
}
71+
public override string CapabilityName => string.Format(CultureInfo.InvariantCulture, "{0}:{1}", this.VendorPrefix, EdgeOptionsCapabilityName);
7672

7773
/// <summary>
7874
/// Gets or sets whether to create a WebView session used for launching an Edge (Chromium) WebView-based app on desktop.
7975
/// </summary>
8076
public bool UseWebView
8177
{
82-
get { return this.BrowserName == WebViewBrowserNameValue; }
83-
set { this.BrowserName = value ? WebViewBrowserNameValue : DefaultBrowserNameValue; }
78+
get => this.BrowserName == WebViewBrowserNameValue;
79+
set => this.BrowserName = value ? WebViewBrowserNameValue : DefaultBrowserNameValue;
8480
}
8581

8682
/// <summary>
@@ -97,7 +93,7 @@ public bool UseWebView
9793
/// where <paramref name="optionName"/> has already been added will overwrite the
9894
/// existing value with the new value in <paramref name="optionValue"/>.
9995
/// Calling this method adds capabilities to the Edge-specific options object passed to
100-
/// webdriver executable (property name 'ms:edgeOptions').</remarks>
96+
/// WebDriver executable (property name 'ms:edgeOptions').</remarks>
10197
public void AddAdditionalEdgeOption(string optionName, object optionValue)
10298
{
10399
this.AddAdditionalChromiumOption(optionName, optionValue);

dotnet/src/webdriver/Firefox/FirefoxOptions.cs

Lines changed: 47 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
using System;
2121
using System.Collections.Generic;
2222

23+
#nullable enable
24+
2325
namespace OpenQA.Selenium.Firefox
2426
{
2527
/// <summary>
@@ -60,16 +62,10 @@ public class FirefoxOptions : DriverOptions
6062
private const string FirefoxEnvCapability = "env";
6163
private const string FirefoxOptionsCapability = "moz:firefoxOptions";
6264
private const string FirefoxEnableDevToolsProtocolCapability = "moz:debuggerAddress";
63-
64-
private bool enableDevToolsProtocol;
65-
private string binaryLocation;
66-
private FirefoxDriverLogLevel logLevel = FirefoxDriverLogLevel.Default;
67-
private FirefoxProfile profile;
68-
private List<string> firefoxArguments = new List<string>();
69-
private Dictionary<string, object> profilePreferences = new Dictionary<string, object>();
70-
private Dictionary<string, object> additionalFirefoxOptions = new Dictionary<string, object>();
71-
private Dictionary<string, object> environmentVariables = new Dictionary<string, object>();
72-
private FirefoxAndroidOptions androidOptions;
65+
private readonly List<string> firefoxArguments = new List<string>();
66+
private readonly Dictionary<string, object> profilePreferences = new Dictionary<string, object>();
67+
private readonly Dictionary<string, object> additionalFirefoxOptions = new Dictionary<string, object>();
68+
private readonly Dictionary<string, object> environmentVariables = new Dictionary<string, object>();
7369

7470
/// <summary>
7571
/// Initializes a new instance of the <see cref="FirefoxOptions"/> class.
@@ -97,63 +93,44 @@ public FirefoxOptions()
9793
/// <summary>
9894
/// Gets or sets the <see cref="FirefoxProfile"/> object to be used with this instance.
9995
/// </summary>
100-
public FirefoxProfile Profile
101-
{
102-
get { return this.profile; }
103-
set { this.profile = value; }
104-
}
96+
public FirefoxProfile? Profile { get; set; }
10597

10698
/// <summary>
10799
/// Gets or sets the path and file name of the Firefox browser executable.
108100
/// </summary>
109-
public override string BinaryLocation
110-
{
111-
get { return this.binaryLocation; }
112-
set { this.binaryLocation = value; }
113-
}
101+
public override string? BinaryLocation { get; set; }
114102

115103
/// <summary>
116104
/// Gets or sets the path and file name of the Firefox browser executable.
117105
/// </summary>
118106
[Obsolete("Use BinaryLocation property instead of BrowserExecutableLocation. This one will be removed soon.")]
119-
public string BrowserExecutableLocation
107+
public string? BrowserExecutableLocation
120108
{
121-
get { return this.binaryLocation; }
122-
set { this.binaryLocation = value; }
109+
get => this.BinaryLocation;
110+
set => this.BinaryLocation = value;
123111
}
124112

125113
/// <summary>
126114
/// Gets or sets the logging level of the Firefox driver.
127115
/// </summary>
128-
public FirefoxDriverLogLevel LogLevel
129-
{
130-
get { return this.logLevel; }
131-
set { this.logLevel = value; }
132-
}
116+
public FirefoxDriverLogLevel LogLevel { get; set; } = FirefoxDriverLogLevel.Default;
133117

134118
/// <summary>
135119
/// Gets or sets a value indicating whether to enable the DevTools protocol for the launched browser.
136120
/// </summary>
137-
public bool EnableDevToolsProtocol
138-
{
139-
get { return this.enableDevToolsProtocol; }
140-
set { this.enableDevToolsProtocol = value; }
141-
}
121+
public bool EnableDevToolsProtocol { get; set; }
142122

143123
/// <summary>
144124
/// Gets or sets the options for automating Firefox on Android.
145125
/// </summary>
146-
public FirefoxAndroidOptions AndroidOptions
147-
{
148-
get { return this.androidOptions; }
149-
set { this.androidOptions = value; }
150-
}
126+
public FirefoxAndroidOptions? AndroidOptions { get; set; }
151127

152128
/// <summary>
153129
/// Adds an argument to be used in launching the Firefox browser.
154130
/// </summary>
155131
/// <param name="argumentName">The argument to add.</param>
156-
/// <remarks>Arguments must be preceeded by two dashes ("--").</remarks>
132+
/// <remarks>Arguments must be preceded by two dashes ("--").</remarks>
133+
/// <exception cref="ArgumentException">If <paramref name="argumentName"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
157134
public void AddArgument(string argumentName)
158135
{
159136
if (string.IsNullOrEmpty(argumentName))
@@ -168,16 +145,18 @@ public void AddArgument(string argumentName)
168145
/// Adds a list arguments to be used in launching the Firefox browser.
169146
/// </summary>
170147
/// <param name="argumentsToAdd">An array of arguments to add.</param>
171-
/// <remarks>Each argument must be preceeded by two dashes ("--").</remarks>
148+
/// <remarks>Each argument must be preceded by two dashes ("--").</remarks>
149+
/// <exception cref="ArgumentNullException">If <paramref name="argumentsToAdd"/> is <see langword="null"/>.</exception>
172150
public void AddArguments(params string[] argumentsToAdd)
173151
{
174-
this.AddArguments(new List<string>(argumentsToAdd));
152+
this.AddArguments((IEnumerable<string>)argumentsToAdd);
175153
}
176154

177155
/// <summary>
178156
/// Adds a list arguments to be used in launching the Firefox browser.
179157
/// </summary>
180158
/// <param name="argumentsToAdd">An array of arguments to add.</param>
159+
/// <exception cref="ArgumentNullException">If <paramref name="argumentsToAdd"/> is <see langword="null"/>.</exception>
181160
public void AddArguments(IEnumerable<string> argumentsToAdd)
182161
{
183162
if (argumentsToAdd == null)
@@ -193,6 +172,7 @@ public void AddArguments(IEnumerable<string> argumentsToAdd)
193172
/// </summary>
194173
/// <param name="preferenceName">Name of the preference to set.</param>
195174
/// <param name="preferenceValue">Value of the preference to set.</param>
175+
/// <exception cref="ArgumentException">If <paramref name="preferenceName"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
196176
public void SetPreference(string preferenceName, bool preferenceValue)
197177
{
198178
this.SetPreferenceValue(preferenceName, preferenceValue);
@@ -203,6 +183,7 @@ public void SetPreference(string preferenceName, bool preferenceValue)
203183
/// </summary>
204184
/// <param name="preferenceName">Name of the preference to set.</param>
205185
/// <param name="preferenceValue">Value of the preference to set.</param>
186+
/// <exception cref="ArgumentException">If <paramref name="preferenceName"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
206187
public void SetPreference(string preferenceName, int preferenceValue)
207188
{
208189
this.SetPreferenceValue(preferenceName, preferenceValue);
@@ -213,6 +194,7 @@ public void SetPreference(string preferenceName, int preferenceValue)
213194
/// </summary>
214195
/// <param name="preferenceName">Name of the preference to set.</param>
215196
/// <param name="preferenceValue">Value of the preference to set.</param>
197+
/// <exception cref="ArgumentException">If <paramref name="preferenceName"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
216198
public void SetPreference(string preferenceName, long preferenceValue)
217199
{
218200
this.SetPreferenceValue(preferenceName, preferenceValue);
@@ -223,6 +205,7 @@ public void SetPreference(string preferenceName, long preferenceValue)
223205
/// </summary>
224206
/// <param name="preferenceName">Name of the preference to set.</param>
225207
/// <param name="preferenceValue">Value of the preference to set.</param>
208+
/// <exception cref="ArgumentException">If <paramref name="preferenceName"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
226209
public void SetPreference(string preferenceName, double preferenceValue)
227210
{
228211
this.SetPreferenceValue(preferenceName, preferenceValue);
@@ -233,29 +216,26 @@ public void SetPreference(string preferenceName, double preferenceValue)
233216
/// </summary>
234217
/// <param name="preferenceName">Name of the preference to set.</param>
235218
/// <param name="preferenceValue">Value of the preference to set.</param>
219+
/// <exception cref="ArgumentException">If <paramref name="preferenceName"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
236220
public void SetPreference(string preferenceName, string preferenceValue)
237221
{
238222
this.SetPreferenceValue(preferenceName, preferenceValue);
239223
}
240224

241225
/// <summary>
242-
/// Sets an environment variable to be set in the operating system's environment under which the Firerox browser is launched.
226+
/// Sets an environment variable to be set in the operating system's environment under which the Firefox browser is launched.
243227
/// </summary>
244228
/// <param name="variableName">The name of the environment variable.</param>
245229
/// <param name="variableValue">The value of the environment variable.</param>
246-
public void SetEnvironmentVariable(string variableName, string variableValue)
230+
/// <exception cref="ArgumentException">If <paramref name="variableName"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
231+
public void SetEnvironmentVariable(string variableName, string? variableValue)
247232
{
248233
if (string.IsNullOrEmpty(variableName))
249234
{
250235
throw new ArgumentException("Environment variable name cannot be null or an empty string");
251236
}
252237

253-
if (variableValue == null)
254-
{
255-
variableValue = string.Empty;
256-
}
257-
258-
this.environmentVariables[variableName] = variableValue;
238+
this.environmentVariables[variableName] = variableValue ?? string.Empty;
259239
}
260240

261241
/// <summary>
@@ -290,7 +270,7 @@ public override ICapabilities ToCapabilities()
290270
IWritableCapabilities capabilities = GenerateDesiredCapabilities(true);
291271
Dictionary<string, object> firefoxOptions = this.GenerateFirefoxOptionsDictionary();
292272
capabilities.SetCapability(FirefoxOptionsCapability, firefoxOptions);
293-
if (this.enableDevToolsProtocol)
273+
if (this.EnableDevToolsProtocol)
294274
{
295275
capabilities.SetCapability(FirefoxEnableDevToolsProtocolCapability, true);
296276
}
@@ -302,30 +282,26 @@ private Dictionary<string, object> GenerateFirefoxOptionsDictionary()
302282
{
303283
Dictionary<string, object> firefoxOptions = new Dictionary<string, object>();
304284

305-
if (this.profile != null)
285+
if (this.Profile != null)
306286
{
307-
firefoxOptions[FirefoxProfileCapability] = this.profile.ToBase64String();
287+
firefoxOptions[FirefoxProfileCapability] = this.Profile.ToBase64String();
308288
}
309289

310-
if (!string.IsNullOrEmpty(this.binaryLocation))
290+
if (!string.IsNullOrEmpty(this.BinaryLocation))
311291
{
312-
firefoxOptions[FirefoxBinaryCapability] = this.binaryLocation;
292+
firefoxOptions[FirefoxBinaryCapability] = this.BinaryLocation;
313293
}
314294

315-
if (this.logLevel != FirefoxDriverLogLevel.Default)
295+
if (this.LogLevel != FirefoxDriverLogLevel.Default)
316296
{
317297
Dictionary<string, object> logObject = new Dictionary<string, object>();
318-
logObject["level"] = this.logLevel.ToString().ToLowerInvariant();
298+
logObject["level"] = this.LogLevel.ToString().ToLowerInvariant();
319299
firefoxOptions[FirefoxLogCapability] = logObject;
320300
}
321301

322302
if (this.firefoxArguments.Count > 0)
323303
{
324-
List<object> args = new List<object>();
325-
foreach (string argument in this.firefoxArguments)
326-
{
327-
args.Add(argument);
328-
}
304+
List<object> args = [.. this.firefoxArguments];
329305

330306
firefoxOptions[FirefoxArgumentsCapability] = args;
331307
}
@@ -340,9 +316,9 @@ private Dictionary<string, object> GenerateFirefoxOptionsDictionary()
340316
firefoxOptions[FirefoxEnvCapability] = this.environmentVariables;
341317
}
342318

343-
if (this.androidOptions != null)
319+
if (this.AndroidOptions != null)
344320
{
345-
this.AddAndroidOptions(firefoxOptions);
321+
AddAndroidOptions(this.AndroidOptions, firefoxOptions);
346322
}
347323

348324
foreach (KeyValuePair<string, object> pair in this.additionalFirefoxOptions)
@@ -363,27 +339,23 @@ private void SetPreferenceValue(string preferenceName, object preferenceValue)
363339
this.profilePreferences[preferenceName] = preferenceValue;
364340
}
365341

366-
private void AddAndroidOptions(Dictionary<string, object> firefoxOptions)
342+
private static void AddAndroidOptions(FirefoxAndroidOptions androidOptions, Dictionary<string, object> firefoxOptions)
367343
{
368-
firefoxOptions["androidPackage"] = this.androidOptions.AndroidPackage;
344+
firefoxOptions["androidPackage"] = androidOptions.AndroidPackage;
369345

370-
if (!string.IsNullOrEmpty(this.androidOptions.AndroidDeviceSerial))
346+
if (!string.IsNullOrEmpty(androidOptions.AndroidDeviceSerial))
371347
{
372-
firefoxOptions["androidDeviceSerial"] = this.androidOptions.AndroidDeviceSerial;
348+
firefoxOptions["androidDeviceSerial"] = androidOptions.AndroidDeviceSerial;
373349
}
374350

375-
if (!string.IsNullOrEmpty(this.androidOptions.AndroidActivity))
351+
if (!string.IsNullOrEmpty(androidOptions.AndroidActivity))
376352
{
377-
firefoxOptions["androidActivity"] = this.androidOptions.AndroidActivity;
353+
firefoxOptions["androidActivity"] = androidOptions.AndroidActivity;
378354
}
379355

380-
if (this.androidOptions.AndroidIntentArguments.Count > 0)
356+
if (androidOptions.AndroidIntentArguments.Count > 0)
381357
{
382-
List<object> args = new List<object>();
383-
foreach (string argument in this.androidOptions.AndroidIntentArguments)
384-
{
385-
args.Add(argument);
386-
}
358+
List<object> args = [.. androidOptions.AndroidIntentArguments];
387359

388360
firefoxOptions["androidIntentArguments"] = args;
389361
}

0 commit comments

Comments
 (0)