Skip to content

Commit 4ce57f6

Browse files
committed
Adding ability to detect spec-compliant capabilities in .NET
This commit updates the .NET `DesiredCapabilities` class to allow the language bindings to construct the correct type of payload for the new session command before sending the command to the remote end. This will remove the burden from intermediate nodes (like the Java standalone server and grid) from incorrectly parsing a driver using the legacy protocol dialect (Edge, Safari, Chrome) and throwing an error on session creation. Additionally, this commit also deprecates the static methods on the `DesiredCapability` classes for specific browsers in .NET. For over a year, the guidance for .NET users has been to use a browser-specific options class (`FirefoxOptions`, `InternetExplorerOptions`, etc.) to set specific capabilities for the driver to be instaniated. To use the options classes with the Java standalone remote server or grid, call the `ToCapabilities` method on the options object. Fixes issue #4443.
1 parent 0722dbc commit 4ce57f6

File tree

4 files changed

+84
-12
lines changed

4 files changed

+84
-12
lines changed

dotnet/src/webdriver/Remote/DesiredCapabilities.cs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ namespace OpenQA.Selenium.Remote
2626
/// Class to Create the capabilities of the browser you require for <see cref="IWebDriver"/>.
2727
/// If you wish to use default values use the static methods
2828
/// </summary>
29-
public class DesiredCapabilities : ICapabilities
29+
public class DesiredCapabilities : ICapabilities, ISpecificationCompliant
3030
{
3131
private readonly Dictionary<string, object> capabilities = new Dictionary<string, object>();
32+
private bool isSpecCompliant = true;
3233

3334
/// <summary>
3435
/// Initializes a new instance of the <see cref="DesiredCapabilities"/> class
@@ -162,6 +163,15 @@ public bool AcceptInsecureCerts
162163
}
163164
}
164165

166+
/// <summary>
167+
/// Gets or sets a value indicating whether this set of capabilities is compliant with the W3C WebDriver specification.
168+
/// </summary>
169+
bool ISpecificationCompliant.IsSpecificationCompliant
170+
{
171+
get { return this.isSpecCompliant; }
172+
set { this.isSpecCompliant = value; }
173+
}
174+
165175
/// <summary>
166176
/// Gets the internal capabilities dictionary.
167177
/// </summary>
@@ -174,6 +184,7 @@ internal Dictionary<string, object> CapabilitiesDictionary
174184
/// Method to return a new DesiredCapabilities using defaults
175185
/// </summary>
176186
/// <returns>New instance of DesiredCapabilities for use with Firefox</returns>
187+
[Obsolete("Use the FirefoxOptions class to set capabilities for use with Firefox. For use with the Java remote server or grid, use the ToCapabilites method of the FirefoxOptions class.")]
177188
public static DesiredCapabilities Firefox()
178189
{
179190
DesiredCapabilities dc = new DesiredCapabilities("firefox", string.Empty, new Platform(PlatformType.Any));
@@ -187,13 +198,16 @@ public static DesiredCapabilities Firefox()
187198
/// <returns>New instance of DesiredCapabilities for use with Firefox</returns>
188199
public static DesiredCapabilities PhantomJS()
189200
{
190-
return new DesiredCapabilities("phantomjs", string.Empty, new Platform(PlatformType.Any));
201+
DesiredCapabilities dc = new DesiredCapabilities("phantomjs", string.Empty, new Platform(PlatformType.Any));
202+
dc.isSpecCompliant = false;
203+
return dc;
191204
}
192205

193206
/// <summary>
194207
/// Method to return a new DesiredCapabilities using defaults
195208
/// </summary>
196209
/// <returns>New instance of DesiredCapabilities for use with Internet Explorer</returns>
210+
[Obsolete("Use the InternetExplorerOptions class to set capabilities for use with Internet Explorer. For use with the Java remote server or grid, use the ToCapabilites method of the InternetExplorerOptions class.")]
197211
public static DesiredCapabilities InternetExplorer()
198212
{
199213
return new DesiredCapabilities("internet explorer", string.Empty, new Platform(PlatformType.Windows));
@@ -203,9 +217,12 @@ public static DesiredCapabilities InternetExplorer()
203217
/// Method to return a new DesiredCapabilities using defaults
204218
/// </summary>
205219
/// <returns>New instance of DesiredCapabilities for use with Microsoft Edge</returns>
220+
[Obsolete("Use the EdgeOptions class to set capabilities for use with Edge. For use with the Java remote server or grid, use the ToCapabilites method of the EdgeOptions class.")]
206221
public static DesiredCapabilities Edge()
207222
{
208-
return new DesiredCapabilities("MicrosoftEdge", string.Empty, new Platform(PlatformType.Windows));
223+
DesiredCapabilities dc = new DesiredCapabilities("MicrosoftEdge", string.Empty, new Platform(PlatformType.Windows));
224+
dc.isSpecCompliant = false;
225+
return dc;
209226
}
210227

211228
/// <summary>
@@ -214,7 +231,9 @@ public static DesiredCapabilities Edge()
214231
/// <returns>New instance of DesiredCapabilities for use with HTMLUnit</returns>
215232
public static DesiredCapabilities HtmlUnit()
216233
{
217-
return new DesiredCapabilities("htmlunit", string.Empty, new Platform(PlatformType.Any));
234+
DesiredCapabilities dc = new DesiredCapabilities("htmlunit", string.Empty, new Platform(PlatformType.Any));
235+
dc.isSpecCompliant = false;
236+
return dc;
218237
}
219238

220239
/// <summary>
@@ -225,13 +244,15 @@ public static DesiredCapabilities HtmlUnitWithJavaScript()
225244
{
226245
DesiredCapabilities dc = new DesiredCapabilities("htmlunit", string.Empty, new Platform(PlatformType.Any));
227246
dc.SetCapability(CapabilityType.IsJavaScriptEnabled, true);
247+
dc.isSpecCompliant = false;
228248
return dc;
229249
}
230250

231251
/// <summary>
232252
/// Method to return a new DesiredCapabilities using defaults
233253
/// </summary>
234254
/// <returns>New instance of DesiredCapabilities for use with iPhone</returns>
255+
[Obsolete("Selenium no longer provides an iOS device driver.")]
235256
public static DesiredCapabilities IPhone()
236257
{
237258
return new DesiredCapabilities("iPhone", string.Empty, new Platform(PlatformType.Mac));
@@ -241,6 +262,7 @@ public static DesiredCapabilities IPhone()
241262
/// Method to return a new DesiredCapabilities using defaults
242263
/// </summary>
243264
/// <returns>New instance of DesiredCapabilities for use with iPad</returns>
265+
[Obsolete("Selenium no longer provides an iOS device driver.")]
244266
public static DesiredCapabilities IPad()
245267
{
246268
return new DesiredCapabilities("iPad", string.Empty, new Platform(PlatformType.Mac));
@@ -250,17 +272,20 @@ public static DesiredCapabilities IPad()
250272
/// Method to return a new DesiredCapabilities using defaults
251273
/// </summary>
252274
/// <returns>New instance of DesiredCapabilities for use with Chrome</returns>
275+
[Obsolete("Use the ChromeOptions class to set capabilities for use with Chrome. For use with the Java remote server or grid, use the ToCapabilites method of the ChromeOptions class.")]
253276
public static DesiredCapabilities Chrome()
254277
{
255278
// This is strangely inconsistent.
256279
DesiredCapabilities dc = new DesiredCapabilities("chrome", string.Empty, new Platform(PlatformType.Any));
280+
dc.isSpecCompliant = false;
257281
return dc;
258282
}
259283

260284
/// <summary>
261285
/// Method to return a new DesiredCapabilities using defaults
262286
/// </summary>
263287
/// <returns>New instance of DesiredCapabilities for use with Android</returns>
288+
[Obsolete("Selenium no longer provides an Android device driver.")]
264289
public static DesiredCapabilities Android()
265290
{
266291
return new DesiredCapabilities("android", string.Empty, new Platform(PlatformType.Android));
@@ -270,18 +295,24 @@ public static DesiredCapabilities Android()
270295
/// Method to return a new DesiredCapabilities using defaults
271296
/// </summary>
272297
/// <returns>New instance of DesiredCapabilities for use with Opera</returns>
298+
[Obsolete("Use the OperaOptions class to set capabilities for use with Opera. For use with the Java remote server or grid, use the ToCapabilites method of the OperaOptions class.")]
273299
public static DesiredCapabilities Opera()
274300
{
275-
return new DesiredCapabilities("opera", string.Empty, new Platform(PlatformType.Any));
301+
DesiredCapabilities dc = new DesiredCapabilities("opera", string.Empty, new Platform(PlatformType.Any));
302+
dc.isSpecCompliant = false;
303+
return dc;
276304
}
277305

278306
/// <summary>
279307
/// Method to return a new DesiredCapabilities using defaults
280308
/// </summary>
281309
/// <returns>New instance of DesiredCapabilities for use with Safari</returns>
310+
[Obsolete("Use the SafariOptions class to set capabilities for use with Safari. For use with the Java remote server or grid, use the ToCapabilites method of the SafariOptions class.")]
282311
public static DesiredCapabilities Safari()
283312
{
284-
return new DesiredCapabilities("safari", string.Empty, new Platform(PlatformType.Mac));
313+
DesiredCapabilities dc = new DesiredCapabilities("safari", string.Empty, new Platform(PlatformType.Mac));
314+
dc.isSpecCompliant = false;
315+
return dc;
285316
}
286317

287318
/// <summary>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// <copyright file="ISpecificationCompliant.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.Remote
25+
{
26+
/// <summary>
27+
/// Interface used to determine whether an implementing object is compliant with the W3C WebDriver specification.
28+
/// </summary>
29+
internal interface ISpecificationCompliant
30+
{
31+
/// <summary>
32+
/// Gets or sets a value indicating whether this set of capabilities is compliant with the W3C WebDriver specification.
33+
/// </summary>
34+
bool IsSpecificationCompliant { get; set; }
35+
}
36+
}

dotnet/src/webdriver/Remote/RemoteWebDriver.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,14 +1102,18 @@ protected void StartSession(ICapabilities desiredCapabilities)
11021102
Dictionary<string, object> parameters = new Dictionary<string, object>();
11031103
parameters.Add("desiredCapabilities", this.GetLegacyCapabilitiesDictionary(desiredCapabilities));
11041104

1105-
Dictionary<string, object> firstMatchCapabilities = this.GetCapabilitiesDictionary(desiredCapabilities);
1105+
ISpecificationCompliant specCompliantCapabilities = desiredCapabilities as ISpecificationCompliant;
1106+
if (specCompliantCapabilities != null && specCompliantCapabilities.IsSpecificationCompliant)
1107+
{
1108+
Dictionary<string, object> firstMatchCapabilities = this.GetCapabilitiesDictionary(desiredCapabilities);
11061109

1107-
List<object> firstMatchCapabilitiesList = new List<object>();
1108-
firstMatchCapabilitiesList.Add(firstMatchCapabilities);
1110+
List<object> firstMatchCapabilitiesList = new List<object>();
1111+
firstMatchCapabilitiesList.Add(firstMatchCapabilities);
11091112

1110-
Dictionary<string, object> specCompliantCapabilities = new Dictionary<string, object>();
1111-
specCompliantCapabilities["firstMatch"] = firstMatchCapabilitiesList;
1112-
parameters.Add("capabilities", specCompliantCapabilities);
1113+
Dictionary<string, object> specCompliantCapabilitiesDictionary = new Dictionary<string, object>();
1114+
specCompliantCapabilitiesDictionary["firstMatch"] = firstMatchCapabilitiesList;
1115+
parameters.Add("capabilities", specCompliantCapabilitiesDictionary);
1116+
}
11131117

11141118
Response response = this.Execute(DriverCommand.NewSession, parameters);
11151119

dotnet/src/webdriver/WebDriver.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@
214214
<Compile Include="Remote\ICommandExecutor.cs" />
215215
<Compile Include="Remote\ICommandServer.cs" />
216216
<Compile Include="Remote\IHasSessionId.cs" />
217+
<Compile Include="Remote\ISpecificationCompliant.cs" />
217218
<Compile Include="Remote\JsonConverters\CharArrayJsonConverter.cs" />
218219
<Compile Include="Remote\JsonConverters\ResponseValueJsonConverter.cs" />
219220
<Compile Include="Remote\LocalFileDetector.cs" />

0 commit comments

Comments
 (0)