"Fossies" - the Fresh Open Source Software Archive 
Member "selenium-selenium-4.8.1/dotnet/src/webdriver/Firefox/FirefoxOptions.cs" (17 Feb 2023, 16104 Bytes) of package /linux/www/selenium-selenium-4.8.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C# source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "FirefoxOptions.cs" see the
Fossies "Dox" file reference documentation.
1 // <copyright file="FirefoxOptions.cs" company="WebDriver Committers">
2 // Licensed to the Software Freedom Conservancy (SFC) under one
3 // or more contributor license agreements. See the NOTICE file
4 // distributed with this work for additional information
5 // regarding copyright ownership. The SFC licenses this file
6 // to you under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 // </copyright>
18
19 using System;
20 using System.Collections.Generic;
21 using System.Globalization;
22 using OpenQA.Selenium.Remote;
23
24 namespace OpenQA.Selenium.Firefox
25 {
26 /// <summary>
27 /// Class to manage options specific to <see cref="FirefoxDriver"/>
28 /// </summary>
29 /// <remarks>
30 /// Used with the marionette executable wires.exe.
31 /// </remarks>
32 /// <example>
33 /// <code>
34 /// FirefoxOptions options = new FirefoxOptions();
35 /// </code>
36 /// <para></para>
37 /// <para>For use with FirefoxDriver:</para>
38 /// <para></para>
39 /// <code>
40 /// FirefoxDriver driver = new FirefoxDriver(options);
41 /// </code>
42 /// <para></para>
43 /// <para>For use with RemoteWebDriver:</para>
44 /// <para></para>
45 /// <code>
46 /// RemoteWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options.ToCapabilities());
47 /// </code>
48 /// </example>
49 public class FirefoxOptions : DriverOptions
50 {
51 private const string BrowserNameValue = "firefox";
52
53 private const string IsMarionetteCapability = "marionette";
54 private const string FirefoxLegacyProfileCapability = "firefox_profile";
55 private const string FirefoxLegacyBinaryCapability = "firefox_binary";
56 private const string FirefoxProfileCapability = "profile";
57 private const string FirefoxBinaryCapability = "binary";
58 private const string FirefoxArgumentsCapability = "args";
59 private const string FirefoxLogCapability = "log";
60 private const string FirefoxPrefsCapability = "prefs";
61 private const string FirefoxEnvCapability = "env";
62 private const string FirefoxOptionsCapability = "moz:firefoxOptions";
63 private const string FirefoxEnableDevToolsProtocolCapability = "moz:debuggerAddress";
64
65 private bool enableDevToolsProtocol;
66 private string browserBinaryLocation;
67 private FirefoxDriverLogLevel logLevel = FirefoxDriverLogLevel.Default;
68 private FirefoxProfile profile;
69 private List<string> firefoxArguments = new List<string>();
70 private Dictionary<string, object> profilePreferences = new Dictionary<string, object>();
71 private Dictionary<string, object> additionalFirefoxOptions = new Dictionary<string, object>();
72 private Dictionary<string, object> environmentVariables = new Dictionary<string, object>();
73 private FirefoxAndroidOptions androidOptions;
74
75 /// <summary>
76 /// Initializes a new instance of the <see cref="FirefoxOptions"/> class.
77 /// </summary>
78 public FirefoxOptions()
79 : base()
80 {
81 this.BrowserName = BrowserNameValue;
82 this.AddKnownCapabilityName(FirefoxOptions.FirefoxOptionsCapability, "current FirefoxOptions class instance");
83 this.AddKnownCapabilityName(FirefoxOptions.IsMarionetteCapability, "UseLegacyImplementation property");
84 this.AddKnownCapabilityName(FirefoxOptions.FirefoxProfileCapability, "Profile property");
85 this.AddKnownCapabilityName(FirefoxOptions.FirefoxBinaryCapability, "BrowserExecutableLocation property");
86 this.AddKnownCapabilityName(FirefoxOptions.FirefoxArgumentsCapability, "AddArguments method");
87 this.AddKnownCapabilityName(FirefoxOptions.FirefoxPrefsCapability, "SetPreference method");
88 this.AddKnownCapabilityName(FirefoxOptions.FirefoxEnvCapability, "SetEnvironmentVariable method");
89 this.AddKnownCapabilityName(FirefoxOptions.FirefoxLogCapability, "LogLevel property");
90 this.AddKnownCapabilityName(FirefoxOptions.FirefoxLegacyProfileCapability, "Profile property");
91 this.AddKnownCapabilityName(FirefoxOptions.FirefoxLegacyBinaryCapability, "BrowserExecutableLocation property");
92 this.AddKnownCapabilityName(FirefoxOptions.FirefoxEnableDevToolsProtocolCapability, "EnableDevToolsProtocol property");
93 }
94
95 /// <summary>
96 /// Gets or sets the <see cref="FirefoxProfile"/> object to be used with this instance.
97 /// </summary>
98 public FirefoxProfile Profile
99 {
100 get { return this.profile; }
101 set { this.profile = value; }
102 }
103
104 /// <summary>
105 /// Gets or sets the path and file name of the Firefox browser executable.
106 /// </summary>
107 public string BrowserExecutableLocation
108 {
109 get { return this.browserBinaryLocation; }
110 set { this.browserBinaryLocation = value; }
111 }
112
113 /// <summary>
114 /// Gets or sets the logging level of the Firefox driver.
115 /// </summary>
116 public FirefoxDriverLogLevel LogLevel
117 {
118 get { return this.logLevel; }
119 set { this.logLevel = value; }
120 }
121
122 public bool EnableDevToolsProtocol
123 {
124 get { return this.enableDevToolsProtocol; }
125 set { this.enableDevToolsProtocol = value; }
126 }
127
128 /// <summary>
129 /// Gets or sets the options for automating Firefox on Android.
130 /// </summary>
131 public FirefoxAndroidOptions AndroidOptions
132 {
133 get { return this.androidOptions; }
134 set { this.androidOptions = value; }
135 }
136
137 /// <summary>
138 /// Adds an argument to be used in launching the Firefox browser.
139 /// </summary>
140 /// <param name="argumentName">The argument to add.</param>
141 /// <remarks>Arguments must be preceeded by two dashes ("--").</remarks>
142 public void AddArgument(string argumentName)
143 {
144 if (string.IsNullOrEmpty(argumentName))
145 {
146 throw new ArgumentException("argumentName must not be null or empty", nameof(argumentName));
147 }
148
149 this.AddArguments(argumentName);
150 }
151
152 /// <summary>
153 /// Adds a list arguments to be used in launching the Firefox browser.
154 /// </summary>
155 /// <param name="argumentsToAdd">An array of arguments to add.</param>
156 /// <remarks>Each argument must be preceeded by two dashes ("--").</remarks>
157 public void AddArguments(params string[] argumentsToAdd)
158 {
159 this.AddArguments(new List<string>(argumentsToAdd));
160 }
161
162 /// <summary>
163 /// Adds a list arguments to be used in launching the Firefox browser.
164 /// </summary>
165 /// <param name="argumentsToAdd">An array of arguments to add.</param>
166 public void AddArguments(IEnumerable<string> argumentsToAdd)
167 {
168 if (argumentsToAdd == null)
169 {
170 throw new ArgumentNullException(nameof(argumentsToAdd), "argumentsToAdd must not be null");
171 }
172
173 this.firefoxArguments.AddRange(argumentsToAdd);
174 }
175
176 /// <summary>
177 /// Sets a preference in the profile used by Firefox.
178 /// </summary>
179 /// <param name="preferenceName">Name of the preference to set.</param>
180 /// <param name="preferenceValue">Value of the preference to set.</param>
181 public void SetPreference(string preferenceName, bool preferenceValue)
182 {
183 this.SetPreferenceValue(preferenceName, preferenceValue);
184 }
185
186 /// <summary>
187 /// Sets a preference in the profile used by Firefox.
188 /// </summary>
189 /// <param name="preferenceName">Name of the preference to set.</param>
190 /// <param name="preferenceValue">Value of the preference to set.</param>
191 public void SetPreference(string preferenceName, int preferenceValue)
192 {
193 this.SetPreferenceValue(preferenceName, preferenceValue);
194 }
195
196 /// <summary>
197 /// Sets a preference in the profile used by Firefox.
198 /// </summary>
199 /// <param name="preferenceName">Name of the preference to set.</param>
200 /// <param name="preferenceValue">Value of the preference to set.</param>
201 public void SetPreference(string preferenceName, long preferenceValue)
202 {
203 this.SetPreferenceValue(preferenceName, preferenceValue);
204 }
205
206 /// <summary>
207 /// Sets a preference in the profile used by Firefox.
208 /// </summary>
209 /// <param name="preferenceName">Name of the preference to set.</param>
210 /// <param name="preferenceValue">Value of the preference to set.</param>
211 public void SetPreference(string preferenceName, double preferenceValue)
212 {
213 this.SetPreferenceValue(preferenceName, preferenceValue);
214 }
215
216 /// <summary>
217 /// Sets a preference in the profile used by Firefox.
218 /// </summary>
219 /// <param name="preferenceName">Name of the preference to set.</param>
220 /// <param name="preferenceValue">Value of the preference to set.</param>
221 public void SetPreference(string preferenceName, string preferenceValue)
222 {
223 this.SetPreferenceValue(preferenceName, preferenceValue);
224 }
225
226 /// <summary>
227 /// Sets an environment variable to be set in the operating system's environment under which the Firerox browser is launched.
228 /// </summary>
229 /// <param name="variableName">The name of the environment variable.</param>
230 /// <param name="variableValue">The value of the environment variable.</param>
231 public void SetEnvironmentVariable(string variableName, string variableValue)
232 {
233 if (string.IsNullOrEmpty(variableName))
234 {
235 throw new ArgumentException("Environment variable name cannot be null or an empty string");
236 }
237
238 if (variableValue == null)
239 {
240 variableValue = string.Empty;
241 }
242
243 this.environmentVariables[variableName] = variableValue;
244 }
245
246 /// <summary>
247 /// Provides a means to add additional capabilities not yet added as type safe options
248 /// for the Firefox driver.
249 /// </summary>
250 /// <param name="optionName">The name of the capability to add.</param>
251 /// <param name="optionValue">The value of the capability to add.</param>
252 /// <exception cref="ArgumentException">
253 /// thrown when attempting to add a capability for which there is already a type safe option, or
254 /// when <paramref name="optionName"/> is <see langword="null"/> or the empty string.
255 /// </exception>
256 /// <remarks>Calling <see cref="AddAdditionalFirefoxOption(string, object)"/>
257 /// where <paramref name="optionName"/> has already been added will overwrite the
258 /// existing value with the new value in <paramref name="optionValue"/>.
259 /// Calling this method adds capabilities to the Firefox-specific options object passed to
260 /// geckodriver.exe (property name 'moz:firefoxOptions').</remarks>
261 public void AddAdditionalFirefoxOption(string optionName, object optionValue)
262 {
263 this.ValidateCapabilityName(optionName);
264 this.additionalFirefoxOptions[optionName] = optionValue;
265 }
266
267 /// <summary>
268 /// Returns DesiredCapabilities for Firefox with these options included as
269 /// capabilities. This does not copy the options. Further changes will be
270 /// reflected in the returned capabilities.
271 /// </summary>
272 /// <returns>The DesiredCapabilities for Firefox with these options.</returns>
273 public override ICapabilities ToCapabilities()
274 {
275 IWritableCapabilities capabilities = GenerateDesiredCapabilities(true);
276 Dictionary<string, object> firefoxOptions = this.GenerateFirefoxOptionsDictionary();
277 capabilities.SetCapability(FirefoxOptionsCapability, firefoxOptions);
278 if (this.enableDevToolsProtocol)
279 {
280 capabilities.SetCapability(FirefoxEnableDevToolsProtocolCapability, true);
281 }
282
283 return capabilities.AsReadOnly();
284 }
285
286 private Dictionary<string, object> GenerateFirefoxOptionsDictionary()
287 {
288 Dictionary<string, object> firefoxOptions = new Dictionary<string, object>();
289
290 if (this.profile != null)
291 {
292 firefoxOptions[FirefoxProfileCapability] = this.profile.ToBase64String();
293 }
294
295 if (!string.IsNullOrEmpty(this.browserBinaryLocation))
296 {
297 firefoxOptions[FirefoxBinaryCapability] = this.browserBinaryLocation;
298 }
299
300 if (this.logLevel != FirefoxDriverLogLevel.Default)
301 {
302 Dictionary<string, object> logObject = new Dictionary<string, object>();
303 logObject["level"] = this.logLevel.ToString().ToLowerInvariant();
304 firefoxOptions[FirefoxLogCapability] = logObject;
305 }
306
307 if (this.firefoxArguments.Count > 0)
308 {
309 List<object> args = new List<object>();
310 foreach (string argument in this.firefoxArguments)
311 {
312 args.Add(argument);
313 }
314
315 firefoxOptions[FirefoxArgumentsCapability] = args;
316 }
317
318 if (this.profilePreferences.Count > 0)
319 {
320 firefoxOptions[FirefoxPrefsCapability] = this.profilePreferences;
321 }
322
323 if (this.environmentVariables.Count > 0)
324 {
325 firefoxOptions[FirefoxEnvCapability] = this.environmentVariables;
326 }
327
328 if (this.androidOptions != null)
329 {
330 this.AddAndroidOptions(firefoxOptions);
331 }
332
333 foreach (KeyValuePair<string, object> pair in this.additionalFirefoxOptions)
334 {
335 firefoxOptions.Add(pair.Key, pair.Value);
336 }
337
338 return firefoxOptions;
339 }
340
341 private void SetPreferenceValue(string preferenceName, object preferenceValue)
342 {
343 if (string.IsNullOrEmpty(preferenceName))
344 {
345 throw new ArgumentException("Preference name may not be null an empty string.", nameof(preferenceName));
346 }
347
348 this.profilePreferences[preferenceName] = preferenceValue;
349 }
350
351 private void AddAndroidOptions(Dictionary<string, object> firefoxOptions)
352 {
353 firefoxOptions["androidPackage"] = this.androidOptions.AndroidPackage;
354
355 if (!string.IsNullOrEmpty(this.androidOptions.AndroidDeviceSerial))
356 {
357 firefoxOptions["androidDeviceSerial"] = this.androidOptions.AndroidDeviceSerial;
358 }
359
360 if (!string.IsNullOrEmpty(this.androidOptions.AndroidActivity))
361 {
362 firefoxOptions["androidActivity"] = this.androidOptions.AndroidActivity;
363 }
364
365 if (this.androidOptions.AndroidIntentArguments.Count > 0)
366 {
367 List<object> args = new List<object>();
368 foreach (string argument in this.androidOptions.AndroidIntentArguments)
369 {
370 args.Add(argument);
371 }
372
373 firefoxOptions["androidIntentArguments"] = args;
374 }
375 }
376 }
377 }