Skip to content

Commit

Permalink
Remove vendor-specific names from ChromiumOptions base class.
Browse files Browse the repository at this point in the history
This makes the base ChromiumOptions class vendor-neutral, and also
allows a custom browser name when using the EdgeOptions class.

Signed-off-by: Jim Evans <[email protected]>
  • Loading branch information
bwalderman authored and jimevans committed Jan 27, 2020
1 parent ab31212 commit cfc2ae8
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 39 deletions.
29 changes: 24 additions & 5 deletions dotnet/src/webdriver/Chrome/ChromeOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,35 @@ namespace OpenQA.Selenium.Chrome
/// </example>
public class ChromeOptions : ChromiumOptions
{
private const string ChromeOptionsCapabilityName = "chromeOptions";

public ChromeOptions()
{
}

/// <summary>
/// Gets the name of the capability used to store Chrome options in
/// an <see cref="ICapabilities"/> object.
/// Gets the default value of the browserName capability.
/// </summary>
public static readonly string Capability = "goog:chromeOptions";
private const string BrowserNameValue = "chrome";
protected override string BrowserNameValue
{
get { return "chrome"; }
}

public ChromeOptions() : base(BrowserNameValue, Capability)
/// <summary>
/// Gets the vendor prefix to apply to Chromium-specific capability names.
/// </summary>
protected override string VendorPrefix
{
get { return "goog"; }
}

/// <summary>
/// Gets the name of the capability used to store Chromium options in
/// an <see cref="ICapabilities"/> object.
/// </summary>
public override string CapabilityName
{
get { return string.Format(CultureInfo.InvariantCulture, "{0}:{1}", this.VendorPrefix, ChromeOptionsCapabilityName); }
}
}
}
18 changes: 11 additions & 7 deletions dotnet/src/webdriver/Chromium/ChromiumDriver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="ChromeDriver.cs" company="WebDriver Committers">
// <copyright file="ChromiumDriver.cs" company="WebDriver Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
Expand Down Expand Up @@ -41,6 +41,8 @@ public abstract class ChromiumDriver : RemoteWebDriver, ISupportsLogs, IDevTools
private const string SendChromeCommand = "sendChromeCommand";
private const string SendChromeCommandWithResult = "sendChromeCommandWithResult";

private readonly string optionsCapabilityName;

/// <summary>
/// Initializes a new instance of the <see cref="ChromiumDriver"/> class using the specified
/// <see cref="ChromiumDriverService"/> and options.
Expand All @@ -56,11 +58,13 @@ public ChromiumDriver(ChromiumDriverService service, ChromiumOptions options)
/// Initializes a new instance of the <see cref="ChromiumDriver"/> class using the specified <see cref="ChromiumDriverService"/>.
/// </summary>
/// <param name="service">The <see cref="ChromiumDriverService"/> to use.</param>
/// <param name="options">The <see cref="ChromiumOptions"/> to be used with the Chrome driver.</param>
/// <param name="options">The <see cref="ChromiumOptions"/> to be used with the ChromiumDriver.</param>
/// <param name="commandTimeout">The maximum amount of time to wait for each command.</param>
public ChromiumDriver(ChromiumDriverService service, ChromiumOptions options, TimeSpan commandTimeout)
: base(new DriverServiceCommandExecutor(service, commandTimeout), ConvertOptionsToCapabilities(options))
{
this.optionsCapabilityName = options.CapabilityName;

// Add the custom commands unique to Chrome
this.AddCustomChromeCommand(GetNetworkConditionsCommand, CommandInfo.GetCommand, "/session/{sessionId}/chromium/network_conditions");
this.AddCustomChromeCommand(SetNetworkConditionsCommand, CommandInfo.PostCommand, "/session/{sessionId}/chromium/network_conditions");
Expand Down Expand Up @@ -147,20 +151,20 @@ public object ExecuteChromeCommandWithResult(string commandName, Dictionary<stri
/// <returns>The active session to use to communicate with the Chromium Developer Tools debugging protocol.</returns>
public DevToolsSession CreateDevToolsSession()
{
if (!this.Capabilities.HasCapability(ChromiumOptions.DefaultCapability))
if (!this.Capabilities.HasCapability(this.optionsCapabilityName))
{
throw new WebDriverException("Cannot find " + ChromiumOptions.DefaultCapability + " capability for driver");
throw new WebDriverException("Cannot find " + this.optionsCapabilityName + " capability for driver");
}

Dictionary<string, object> options = this.Capabilities.GetCapability(ChromiumOptions.DefaultCapability) as Dictionary<string, object>;
Dictionary<string, object> options = this.Capabilities.GetCapability(this.optionsCapabilityName) as Dictionary<string, object>;
if (options == null)
{
throw new WebDriverException("Found " + ChromiumOptions.DefaultCapability + " capability, but is not an object");
throw new WebDriverException("Found " + this.optionsCapabilityName + " capability, but is not an object");
}

if (!options.ContainsKey("debuggerAddress"))
{
throw new WebDriverException("Did not find debuggerAddress capability in goog:chromeOptions");
throw new WebDriverException("Did not find debuggerAddress capability in " + this.optionsCapabilityName);
}

string debuggerAddress = options["debuggerAddress"].ToString();
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/Chromium/ChromiumDriverService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="ChromeDriverService.cs" company="WebDriver Committers">
// <copyright file="ChromiumDriverService.cs" company="WebDriver Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="ChromeMobileEmulationDeviceSettings.cs" company="WebDriver Committers">
// <copyright file="ChromiumMobileEmulationDeviceSettings.cs" company="WebDriver Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/Chromium/ChromiumNetworkConditions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="ChromeNetworkConditions.cs" company="WebDriver Committers">
// <copyright file="ChromiumNetworkConditions.cs" company="WebDriver Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
Expand Down
44 changes: 27 additions & 17 deletions dotnet/src/webdriver/Chromium/ChromiumOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="ChromeOptions.cs" company="WebDriver Committers">
// <copyright file="ChromiumOptions.cs" company="WebDriver Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
Expand Down Expand Up @@ -27,15 +27,6 @@ namespace OpenQA.Selenium.Chromium
{
public abstract class ChromiumOptions : DriverOptions
{
/// <summary>
/// Gets the name of the capability used to store Chromium options in
/// an <see cref="ICapabilities"/> object.
/// </summary>
public const string DefaultCapability = "goog:chromeOptions";
public const string LoggingPreferencesChromeOption = "goog:loggingPrefs";

private const string DefaultBrowserNameValue = "chrome";

private const string ArgumentsChromeOption = "args";
private const string BinaryChromeOption = "binary";
private const string ExtensionsChromeOption = "extensions";
Expand All @@ -50,7 +41,6 @@ public abstract class ChromiumOptions : DriverOptions
private const string WindowTypesChromeOption = "windowTypes";
private const string UseSpecCompliantProtocolOption = "w3c";

private string Capability;
private bool leaveBrowserRunning;
private bool useSpecCompliantProtocol = true;
private string binaryLocation;
Expand All @@ -69,13 +59,12 @@ public abstract class ChromiumOptions : DriverOptions
private ChromiumMobileEmulationDeviceSettings mobileEmulationDeviceSettings;
private ChromiumPerformanceLoggingPreferences perfLoggingPreferences;

public ChromiumOptions(string browserName = DefaultBrowserNameValue, string capabilityKey = DefaultCapability) : base()
public ChromiumOptions() : base()
{
this.BrowserName = browserName;
this.Capability = capabilityKey;
this.AddKnownCapabilityName(capabilityKey, "current ChromeOptions class instance");
this.BrowserName = BrowserNameValue;
this.AddKnownCapabilityName(this.CapabilityName, "current ChromiumOptions class instance");
this.AddKnownCapabilityName(CapabilityType.LoggingPreferences, "SetLoggingPreference method");
this.AddKnownCapabilityName(ChromiumOptions.LoggingPreferencesChromeOption, "SetLoggingPreference method");
this.AddKnownCapabilityName(this.LoggingPreferencesChromeOption, "SetLoggingPreference method");
this.AddKnownCapabilityName(ChromiumOptions.ArgumentsChromeOption, "AddArguments method");
this.AddKnownCapabilityName(ChromiumOptions.BinaryChromeOption, "BinaryLocation property");
this.AddKnownCapabilityName(ChromiumOptions.ExtensionsChromeOption, "AddExtensions method");
Expand All @@ -91,6 +80,27 @@ public ChromiumOptions(string browserName = DefaultBrowserNameValue, string capa
this.AddKnownCapabilityName(ChromiumOptions.UseSpecCompliantProtocolOption, "UseSpecCompliantProtocol property");
}

/// <summary>
/// Gets the default value of the browserName capability.
/// </summary>
protected abstract string BrowserNameValue { get; }

/// <summary>
/// Gets the vendor prefix to apply to Chromium-specific capability names.
/// </summary>
protected abstract string VendorPrefix { get; }

private string LoggingPreferencesChromeOption
{
get { return this.VendorPrefix + ":loggingPrefs"; }
}

/// <summary>
/// Gets the name of the capability used to store Chromium options in
/// an <see cref="ICapabilities"/> object.
/// </summary>
public abstract string CapabilityName { get; }

/// <summary>
/// Gets or sets the location of the Chromium browser's binary executable file.
/// </summary>
Expand Down Expand Up @@ -549,7 +559,7 @@ public override ICapabilities ToCapabilities()
Dictionary<string, object> chromeOptions = this.BuildChromeOptionsDictionary();

IWritableCapabilities capabilities = this.GenerateDesiredCapabilities(false);
capabilities.SetCapability(this.Capability, chromeOptions);
capabilities.SetCapability(this.CapabilityName, chromeOptions);

Dictionary<string, object> loggingPreferences = this.GenerateLoggingPreferencesDictionary();
if (loggingPreferences != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="ChromePerformanceLoggingPreferences.cs" company="WebDriver Committers">
// <copyright file="ChromiumPerformanceLoggingPreferences.cs" company="WebDriver Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/Chromium/ChromiumWebElement.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="ChromeWebElement.cs" company="WebDriver Committers">
// <copyright file="ChromiumWebElement.cs" company="WebDriver Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
Expand Down
37 changes: 32 additions & 5 deletions dotnet/src/webdriver/Edge/EdgeOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,17 @@ namespace OpenQA.Selenium.Edge
/// </example>
public class EdgeOptions : ChromiumOptions
{
private const string BrowserNameValue = "MicrosoftEdge";
private const string DefaultBrowserNameValue = "MicrosoftEdge";
private const string UseInPrivateBrowsingCapability = "ms:inPrivate";
private const string ExtensionPathsCapability = "ms:extensionPaths";
private const string StartPageCapability = "ms:startPage";
private const string EdgeOptionsCapabilityName = "edgeOptions";

private static readonly string[] ChromiumCapabilityNames = { "goog:chromeOptions", "se:forceAlwaysMatch", "args",
"binary", "extensions", "localState", "prefs", "detach", "debuggerAddress", "excludeSwitches", "minidumpPath",
"mobileEmulation", "perfLoggingPrefs", "windowTypes", "w3c"};

private readonly string browserName;
private bool useInPrivateBrowsing;
private string startPage;
private List<string> extensionPaths = new List<string>();
Expand All @@ -67,13 +69,14 @@ public EdgeOptions() : this(true)
}

/// <summary>
/// Create an EdgeOption for ChromiumEdge
/// Create an EdgeOptions for Chromium-based Edge.
/// </summary>
/// <param name="isLegacy">Whether to use Legacy Mode. If so, remove all Chromium Capabilities</param>
/// <param name="isLegacy">Whether to use Legacy Mode. If so, remove all Chromium Capabilities.</param>
/// <param name="browserName">The name of the browser to use. Defaults to "MicrosoftEdge".</param>
public EdgeOptions(bool isLegacy, string browserName = BrowserNameValue) : base(browserName, "ms:edgeOptions")
public EdgeOptions(bool isLegacy, string browserName = DefaultBrowserNameValue)
{
this.isLegacy = isLegacy;
this.browserName = browserName;

if (this.isLegacy)
{
Expand All @@ -88,6 +91,31 @@ public EdgeOptions(bool isLegacy, string browserName = BrowserNameValue) : base(
}
}

/// <summary>
/// Gets the default value of the browserName capability.
/// </summary>
protected override string BrowserNameValue
{
get { return browserName; }
}

/// <summary>
/// Gets the vendor prefix to apply to Chromium-specific capability names.
/// </summary>
protected override string VendorPrefix
{
get { return "ms"; }
}

/// <summary>
/// Gets the name of the capability used to store Chromium options in
/// an <see cref="ICapabilities"/> object.
/// </summary>
public override string CapabilityName
{
get { return string.Format(CultureInfo.InvariantCulture, "{0}:{1}", this.VendorPrefix, EdgeOptionsCapabilityName); }
}

/// <summary>
/// Gets or sets the location of the Edge browser's binary executable file.
/// </summary>
Expand Down Expand Up @@ -164,7 +192,6 @@ public string StartPage
}
}


/// <summary>
/// Adds a path to an extension that is to be used with the Edge driver.
/// </summary>
Expand Down

0 comments on commit cfc2ae8

Please sign in to comment.