Skip to content

Commit

Permalink
Adding ability to use chromedriver in spec-compliant mode in .NET
Browse files Browse the repository at this point in the history
This code is intended to be only temporary, until the appropriate bugs in
chromedriver are fixed so that the proper new session payload can be sent
to chromedriver, and a session created.
  • Loading branch information
jimevans committed Aug 20, 2018
1 parent dbf0de8 commit e09d7d2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
20 changes: 20 additions & 0 deletions dotnet/src/webdriver/Chrome/ChromeOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public class ChromeOptions : DriverOptions
/// </summary>
public static readonly string Capability = "goog:chromeOptions";

// TODO: Remove this if block when chromedriver bug 2371 is fixed
// (https://bugs.chromium.org/p/chromedriver/issues/detail?id=2371)
internal static readonly string ForceAlwaysMatchCapabilityName = "se:forceAlwaysMatch";

private const string BrowserNameValue = "chrome";

private const string ArgumentsChromeOption = "args";
Expand Down Expand Up @@ -111,6 +115,7 @@ public ChromeOptions() : base()
this.AddKnownCapabilityName(ChromeOptions.PerformanceLoggingPreferencesChromeOption, "PerformanceLoggingPreferences property");
this.AddKnownCapabilityName(ChromeOptions.WindowTypesChromeOption, "AddWindowTypes method");
this.AddKnownCapabilityName(ChromeOptions.UseSpecCompliantProtocolOption, "UseSpecCompliantProtocol property");
this.AddKnownCapabilityName(ChromeOptions.ForceAlwaysMatchCapabilityName, "");
}

/// <summary>
Expand Down Expand Up @@ -531,6 +536,14 @@ public void AddAdditionalCapability(string capabilityName, object capabilityValu
{
string typeSafeOptionName = this.GetTypeSafeOptionName(capabilityName);
string message = string.Format(CultureInfo.InvariantCulture, "There is already an option for the {0} capability. Please use the {1} instead.", capabilityName, typeSafeOptionName);

// TODO: Remove this if block when chromedriver bug 2371 is fixed
// (https://bugs.chromium.org/p/chromedriver/issues/detail?id=2371)
if (capabilityName == ForceAlwaysMatchCapabilityName)
{
message = string.Format(CultureInfo.InvariantCulture, "The {0} capability is internal to the driver, and not intended to be set from users' code. Do not attempt to set this capability.", capabilityName);
}

throw new ArgumentException(message, "capabilityName");
}

Expand Down Expand Up @@ -573,6 +586,13 @@ public override ICapabilities ToCapabilities()
capabilities.SetCapability(pair.Key, pair.Value);
}

// TODO: Remove this if block when chromedriver bug 2371 is fixed
// (https://bugs.chromium.org/p/chromedriver/issues/detail?id=2371)
if (this.useSpecCompliantProtocol)
{
capabilities.SetCapability(ForceAlwaysMatchCapabilityName, true);
}

// Should return capabilities.AsReadOnly(), and will in a future release.
return capabilities;
}
Expand Down
32 changes: 27 additions & 5 deletions dotnet/src/webdriver/Remote/RemoteWebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1074,13 +1074,35 @@ protected void StartSession(ICapabilities desiredCapabilities)
{
parameters.Add("desiredCapabilities", this.GetLegacyCapabilitiesDictionary(desiredCapabilities));

Dictionary<string, object> firstMatchCapabilities = this.GetCapabilitiesDictionary(desiredCapabilities);

List<object> firstMatchCapabilitiesList = new List<object>();
firstMatchCapabilitiesList.Add(firstMatchCapabilities);
Dictionary<string, object> matchCapabilities = this.GetCapabilitiesDictionary(desiredCapabilities);

// TODO: Remove this when chromedriver bug 2371 is fixed.
// (https://bugs.chromium.org/p/chromedriver/issues/detail?id=2371)
// Chromedriver does not recognize the W3C capability when put in the
// "firstMatch" property of the new session command payload, but it does
// recognize it in the "alwaysMatch" property. Temporarily, and only if
// we are using a set of capabilities for Chrome where the W3C capability
// is specified, use them in alwaysMatch instead of firstMatch. This
// piece of code is intended to only be temporary.
bool forceAlwaysMatch = false;
if (matchCapabilities.ContainsKey(Chrome.ChromeOptions.ForceAlwaysMatchCapabilityName))
{
forceAlwaysMatch = true;
matchCapabilities.Remove(Chrome.ChromeOptions.ForceAlwaysMatchCapabilityName);
}

Dictionary<string, object> specCompliantCapabilitiesDictionary = new Dictionary<string, object>();
specCompliantCapabilitiesDictionary["firstMatch"] = firstMatchCapabilitiesList;
if (forceAlwaysMatch)
{
specCompliantCapabilitiesDictionary["alwaysMatch"] = matchCapabilities;
}
else
{
List<object> firstMatchCapabilitiesList = new List<object>();
firstMatchCapabilitiesList.Add(matchCapabilities);

specCompliantCapabilitiesDictionary["firstMatch"] = firstMatchCapabilitiesList;
}

parameters.Add("capabilities", specCompliantCapabilitiesDictionary);
}
Expand Down

0 comments on commit e09d7d2

Please sign in to comment.