Skip to content

Commit 5516b4f

Browse files
committed
Enabling reconstitution of .NET FirefoxOptions from raw capabilities
This is a temporary measure until DesiredCapabilities is done away with for good. Fixes issue #4855.
1 parent 9120f82 commit 5516b4f

File tree

2 files changed

+85
-15
lines changed

2 files changed

+85
-15
lines changed

dotnet/src/webdriver/Firefox/FirefoxDriver.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public class FirefoxDriver : RemoteWebDriver
102102
/// Initializes a new instance of the <see cref="FirefoxDriver"/> class.
103103
/// </summary>
104104
public FirefoxDriver()
105-
: this(new FirefoxOptions(null, null))
105+
: this(new FirefoxOptions(null, null, null))
106106
{
107107
}
108108

@@ -113,7 +113,7 @@ public FirefoxDriver()
113113
/// to be used in starting Firefox.</param>
114114
[Obsolete("FirefoxDriver should not be constructed with a FirefoxProfile object. Use FirefoxOptions instead. This constructor will be removed in a future release.")]
115115
public FirefoxDriver(FirefoxProfile profile)
116-
: this(new FirefoxOptions(profile, null))
116+
: this(new FirefoxOptions(profile, null, null))
117117
{
118118
}
119119

@@ -137,7 +137,7 @@ public FirefoxDriver(ICapabilities capabilities)
137137
/// to be used in starting Firefox.</param>
138138
[Obsolete("FirefoxDriver should not be constructed with a FirefoxBinary object. Use FirefoxOptions instead. This constructor will be removed in a future release.")]
139139
public FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile)
140-
: this(new FirefoxOptions(profile, binary))
140+
: this(new FirefoxOptions(profile, binary, null))
141141
{
142142
}
143143

@@ -151,7 +151,7 @@ public FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile)
151151
/// <param name="commandTimeout">The maximum amount of time to wait for each command.</param>
152152
[Obsolete("FirefoxDriver should not be constructed with a FirefoxBinary object. Use FirefoxOptions instead. This constructor will be removed in a future release.")]
153153
public FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile, TimeSpan commandTimeout)
154-
: this((FirefoxDriverService)null, new FirefoxOptions(profile, binary), commandTimeout)
154+
: this((FirefoxDriverService)null, new FirefoxOptions(profile, binary, null), commandTimeout)
155155
{
156156
}
157157

@@ -341,16 +341,7 @@ private static FirefoxOptions CreateOptionsFromCapabilities(ICapabilities capabi
341341
FirefoxProfile profile = ExtractProfile(capabilities);
342342
DesiredCapabilities desiredCaps = RemoveUnneededCapabilities(capabilities) as DesiredCapabilities;
343343

344-
FirefoxOptions options = new FirefoxOptions(profile, binary);
345-
if (desiredCaps != null)
346-
{
347-
Dictionary<string, object> capsDictionary = desiredCaps.ToDictionary();
348-
foreach (KeyValuePair<string, object> capability in capsDictionary)
349-
{
350-
options.AddAdditionalCapability(capability.Key, capability.Value);
351-
}
352-
}
353-
344+
FirefoxOptions options = new FirefoxOptions(profile, binary, desiredCaps);
354345
return options;
355346
}
356347

dotnet/src/webdriver/Firefox/FirefoxOptions.cs

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ public FirefoxOptions()
9292
/// </summary>
9393
/// <param name="profile">The <see cref="FirefoxProfile"/> to use in the options.</param>
9494
/// <param name="binary">The <see cref="FirefoxBinary"/> to use in the options.</param>
95-
internal FirefoxOptions(FirefoxProfile profile, FirefoxBinary binary)
95+
/// <param name="capabilities">The <see cref="DesiredCapabilities"/> to copy into the options.</param>
96+
internal FirefoxOptions(FirefoxProfile profile, FirefoxBinary binary, DesiredCapabilities capabilities)
9697
{
9798
this.BrowserName = BrowserNameValue;
9899
if (profile != null)
@@ -104,6 +105,11 @@ internal FirefoxOptions(FirefoxProfile profile, FirefoxBinary binary)
104105
{
105106
this.browserBinaryLocation = binary.BinaryExecutable.ExecutablePath;
106107
}
108+
109+
if (capabilities != null)
110+
{
111+
this.ImportCapabilities(capabilities);
112+
}
107113
}
108114

109115
/// <summary>
@@ -415,5 +421,78 @@ private void SetPreferenceValue(string preferenceName, object preferenceValue)
415421

416422
this.profilePreferences[preferenceName] = preferenceValue;
417423
}
424+
425+
private void ImportCapabilities(DesiredCapabilities capabilities)
426+
{
427+
foreach (KeyValuePair<string, object> pair in capabilities.CapabilitiesDictionary)
428+
{
429+
if (pair.Key == CapabilityType.BrowserName)
430+
{
431+
}
432+
else if (pair.Key == CapabilityType.BrowserVersion)
433+
{
434+
this.BrowserVersion = pair.Value.ToString();
435+
}
436+
else if (pair.Key == CapabilityType.PlatformName)
437+
{
438+
this.PlatformName = pair.Value.ToString();
439+
}
440+
else if (pair.Key == CapabilityType.Proxy)
441+
{
442+
this.Proxy = new Proxy(pair.Value as Dictionary<string, object>);
443+
}
444+
else if (pair.Key == CapabilityType.UnhandledPromptBehavior)
445+
{
446+
this.UnhandledPromptBehavior = (UnhandledPromptBehavior)Enum.Parse(typeof(UnhandledPromptBehavior), pair.Value.ToString(), true);
447+
}
448+
else if (pair.Key == CapabilityType.PageLoadStrategy)
449+
{
450+
this.PageLoadStrategy = (PageLoadStrategy)Enum.Parse(typeof(PageLoadStrategy), pair.Value.ToString(), true);
451+
}
452+
else if (pair.Key == FirefoxOptionsCapability)
453+
{
454+
Dictionary<string, object> mozFirefoxOptions = pair.Value as Dictionary<string, object>;
455+
foreach (KeyValuePair<string, object> option in mozFirefoxOptions)
456+
{
457+
if (option.Key == FirefoxArgumentsCapability)
458+
{
459+
object[] args = option.Value as object[];
460+
for (int i = 0; i < args.Length; i++)
461+
{
462+
this.firefoxArguments.Add(args[i].ToString());
463+
}
464+
}
465+
else if (option.Key == FirefoxPrefsCapability)
466+
{
467+
this.profilePreferences = option.Value as Dictionary<string, object>;
468+
}
469+
else if (option.Key == FirefoxLogCapability)
470+
{
471+
Dictionary<string, object> logDictionary = option.Value as Dictionary<string, object>;
472+
if (logDictionary.ContainsKey("level"))
473+
{
474+
this.logLevel = (FirefoxDriverLogLevel)Enum.Parse(typeof(FirefoxDriverLogLevel), logDictionary["level"].ToString(), true);
475+
}
476+
}
477+
else if (option.Key == FirefoxBinaryCapability)
478+
{
479+
this.browserBinaryLocation = option.Value.ToString();
480+
}
481+
else if (option.Key == FirefoxProfileCapability)
482+
{
483+
this.profile = FirefoxProfile.FromBase64String(option.Value.ToString());
484+
}
485+
else
486+
{
487+
this.AddAdditionalCapability(option.Key, option.Value);
488+
}
489+
}
490+
}
491+
else
492+
{
493+
this.AddAdditionalCapability(pair.Key, pair.Value, true);
494+
}
495+
}
496+
}
418497
}
419498
}

0 commit comments

Comments
 (0)