Skip to content

Commit

Permalink
Refactoring .NET driver Options classes to descend from common class
Browse files Browse the repository at this point in the history
This commit introduces a common base class for driver-specific type-safe
Options classes (e.g., ChromeOptions, InternetExplorerOptions, etc.). This
will help pave the way to eliminate needing to know the name or expected
type of arbitrary capabilities in a future release.
  • Loading branch information
jimevans committed Feb 5, 2016
1 parent cf812cd commit 48b9df4
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 51 deletions.
7 changes: 3 additions & 4 deletions dotnet/src/webdriver/Chrome/ChromeOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
using System.Globalization;
using System.IO;
using System.Text;
using Newtonsoft.Json;
using OpenQA.Selenium.Remote;

namespace OpenQA.Selenium.Chrome
Expand Down Expand Up @@ -52,7 +51,7 @@ namespace OpenQA.Selenium.Chrome
/// RemoteWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options.ToCapabilities());
/// </code>
/// </example>
public class ChromeOptions
public class ChromeOptions : DriverOptions
{
/// <summary>
/// Gets the name of the capability used to store Chrome options in
Expand Down Expand Up @@ -496,7 +495,7 @@ public void AddWindowTypes(IEnumerable<string> windowTypesToAdd)
/// existing value with the new value in <paramref name="capabilityValue"/>.
/// Also, by default, calling this method adds capabilities to the options object passed to
/// chromedriver.exe.</remarks>
public void AddAdditionalCapability(string capabilityName, object capabilityValue)
public override void AddAdditionalCapability(string capabilityName, object capabilityValue)
{
// Add the capability to the chromeOptions object by default. This is to handle
// the 80% case where the chromedriver team adds a new option in chromedriver.exe
Expand Down Expand Up @@ -562,7 +561,7 @@ public void AddAdditionalCapability(string capabilityName, object capabilityValu
/// reflected in the returned capabilities.
/// </summary>
/// <returns>The DesiredCapabilities for Chrome with these options.</returns>
public ICapabilities ToCapabilities()
public override ICapabilities ToCapabilities()
{
Dictionary<string, object> chromeOptions = this.BuildChromeOptionsDictionary();

Expand Down
55 changes: 55 additions & 0 deletions dotnet/src/webdriver/DriverOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// <copyright file="DriverOptions.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
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OpenQA.Selenium
{
/// <summary>
/// Base class for managing options specific to a browser driver.
/// </summary>
public abstract class DriverOptions
{
/// <summary>
/// Provides a means to add additional capabilities not yet added as type safe options
/// for the specific browser driver.
/// </summary>
/// <param name="capabilityName">The name of the capability to add.</param>
/// <param name="capabilityValue">The value of the capability to add.</param>
/// <exception cref="ArgumentException">
/// thrown when attempting to add a capability for which there is already a type safe option, or
/// when <paramref name="capabilityName"/> is <see langword="null"/> or the empty string.
/// </exception>
/// <remarks>Calling <see cref="AddAdditionalCapability(string, object)"/>
/// where <paramref name="capabilityName"/> has already been added will overwrite the
/// existing value with the new value in <paramref name="capabilityValue"/>.
/// </remarks>
public abstract void AddAdditionalCapability(string capabilityName, object capabilityValue);

/// <summary>
/// Returns DesiredCapabilities for the specific browser driver with these
/// options included ascapabilities. This does not copy the options. Further
/// changes will be reflected in the returned capabilities.
/// </summary>
/// <returns>The DesiredCapabilities for browser driver with these options.</returns>
public abstract ICapabilities ToCapabilities();
}
}
6 changes: 3 additions & 3 deletions dotnet/src/webdriver/Edge/EdgeOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public enum EdgePageLoadStrategy
/// RemoteWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options.ToCapabilities());
/// </code>
/// </example>
public class EdgeOptions
public class EdgeOptions : DriverOptions
{
private EdgePageLoadStrategy pageLoadStrategy = EdgePageLoadStrategy.Default;
private Dictionary<string, object> additionalCapabilities = new Dictionary<string, object>();
Expand All @@ -100,7 +100,7 @@ public EdgePageLoadStrategy PageLoadStrategy
/// </exception>
/// <remarks>Calling <see cref="AddAdditionalCapability"/> where <paramref name="capabilityName"/>
/// has already been added will overwrite the existing value with the new value in <paramref name="capabilityValue"/></remarks>
public void AddAdditionalCapability(string capabilityName, object capabilityValue)
public override void AddAdditionalCapability(string capabilityName, object capabilityValue)
{
if (capabilityName == CapabilityType.PageLoadStrategy)
{
Expand All @@ -122,7 +122,7 @@ public void AddAdditionalCapability(string capabilityName, object capabilityValu
/// reflected in the returned capabilities.
/// </summary>
/// <returns>The DesiredCapabilities for Edge with these options.</returns>
public ICapabilities ToCapabilities()
public override ICapabilities ToCapabilities()
{
DesiredCapabilities capabilities = DesiredCapabilities.Edge();
if (this.pageLoadStrategy != EdgePageLoadStrategy.Default)
Expand Down
21 changes: 19 additions & 2 deletions dotnet/src/webdriver/Firefox/FirefoxOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace OpenQA.Selenium.Firefox
/// RemoteWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options.ToCapabilities());
/// </code>
/// </example>
public class FirefoxOptions
public class FirefoxOptions : DriverOptions
{
private bool isMarionette = true;

Expand All @@ -60,13 +60,30 @@ public bool IsMarionette
set { this.isMarionette = value; }
}

/// <summary>
/// Provides a means to add additional capabilities not yet added as type safe options
/// for the Firefox driver.
/// </summary>
/// <param name="capabilityName">The name of the capability to add.</param>
/// <param name="capabilityValue">The value of the capability to add.</param>
/// <exception cref="ArgumentException">
/// thrown when attempting to add a capability for which there is already a type safe option, or
/// when <paramref name="capabilityName"/> is <see langword="null"/> or the empty string.
/// </exception>
/// <remarks>For the moment, this method has no effect for the Firefox driver, as use
/// of the FirefoxOptions class is only used as a marker for Marionette. This will
/// change in the future.</remarks>
public override void AddAdditionalCapability(string capabilityName, object capabilityValue)
{
}

/// <summary>
/// Returns DesiredCapabilities for Firefox with these options included as
/// capabilities. This does not copy the options. Further changes will be
/// reflected in the returned capabilities.
/// </summary>
/// <returns>The DesiredCapabilities for Firefox with these options.</returns>
public ICapabilities ToCapabilities()
public override ICapabilities ToCapabilities()
{
DesiredCapabilities capabilities = DesiredCapabilities.Firefox();
capabilities.SetCapability("marionette", this.isMarionette);
Expand Down
6 changes: 3 additions & 3 deletions dotnet/src/webdriver/IE/InternetExplorerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public enum InternetExplorerPageLoadStrategy
/// RemoteWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options.ToCapabilities());
/// </code>
/// </example>
public class InternetExplorerOptions
public class InternetExplorerOptions : DriverOptions
{
private const string IgnoreProtectedModeSettingsCapability = "ignoreProtectedModeSettings";
private const string IgnoreZoomSettingCapability = "ignoreZoomSetting";
Expand Down Expand Up @@ -353,7 +353,7 @@ public bool EnsureCleanSession
/// </exception>
/// <remarks>Calling <see cref="AddAdditionalCapability"/> where <paramref name="capabilityName"/>
/// has already been added will overwrite the existing value with the new value in <paramref name="capabilityValue"/></remarks>
public void AddAdditionalCapability(string capabilityName, object capabilityValue)
public override void AddAdditionalCapability(string capabilityName, object capabilityValue)
{
if (capabilityName == IgnoreProtectedModeSettingsCapability ||
capabilityName == IgnoreZoomSettingCapability ||
Expand Down Expand Up @@ -392,7 +392,7 @@ public void AddAdditionalCapability(string capabilityName, object capabilityValu
/// reflected in the returned capabilities.
/// </summary>
/// <returns>The DesiredCapabilities for IE with these options.</returns>
public ICapabilities ToCapabilities()
public override ICapabilities ToCapabilities()
{
DesiredCapabilities capabilities = DesiredCapabilities.InternetExplorer();
capabilities.SetCapability(CapabilityType.HasNativeEvents, this.enableNativeEvents);
Expand Down
6 changes: 3 additions & 3 deletions dotnet/src/webdriver/Opera/OperaOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace OpenQA.Selenium.Opera
/// RemoteWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options.ToCapabilities());
/// </code>
/// </example>
public class OperaOptions
public class OperaOptions : DriverOptions
{
/// <summary>
/// Gets the name of the capability used to store Opera options in
Expand Down Expand Up @@ -385,7 +385,7 @@ public void AddLocalStatePreference(string preferenceName, object preferenceValu
/// existing value with the new value in <paramref name="capabilityValue"/>.
/// Also, by default, calling this method adds capabilities to the options object passed to
/// operadriver.exe.</remarks>
public void AddAdditionalCapability(string capabilityName, object capabilityValue)
public override void AddAdditionalCapability(string capabilityName, object capabilityValue)
{
// Add the capability to the OperaOptions object by default. This is to handle
// the 80% case where the Operadriver team adds a new option in Operadriver.exe
Expand Down Expand Up @@ -448,7 +448,7 @@ public void AddAdditionalCapability(string capabilityName, object capabilityValu
/// reflected in the returned capabilities.
/// </summary>
/// <returns>The DesiredCapabilities for Opera with these options.</returns>
public ICapabilities ToCapabilities()
public override ICapabilities ToCapabilities()
{
Dictionary<string, object> operaOptions = this.BuildOperaOptionsDictionary();

Expand Down
6 changes: 3 additions & 3 deletions dotnet/src/webdriver/PhantomJS/PhantomJSOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace OpenQA.Selenium.PhantomJS
/// RemoteWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options.ToCapabilities());
/// </code>
/// </example>
public class PhantomJSOptions
public class PhantomJSOptions : DriverOptions
{
private Dictionary<string, object> additionalCapabilities = new Dictionary<string, object>();

Expand All @@ -59,7 +59,7 @@ public class PhantomJSOptions
/// </exception>
/// <remarks>Calling <see cref="AddAdditionalCapability"/> where <paramref name="capabilityName"/>
/// has already been added will overwrite the existing value with the new value in <paramref name="capabilityValue"/></remarks>
public void AddAdditionalCapability(string capabilityName, object capabilityValue)
public override void AddAdditionalCapability(string capabilityName, object capabilityValue)
{
if (string.IsNullOrEmpty(capabilityName))
{
Expand All @@ -75,7 +75,7 @@ public void AddAdditionalCapability(string capabilityName, object capabilityValu
/// reflected in the returned capabilities.
/// </summary>
/// <returns>The DesiredCapabilities for PhantomJS with these options.</returns>
public ICapabilities ToCapabilities()
public override ICapabilities ToCapabilities()
{
DesiredCapabilities capabilities = DesiredCapabilities.PhantomJS();

Expand Down
60 changes: 30 additions & 30 deletions dotnet/src/webdriver/Remote/RemoteWebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,36 @@ public class RemoteWebDriver : IWebDriver, ISearchContext, IJavaScriptExecutor,
private ILocationContext locationContext;
private IFileDetector fileDetector = new DefaultFileDetector();

/// <summary>
/// Initializes a new instance of the <see cref="RemoteWebDriver"/> class. This constructor defaults proxy to http://127.0.0.1:4444/wd/hub
/// </summary>
/// <param name="desiredCapabilities">An <see cref="ICapabilities"/> object containing the desired capabilities of the browser.</param>
public RemoteWebDriver(ICapabilities desiredCapabilities)
: this(new Uri("http://127.0.0.1:4444/wd/hub"), desiredCapabilities)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="RemoteWebDriver"/> class
/// </summary>
/// <param name="remoteAddress">URI containing the address of the WebDriver remote server (e.g. http://127.0.0.1:4444/wd/hub).</param>
/// <param name="desiredCapabilities">An <see cref="ICapabilities"/> object containing the desired capabilities of the browser.</param>
public RemoteWebDriver(Uri remoteAddress, ICapabilities desiredCapabilities)
: this(remoteAddress, desiredCapabilities, RemoteWebDriver.DefaultCommandTimeout)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="RemoteWebDriver"/> class using the specified remote address, desired capabilities, and command timeout.
/// </summary>
/// <param name="remoteAddress">URI containing the address of the WebDriver remote server (e.g. http://127.0.0.1:4444/wd/hub).</param>
/// <param name="desiredCapabilities">An <see cref="ICapabilities"/> object containing the desired capabilities of the browser.</param>
/// <param name="commandTimeout">The maximum amount of time to wait for each command.</param>
public RemoteWebDriver(Uri remoteAddress, ICapabilities desiredCapabilities, TimeSpan commandTimeout)
: this(new HttpCommandExecutor(remoteAddress, commandTimeout), desiredCapabilities)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="RemoteWebDriver"/> class
/// </summary>
Expand Down Expand Up @@ -117,36 +147,6 @@ public RemoteWebDriver(ICommandExecutor commandExecutor, ICapabilities desiredCa
}
}

/// <summary>
/// Initializes a new instance of the <see cref="RemoteWebDriver"/> class. This constructor defaults proxy to http://127.0.0.1:4444/wd/hub
/// </summary>
/// <param name="desiredCapabilities">An <see cref="ICapabilities"/> object containing the desired capabilities of the browser.</param>
public RemoteWebDriver(ICapabilities desiredCapabilities)
: this(new Uri("http://127.0.0.1:4444/wd/hub"), desiredCapabilities)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="RemoteWebDriver"/> class
/// </summary>
/// <param name="remoteAddress">URI containing the address of the WebDriver remote server (e.g. http://127.0.0.1:4444/wd/hub).</param>
/// <param name="desiredCapabilities">An <see cref="ICapabilities"/> object containing the desired capabilities of the browser.</param>
public RemoteWebDriver(Uri remoteAddress, ICapabilities desiredCapabilities)
: this(remoteAddress, desiredCapabilities, RemoteWebDriver.DefaultCommandTimeout)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="RemoteWebDriver"/> class using the specified remote address, desired capabilities, and command timeout.
/// </summary>
/// <param name="remoteAddress">URI containing the address of the WebDriver remote server (e.g. http://127.0.0.1:4444/wd/hub).</param>
/// <param name="desiredCapabilities">An <see cref="ICapabilities"/> object containing the desired capabilities of the browser.</param>
/// <param name="commandTimeout">The maximum amount of time to wait for each command.</param>
public RemoteWebDriver(Uri remoteAddress, ICapabilities desiredCapabilities, TimeSpan commandTimeout)
: this(new HttpCommandExecutor(remoteAddress, commandTimeout), desiredCapabilities)
{
}

/// <summary>
/// Gets or sets the URL the browser is currently displaying.
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions dotnet/src/webdriver/Safari/SafariOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace OpenQA.Selenium.Safari
/// RemoteWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options.ToCapabilities());
/// </code>
/// </example>
public class SafariOptions
public class SafariOptions : DriverOptions
{
private int port;
private bool skipExtensionInstallation;
Expand Down Expand Up @@ -114,7 +114,7 @@ public bool SkipExtensionInstallation
/// </exception>
/// <remarks>Calling <see cref="AddAdditionalCapability"/> where <paramref name="capabilityName"/>
/// has already been added will overwrite the existing value with the new value in <paramref name="capabilityValue"/></remarks>
public void AddAdditionalCapability(string capabilityName, object capabilityValue)
public override void AddAdditionalCapability(string capabilityName, object capabilityValue)
{
if (string.IsNullOrEmpty(capabilityName))
{
Expand All @@ -130,7 +130,7 @@ public void AddAdditionalCapability(string capabilityName, object capabilityValu
/// reflected in the returned capabilities.
/// </summary>
/// <returns>The ICapabilities for Safari with these options.</returns>
public ICapabilities ToCapabilities()
public override ICapabilities ToCapabilities()
{
DesiredCapabilities capabilities = DesiredCapabilities.Safari();
foreach (KeyValuePair<string, object> pair in this.additionalCapabilities)
Expand Down
1 change: 1 addition & 0 deletions dotnet/src/webdriver/WebDriver.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<Compile Include="Chrome\ChromeMobileEmulationDeviceSettings.cs" />
<Compile Include="Chrome\ChromePerformanceLoggingPreferences.cs" />
<Compile Include="DefaultFileDetector.cs" />
<Compile Include="DriverOptions.cs" />
<Compile Include="Edge\EdgeDriver.cs" />
<Compile Include="Edge\EdgeDriverService.cs" />
<Compile Include="Edge\EdgeOptions.cs" />
Expand Down

0 comments on commit 48b9df4

Please sign in to comment.