Skip to content

Commit 2a47bee

Browse files
committed
Adding support for environment capability to .NET FirefoxOptions
1 parent f65bdec commit 2a47bee

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

dotnet/src/webdriver/Firefox/FirefoxOptions.cs

+28
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public class FirefoxOptions : DriverOptions
5858
private const string FirefoxArgumentsCapability = "args";
5959
private const string FirefoxLogCapability = "log";
6060
private const string FirefoxPrefsCapability = "prefs";
61+
private const string FirefoxEnvCapability = "env";
6162
private const string FirefoxOptionsCapability = "moz:firefoxOptions";
6263

6364
private string browserBinaryLocation;
@@ -66,6 +67,7 @@ public class FirefoxOptions : DriverOptions
6667
private List<string> firefoxArguments = new List<string>();
6768
private Dictionary<string, object> profilePreferences = new Dictionary<string, object>();
6869
private Dictionary<string, object> additionalFirefoxOptions = new Dictionary<string, object>();
70+
private Dictionary<string, object> environmentVariables = new Dictionary<string, object>();
6971

7072
/// <summary>
7173
/// Initializes a new instance of the <see cref="FirefoxOptions"/> class.
@@ -80,6 +82,7 @@ public FirefoxOptions()
8082
this.AddKnownCapabilityName(FirefoxOptions.FirefoxBinaryCapability, "BrowserExecutableLocation property");
8183
this.AddKnownCapabilityName(FirefoxOptions.FirefoxArgumentsCapability, "AddArguments method");
8284
this.AddKnownCapabilityName(FirefoxOptions.FirefoxPrefsCapability, "SetPreference method");
85+
this.AddKnownCapabilityName(FirefoxOptions.FirefoxEnvCapability, "SetEnvironmentVariable method");
8386
this.AddKnownCapabilityName(FirefoxOptions.FirefoxLogCapability, "LogLevel property");
8487
this.AddKnownCapabilityName(FirefoxOptions.FirefoxLegacyProfileCapability, "Profile property");
8588
this.AddKnownCapabilityName(FirefoxOptions.FirefoxLegacyBinaryCapability, "BrowserExecutableLocation property");
@@ -211,6 +214,26 @@ public void SetPreference(string preferenceName, string preferenceValue)
211214
this.SetPreferenceValue(preferenceName, preferenceValue);
212215
}
213216

217+
/// <summary>
218+
/// Sets an environment variable to be set in the operating system's environment under which the Firerox browser is launched.
219+
/// </summary>
220+
/// <param name="variableName">The name of the environment variable.</param>
221+
/// <param name="variableValue">The value of the environment variable.</param>
222+
public void SetEnvironmentVariable(string variableName, string variableValue)
223+
{
224+
if (string.IsNullOrEmpty(variableName))
225+
{
226+
throw new ArgumentException("Environment variable name cannot be null or an empty string");
227+
}
228+
229+
if (variableValue == null)
230+
{
231+
variableValue = string.Empty;
232+
}
233+
234+
this.environmentVariables[variableName] = variableValue;
235+
}
236+
214237
/// <summary>
215238
/// Provides a means to add additional capabilities not yet added as type safe options
216239
/// for the Firefox driver.
@@ -336,6 +359,11 @@ private Dictionary<string, object> GenerateFirefoxOptionsDictionary()
336359
firefoxOptions[FirefoxPrefsCapability] = this.profilePreferences;
337360
}
338361

362+
if (this.environmentVariables.Count > 0)
363+
{
364+
firefoxOptions[FirefoxEnvCapability] = this.environmentVariables;
365+
}
366+
339367
foreach (KeyValuePair<string, object> pair in this.additionalFirefoxOptions)
340368
{
341369
firefoxOptions.Add(pair.Key, pair.Value);

0 commit comments

Comments
 (0)