2020using System ;
2121using System . Collections . Generic ;
2222
23+ #nullable enable
24+
2325namespace 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