Skip to content

Commit 48b9df4

Browse files
committed
Refactoring .NET driver Options classes to descend from common class
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.
1 parent cf812cd commit 48b9df4

File tree

10 files changed

+123
-51
lines changed

10 files changed

+123
-51
lines changed

dotnet/src/webdriver/Chrome/ChromeOptions.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
using System.Globalization;
2323
using System.IO;
2424
using System.Text;
25-
using Newtonsoft.Json;
2625
using OpenQA.Selenium.Remote;
2726

2827
namespace OpenQA.Selenium.Chrome
@@ -52,7 +51,7 @@ namespace OpenQA.Selenium.Chrome
5251
/// RemoteWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options.ToCapabilities());
5352
/// </code>
5453
/// </example>
55-
public class ChromeOptions
54+
public class ChromeOptions : DriverOptions
5655
{
5756
/// <summary>
5857
/// Gets the name of the capability used to store Chrome options in
@@ -496,7 +495,7 @@ public void AddWindowTypes(IEnumerable<string> windowTypesToAdd)
496495
/// existing value with the new value in <paramref name="capabilityValue"/>.
497496
/// Also, by default, calling this method adds capabilities to the options object passed to
498497
/// chromedriver.exe.</remarks>
499-
public void AddAdditionalCapability(string capabilityName, object capabilityValue)
498+
public override void AddAdditionalCapability(string capabilityName, object capabilityValue)
500499
{
501500
// Add the capability to the chromeOptions object by default. This is to handle
502501
// the 80% case where the chromedriver team adds a new option in chromedriver.exe
@@ -562,7 +561,7 @@ public void AddAdditionalCapability(string capabilityName, object capabilityValu
562561
/// reflected in the returned capabilities.
563562
/// </summary>
564563
/// <returns>The DesiredCapabilities for Chrome with these options.</returns>
565-
public ICapabilities ToCapabilities()
564+
public override ICapabilities ToCapabilities()
566565
{
567566
Dictionary<string, object> chromeOptions = this.BuildChromeOptionsDictionary();
568567

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// <copyright file="DriverOptions.cs" company="WebDriver Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
// </copyright>
18+
19+
using System;
20+
using System.Collections.Generic;
21+
using System.Linq;
22+
using System.Text;
23+
24+
namespace OpenQA.Selenium
25+
{
26+
/// <summary>
27+
/// Base class for managing options specific to a browser driver.
28+
/// </summary>
29+
public abstract class DriverOptions
30+
{
31+
/// <summary>
32+
/// Provides a means to add additional capabilities not yet added as type safe options
33+
/// for the specific browser driver.
34+
/// </summary>
35+
/// <param name="capabilityName">The name of the capability to add.</param>
36+
/// <param name="capabilityValue">The value of the capability to add.</param>
37+
/// <exception cref="ArgumentException">
38+
/// thrown when attempting to add a capability for which there is already a type safe option, or
39+
/// when <paramref name="capabilityName"/> is <see langword="null"/> or the empty string.
40+
/// </exception>
41+
/// <remarks>Calling <see cref="AddAdditionalCapability(string, object)"/>
42+
/// where <paramref name="capabilityName"/> has already been added will overwrite the
43+
/// existing value with the new value in <paramref name="capabilityValue"/>.
44+
/// </remarks>
45+
public abstract void AddAdditionalCapability(string capabilityName, object capabilityValue);
46+
47+
/// <summary>
48+
/// Returns DesiredCapabilities for the specific browser driver with these
49+
/// options included ascapabilities. This does not copy the options. Further
50+
/// changes will be reflected in the returned capabilities.
51+
/// </summary>
52+
/// <returns>The DesiredCapabilities for browser driver with these options.</returns>
53+
public abstract ICapabilities ToCapabilities();
54+
}
55+
}

dotnet/src/webdriver/Edge/EdgeOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public enum EdgePageLoadStrategy
7373
/// RemoteWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options.ToCapabilities());
7474
/// </code>
7575
/// </example>
76-
public class EdgeOptions
76+
public class EdgeOptions : DriverOptions
7777
{
7878
private EdgePageLoadStrategy pageLoadStrategy = EdgePageLoadStrategy.Default;
7979
private Dictionary<string, object> additionalCapabilities = new Dictionary<string, object>();
@@ -100,7 +100,7 @@ public EdgePageLoadStrategy PageLoadStrategy
100100
/// </exception>
101101
/// <remarks>Calling <see cref="AddAdditionalCapability"/> where <paramref name="capabilityName"/>
102102
/// has already been added will overwrite the existing value with the new value in <paramref name="capabilityValue"/></remarks>
103-
public void AddAdditionalCapability(string capabilityName, object capabilityValue)
103+
public override void AddAdditionalCapability(string capabilityName, object capabilityValue)
104104
{
105105
if (capabilityName == CapabilityType.PageLoadStrategy)
106106
{
@@ -122,7 +122,7 @@ public void AddAdditionalCapability(string capabilityName, object capabilityValu
122122
/// reflected in the returned capabilities.
123123
/// </summary>
124124
/// <returns>The DesiredCapabilities for Edge with these options.</returns>
125-
public ICapabilities ToCapabilities()
125+
public override ICapabilities ToCapabilities()
126126
{
127127
DesiredCapabilities capabilities = DesiredCapabilities.Edge();
128128
if (this.pageLoadStrategy != EdgePageLoadStrategy.Default)

dotnet/src/webdriver/Firefox/FirefoxOptions.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ namespace OpenQA.Selenium.Firefox
4747
/// RemoteWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options.ToCapabilities());
4848
/// </code>
4949
/// </example>
50-
public class FirefoxOptions
50+
public class FirefoxOptions : DriverOptions
5151
{
5252
private bool isMarionette = true;
5353

@@ -60,13 +60,30 @@ public bool IsMarionette
6060
set { this.isMarionette = value; }
6161
}
6262

63+
/// <summary>
64+
/// Provides a means to add additional capabilities not yet added as type safe options
65+
/// for the Firefox driver.
66+
/// </summary>
67+
/// <param name="capabilityName">The name of the capability to add.</param>
68+
/// <param name="capabilityValue">The value of the capability to add.</param>
69+
/// <exception cref="ArgumentException">
70+
/// thrown when attempting to add a capability for which there is already a type safe option, or
71+
/// when <paramref name="capabilityName"/> is <see langword="null"/> or the empty string.
72+
/// </exception>
73+
/// <remarks>For the moment, this method has no effect for the Firefox driver, as use
74+
/// of the FirefoxOptions class is only used as a marker for Marionette. This will
75+
/// change in the future.</remarks>
76+
public override void AddAdditionalCapability(string capabilityName, object capabilityValue)
77+
{
78+
}
79+
6380
/// <summary>
6481
/// Returns DesiredCapabilities for Firefox with these options included as
6582
/// capabilities. This does not copy the options. Further changes will be
6683
/// reflected in the returned capabilities.
6784
/// </summary>
6885
/// <returns>The DesiredCapabilities for Firefox with these options.</returns>
69-
public ICapabilities ToCapabilities()
86+
public override ICapabilities ToCapabilities()
7087
{
7188
DesiredCapabilities capabilities = DesiredCapabilities.Firefox();
7289
capabilities.SetCapability("marionette", this.isMarionette);

dotnet/src/webdriver/IE/InternetExplorerOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public enum InternetExplorerPageLoadStrategy
113113
/// RemoteWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options.ToCapabilities());
114114
/// </code>
115115
/// </example>
116-
public class InternetExplorerOptions
116+
public class InternetExplorerOptions : DriverOptions
117117
{
118118
private const string IgnoreProtectedModeSettingsCapability = "ignoreProtectedModeSettings";
119119
private const string IgnoreZoomSettingCapability = "ignoreZoomSetting";
@@ -353,7 +353,7 @@ public bool EnsureCleanSession
353353
/// </exception>
354354
/// <remarks>Calling <see cref="AddAdditionalCapability"/> where <paramref name="capabilityName"/>
355355
/// has already been added will overwrite the existing value with the new value in <paramref name="capabilityValue"/></remarks>
356-
public void AddAdditionalCapability(string capabilityName, object capabilityValue)
356+
public override void AddAdditionalCapability(string capabilityName, object capabilityValue)
357357
{
358358
if (capabilityName == IgnoreProtectedModeSettingsCapability ||
359359
capabilityName == IgnoreZoomSettingCapability ||
@@ -392,7 +392,7 @@ public void AddAdditionalCapability(string capabilityName, object capabilityValu
392392
/// reflected in the returned capabilities.
393393
/// </summary>
394394
/// <returns>The DesiredCapabilities for IE with these options.</returns>
395-
public ICapabilities ToCapabilities()
395+
public override ICapabilities ToCapabilities()
396396
{
397397
DesiredCapabilities capabilities = DesiredCapabilities.InternetExplorer();
398398
capabilities.SetCapability(CapabilityType.HasNativeEvents, this.enableNativeEvents);

dotnet/src/webdriver/Opera/OperaOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ namespace OpenQA.Selenium.Opera
5252
/// RemoteWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options.ToCapabilities());
5353
/// </code>
5454
/// </example>
55-
public class OperaOptions
55+
public class OperaOptions : DriverOptions
5656
{
5757
/// <summary>
5858
/// Gets the name of the capability used to store Opera options in
@@ -385,7 +385,7 @@ public void AddLocalStatePreference(string preferenceName, object preferenceValu
385385
/// existing value with the new value in <paramref name="capabilityValue"/>.
386386
/// Also, by default, calling this method adds capabilities to the options object passed to
387387
/// operadriver.exe.</remarks>
388-
public void AddAdditionalCapability(string capabilityName, object capabilityValue)
388+
public override void AddAdditionalCapability(string capabilityName, object capabilityValue)
389389
{
390390
// Add the capability to the OperaOptions object by default. This is to handle
391391
// the 80% case where the Operadriver team adds a new option in Operadriver.exe
@@ -448,7 +448,7 @@ public void AddAdditionalCapability(string capabilityName, object capabilityValu
448448
/// reflected in the returned capabilities.
449449
/// </summary>
450450
/// <returns>The DesiredCapabilities for Opera with these options.</returns>
451-
public ICapabilities ToCapabilities()
451+
public override ICapabilities ToCapabilities()
452452
{
453453
Dictionary<string, object> operaOptions = this.BuildOperaOptionsDictionary();
454454

dotnet/src/webdriver/PhantomJS/PhantomJSOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace OpenQA.Selenium.PhantomJS
4343
/// RemoteWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options.ToCapabilities());
4444
/// </code>
4545
/// </example>
46-
public class PhantomJSOptions
46+
public class PhantomJSOptions : DriverOptions
4747
{
4848
private Dictionary<string, object> additionalCapabilities = new Dictionary<string, object>();
4949

@@ -59,7 +59,7 @@ public class PhantomJSOptions
5959
/// </exception>
6060
/// <remarks>Calling <see cref="AddAdditionalCapability"/> where <paramref name="capabilityName"/>
6161
/// has already been added will overwrite the existing value with the new value in <paramref name="capabilityValue"/></remarks>
62-
public void AddAdditionalCapability(string capabilityName, object capabilityValue)
62+
public override void AddAdditionalCapability(string capabilityName, object capabilityValue)
6363
{
6464
if (string.IsNullOrEmpty(capabilityName))
6565
{
@@ -75,7 +75,7 @@ public void AddAdditionalCapability(string capabilityName, object capabilityValu
7575
/// reflected in the returned capabilities.
7676
/// </summary>
7777
/// <returns>The DesiredCapabilities for PhantomJS with these options.</returns>
78-
public ICapabilities ToCapabilities()
78+
public override ICapabilities ToCapabilities()
7979
{
8080
DesiredCapabilities capabilities = DesiredCapabilities.PhantomJS();
8181

dotnet/src/webdriver/Remote/RemoteWebDriver.cs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,36 @@ public class RemoteWebDriver : IWebDriver, ISearchContext, IJavaScriptExecutor,
7676
private ILocationContext locationContext;
7777
private IFileDetector fileDetector = new DefaultFileDetector();
7878

79+
/// <summary>
80+
/// Initializes a new instance of the <see cref="RemoteWebDriver"/> class. This constructor defaults proxy to http://127.0.0.1:4444/wd/hub
81+
/// </summary>
82+
/// <param name="desiredCapabilities">An <see cref="ICapabilities"/> object containing the desired capabilities of the browser.</param>
83+
public RemoteWebDriver(ICapabilities desiredCapabilities)
84+
: this(new Uri("http://127.0.0.1:4444/wd/hub"), desiredCapabilities)
85+
{
86+
}
87+
88+
/// <summary>
89+
/// Initializes a new instance of the <see cref="RemoteWebDriver"/> class
90+
/// </summary>
91+
/// <param name="remoteAddress">URI containing the address of the WebDriver remote server (e.g. http://127.0.0.1:4444/wd/hub).</param>
92+
/// <param name="desiredCapabilities">An <see cref="ICapabilities"/> object containing the desired capabilities of the browser.</param>
93+
public RemoteWebDriver(Uri remoteAddress, ICapabilities desiredCapabilities)
94+
: this(remoteAddress, desiredCapabilities, RemoteWebDriver.DefaultCommandTimeout)
95+
{
96+
}
97+
98+
/// <summary>
99+
/// Initializes a new instance of the <see cref="RemoteWebDriver"/> class using the specified remote address, desired capabilities, and command timeout.
100+
/// </summary>
101+
/// <param name="remoteAddress">URI containing the address of the WebDriver remote server (e.g. http://127.0.0.1:4444/wd/hub).</param>
102+
/// <param name="desiredCapabilities">An <see cref="ICapabilities"/> object containing the desired capabilities of the browser.</param>
103+
/// <param name="commandTimeout">The maximum amount of time to wait for each command.</param>
104+
public RemoteWebDriver(Uri remoteAddress, ICapabilities desiredCapabilities, TimeSpan commandTimeout)
105+
: this(new HttpCommandExecutor(remoteAddress, commandTimeout), desiredCapabilities)
106+
{
107+
}
108+
79109
/// <summary>
80110
/// Initializes a new instance of the <see cref="RemoteWebDriver"/> class
81111
/// </summary>
@@ -117,36 +147,6 @@ public RemoteWebDriver(ICommandExecutor commandExecutor, ICapabilities desiredCa
117147
}
118148
}
119149

120-
/// <summary>
121-
/// Initializes a new instance of the <see cref="RemoteWebDriver"/> class. This constructor defaults proxy to http://127.0.0.1:4444/wd/hub
122-
/// </summary>
123-
/// <param name="desiredCapabilities">An <see cref="ICapabilities"/> object containing the desired capabilities of the browser.</param>
124-
public RemoteWebDriver(ICapabilities desiredCapabilities)
125-
: this(new Uri("http://127.0.0.1:4444/wd/hub"), desiredCapabilities)
126-
{
127-
}
128-
129-
/// <summary>
130-
/// Initializes a new instance of the <see cref="RemoteWebDriver"/> class
131-
/// </summary>
132-
/// <param name="remoteAddress">URI containing the address of the WebDriver remote server (e.g. http://127.0.0.1:4444/wd/hub).</param>
133-
/// <param name="desiredCapabilities">An <see cref="ICapabilities"/> object containing the desired capabilities of the browser.</param>
134-
public RemoteWebDriver(Uri remoteAddress, ICapabilities desiredCapabilities)
135-
: this(remoteAddress, desiredCapabilities, RemoteWebDriver.DefaultCommandTimeout)
136-
{
137-
}
138-
139-
/// <summary>
140-
/// Initializes a new instance of the <see cref="RemoteWebDriver"/> class using the specified remote address, desired capabilities, and command timeout.
141-
/// </summary>
142-
/// <param name="remoteAddress">URI containing the address of the WebDriver remote server (e.g. http://127.0.0.1:4444/wd/hub).</param>
143-
/// <param name="desiredCapabilities">An <see cref="ICapabilities"/> object containing the desired capabilities of the browser.</param>
144-
/// <param name="commandTimeout">The maximum amount of time to wait for each command.</param>
145-
public RemoteWebDriver(Uri remoteAddress, ICapabilities desiredCapabilities, TimeSpan commandTimeout)
146-
: this(new HttpCommandExecutor(remoteAddress, commandTimeout), desiredCapabilities)
147-
{
148-
}
149-
150150
/// <summary>
151151
/// Gets or sets the URL the browser is currently displaying.
152152
/// </summary>

dotnet/src/webdriver/Safari/SafariOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace OpenQA.Selenium.Safari
4545
/// RemoteWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options.ToCapabilities());
4646
/// </code>
4747
/// </example>
48-
public class SafariOptions
48+
public class SafariOptions : DriverOptions
4949
{
5050
private int port;
5151
private bool skipExtensionInstallation;
@@ -114,7 +114,7 @@ public bool SkipExtensionInstallation
114114
/// </exception>
115115
/// <remarks>Calling <see cref="AddAdditionalCapability"/> where <paramref name="capabilityName"/>
116116
/// has already been added will overwrite the existing value with the new value in <paramref name="capabilityValue"/></remarks>
117-
public void AddAdditionalCapability(string capabilityName, object capabilityValue)
117+
public override void AddAdditionalCapability(string capabilityName, object capabilityValue)
118118
{
119119
if (string.IsNullOrEmpty(capabilityName))
120120
{
@@ -130,7 +130,7 @@ public void AddAdditionalCapability(string capabilityName, object capabilityValu
130130
/// reflected in the returned capabilities.
131131
/// </summary>
132132
/// <returns>The ICapabilities for Safari with these options.</returns>
133-
public ICapabilities ToCapabilities()
133+
public override ICapabilities ToCapabilities()
134134
{
135135
DesiredCapabilities capabilities = DesiredCapabilities.Safari();
136136
foreach (KeyValuePair<string, object> pair in this.additionalCapabilities)

dotnet/src/webdriver/WebDriver.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<Compile Include="Chrome\ChromeMobileEmulationDeviceSettings.cs" />
8585
<Compile Include="Chrome\ChromePerformanceLoggingPreferences.cs" />
8686
<Compile Include="DefaultFileDetector.cs" />
87+
<Compile Include="DriverOptions.cs" />
8788
<Compile Include="Edge\EdgeDriver.cs" />
8889
<Compile Include="Edge\EdgeDriverService.cs" />
8990
<Compile Include="Edge\EdgeOptions.cs" />

0 commit comments

Comments
 (0)