Skip to content

Commit

Permalink
Enabling reconstitution of .NET FirefoxOptions from raw capabilities
Browse files Browse the repository at this point in the history
This is a temporary measure until DesiredCapabilities is done away with
for good. Fixes issue #4855.
  • Loading branch information
jimevans committed Mar 22, 2018
1 parent 9120f82 commit 5516b4f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 15 deletions.
19 changes: 5 additions & 14 deletions dotnet/src/webdriver/Firefox/FirefoxDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public class FirefoxDriver : RemoteWebDriver
/// Initializes a new instance of the <see cref="FirefoxDriver"/> class.
/// </summary>
public FirefoxDriver()
: this(new FirefoxOptions(null, null))
: this(new FirefoxOptions(null, null, null))
{
}

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

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

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

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

FirefoxOptions options = new FirefoxOptions(profile, binary);
if (desiredCaps != null)
{
Dictionary<string, object> capsDictionary = desiredCaps.ToDictionary();
foreach (KeyValuePair<string, object> capability in capsDictionary)
{
options.AddAdditionalCapability(capability.Key, capability.Value);
}
}

FirefoxOptions options = new FirefoxOptions(profile, binary, desiredCaps);
return options;
}

Expand Down
81 changes: 80 additions & 1 deletion dotnet/src/webdriver/Firefox/FirefoxOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public FirefoxOptions()
/// </summary>
/// <param name="profile">The <see cref="FirefoxProfile"/> to use in the options.</param>
/// <param name="binary">The <see cref="FirefoxBinary"/> to use in the options.</param>
internal FirefoxOptions(FirefoxProfile profile, FirefoxBinary binary)
/// <param name="capabilities">The <see cref="DesiredCapabilities"/> to copy into the options.</param>
internal FirefoxOptions(FirefoxProfile profile, FirefoxBinary binary, DesiredCapabilities capabilities)
{
this.BrowserName = BrowserNameValue;
if (profile != null)
Expand All @@ -104,6 +105,11 @@ internal FirefoxOptions(FirefoxProfile profile, FirefoxBinary binary)
{
this.browserBinaryLocation = binary.BinaryExecutable.ExecutablePath;
}

if (capabilities != null)
{
this.ImportCapabilities(capabilities);
}
}

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

this.profilePreferences[preferenceName] = preferenceValue;
}

private void ImportCapabilities(DesiredCapabilities capabilities)
{
foreach (KeyValuePair<string, object> pair in capabilities.CapabilitiesDictionary)
{
if (pair.Key == CapabilityType.BrowserName)
{
}
else if (pair.Key == CapabilityType.BrowserVersion)
{
this.BrowserVersion = pair.Value.ToString();
}
else if (pair.Key == CapabilityType.PlatformName)
{
this.PlatformName = pair.Value.ToString();
}
else if (pair.Key == CapabilityType.Proxy)
{
this.Proxy = new Proxy(pair.Value as Dictionary<string, object>);
}
else if (pair.Key == CapabilityType.UnhandledPromptBehavior)
{
this.UnhandledPromptBehavior = (UnhandledPromptBehavior)Enum.Parse(typeof(UnhandledPromptBehavior), pair.Value.ToString(), true);
}
else if (pair.Key == CapabilityType.PageLoadStrategy)
{
this.PageLoadStrategy = (PageLoadStrategy)Enum.Parse(typeof(PageLoadStrategy), pair.Value.ToString(), true);
}
else if (pair.Key == FirefoxOptionsCapability)
{
Dictionary<string, object> mozFirefoxOptions = pair.Value as Dictionary<string, object>;
foreach (KeyValuePair<string, object> option in mozFirefoxOptions)
{
if (option.Key == FirefoxArgumentsCapability)
{
object[] args = option.Value as object[];
for (int i = 0; i < args.Length; i++)
{
this.firefoxArguments.Add(args[i].ToString());
}
}
else if (option.Key == FirefoxPrefsCapability)
{
this.profilePreferences = option.Value as Dictionary<string, object>;
}
else if (option.Key == FirefoxLogCapability)
{
Dictionary<string, object> logDictionary = option.Value as Dictionary<string, object>;
if (logDictionary.ContainsKey("level"))
{
this.logLevel = (FirefoxDriverLogLevel)Enum.Parse(typeof(FirefoxDriverLogLevel), logDictionary["level"].ToString(), true);
}
}
else if (option.Key == FirefoxBinaryCapability)
{
this.browserBinaryLocation = option.Value.ToString();
}
else if (option.Key == FirefoxProfileCapability)
{
this.profile = FirefoxProfile.FromBase64String(option.Value.ToString());
}
else
{
this.AddAdditionalCapability(option.Key, option.Value);
}
}
}
else
{
this.AddAdditionalCapability(pair.Key, pair.Value, true);
}
}
}
}
}

0 comments on commit 5516b4f

Please sign in to comment.