Adding support for environment capability to .NET FirefoxOptions
@@ -58,6 +58,7 @@ public class FirefoxOptions : DriverOptions
private const string FirefoxArgumentsCapability = "args";
private const string FirefoxLogCapability = "log";
private const string FirefoxPrefsCapability = "prefs";
+ private const string FirefoxEnvCapability = "env";
private const string FirefoxOptionsCapability = "moz:firefoxOptions";
private string browserBinaryLocation;
@@ -66,6 +67,7 @@ public class FirefoxOptions : DriverOptions
private List<string> firefoxArguments = new List<string>();
private Dictionary<string, object> profilePreferences = new Dictionary<string, object>();
private Dictionary<string, object> additionalFirefoxOptions = new Dictionary<string, object>();
+ private Dictionary<string, object> environmentVariables = new Dictionary<string, object>();
/// <summary>
/// Initializes a new instance of the <see cref="FirefoxOptions"/> class.
@@ -80,6 +82,7 @@ public FirefoxOptions()
this.AddKnownCapabilityName(FirefoxOptions.FirefoxBinaryCapability, "BrowserExecutableLocation property");
this.AddKnownCapabilityName(FirefoxOptions.FirefoxArgumentsCapability, "AddArguments method");
this.AddKnownCapabilityName(FirefoxOptions.FirefoxPrefsCapability, "SetPreference method");
+ this.AddKnownCapabilityName(FirefoxOptions.FirefoxEnvCapability, "SetEnvironmentVariable method");
this.AddKnownCapabilityName(FirefoxOptions.FirefoxLogCapability, "LogLevel property");
this.AddKnownCapabilityName(FirefoxOptions.FirefoxLegacyProfileCapability, "Profile property");
this.AddKnownCapabilityName(FirefoxOptions.FirefoxLegacyBinaryCapability, "BrowserExecutableLocation property");
@@ -211,6 +214,26 @@ public void SetPreference(string preferenceName, string preferenceValue)
this.SetPreferenceValue(preferenceName, preferenceValue);
}
+ /// <summary>
+ /// Sets an environment variable to be set in the operating system's environment under which the Firerox browser is launched.
+ /// </summary>
+ /// <param name="variableName">The name of the environment variable.</param>
+ /// <param name="variableValue">The value of the environment variable.</param>
+ public void SetEnvironmentVariable(string variableName, string variableValue)
+ {
+ if (string.IsNullOrEmpty(variableName))
+ {
+ throw new ArgumentException("Environment variable name cannot be null or an empty string");
+ }
+
+ if (variableValue == null)
+ {
+ variableValue = string.Empty;
+ }
+
+ this.environmentVariables[variableName] = variableValue;
+ }
+
/// <summary>
/// Provides a means to add additional capabilities not yet added as type safe options
/// for the Firefox driver.
@@ -336,6 +359,11 @@ public override ICapabilities ToCapabilities()
firefoxOptions[FirefoxPrefsCapability] = this.profilePreferences;
}
+ if (this.environmentVariables.Count > 0)
+ {
+ firefoxOptions[FirefoxEnvCapability] = this.environmentVariables;
+ }
+
foreach (KeyValuePair<string, object> pair in this.additionalFirefoxOptions)
{
firefoxOptions.Add(pair.Key, pair.Value);
