Use of DesiredCapabilities has been deprecated

Applies to CrossBrowserTesting SaaS, last modified on January 10, 2023

Why do I get the message “Use of DesiredCapabilities has been deprecated” in C#?

The C# Selenium bindings went through some major changes in August 2018. One of these changes was to replace DesiredCapabilities with browser-specific Options classes and RemoteSessionSettings. As stated in the version 3.14 changelog:

Refactored .NET capability handling. This version introduces a large and intrusive refactor of the .NET bindings’ capability handling. It introduces a new class, `RemoteSessionSettings`, for use with creating remote sessions via ‘RemoteWebDriver’. Additionally, the ‘DesiredCapabilities’ class is now marked as deprecated and will generate a compile warning on its use.

The compile warning they are referring to looks like this:

'DesiredCapabilities' is obsolete: 'Use of DesiredCapabilities has been deprecated in favor of browser-specific Options classes'

The full text of the 3.14 release notes provide additional information on the changes, as well as suggested practices for particular use cases. In the section below, we have some examples of the correct ways to set up capabilities for CBT tests if you are using the newer bindings.

Suggested Changes

DesiredCapabilities will still work as before, so your tests should still function properly if you suppress/dismiss the warning. However, changing to the new method is the most robust and future-proof solution. You should use browser-specific options classes.

Browser-Specific Options Usage

There are options classes for several browsers that make it easy to set custom options. Here is an example using Chrome: ChromeOptions

var caps = new ChromeOptions();

caps.PlatformName = "Mac OSX 10.15";
caps.BrowserVersion = "84";
caps.AddAdditionalCapability("username", username, true);
caps.AddAdditionalCapability("password", authkey, true);

RemoteWebDriver driver = new RemoteWebDriver(new Uri("http://hub.crossbrowsertesting.com:80/wd/hub"), caps);

For other browsers, you simply replace ChromeOptions() with the browser-specific options class for that browser. Note that each browser has different options available as well– be sure to check the docs to see what is available to use.

Internet Explorer: InternetExplorerOptions

var caps = new InternetExplorerOptions();

caps.PlatformName = "Windows 10";
caps.BrowserVersion = "11";
caps.AddAdditionalCapability("username", username);
caps.AddAdditionalCapability("password", authkey);

RemoteWebDriver driver = new RemoteWebDriver(new Uri("http://hub.crossbrowsertesting.com:80/wd/hub"), caps);

Microsoft Edge: EdgeOptions

var caps = new EdgeOptions();

caps.PlatformName = "Windows 10";
caps.BrowserVersion = "83";
caps.AddAdditionalCapability("username", username);
caps.AddAdditionalCapability("password", authkey);

RemoteWebDriver driver = new RemoteWebDriver(new Uri("http://hub.crossbrowsertesting.com:80/wd/hub"), caps);

FireFox: FirefoxOptions

var caps = new FirefoxOptions();

caps.PlatformName = "Windows 10";
caps.BrowserVersion = "77";
caps.AddAdditionalCapability("username", username, true);
caps.AddAdditionalCapability("password", authkey, true);

RemoteWebDriver driver = new RemoteWebDriver(new Uri("http://hub.crossbrowsertesting.com:80/wd/hub"), caps);

Safari: SafariOptions

var caps = new SafariOptions();

caps.PlatformName = "Mac OSX 10.14";
caps.BrowserVersion = "12";
caps.AddAdditionalCapability("username", username);
caps.AddAdditionalCapability("password", authkey);

RemoteWebDriver driver = new RemoteWebDriver(new Uri("http://hub.crossbrowsertesting.com:80/wd/hub"), caps);

If you run into any trouble running your tests, then don’t hesitate to reach out to our support team.

See Also

About Selenium Testing
Selenium and C#

Highlight search results