Passing both DesiredCapabilities and ChromeOptions to RemoteWebDriver version 2.40 or later

2,297 views
Skip to first unread message

dennish

unread,
Oct 21, 2014, 6:36:14 PM10/21/14
to seleniu...@googlegroups.com
Hello,

I am trying to upgrade our .NET Selenium library to the latest version (2.43.1) from 2.39, which we use to run tests in various browsers on a local Selenium Grid installation. The grid is running Selenium Server 2.43.1 and the appropriate node is running ChromeDriver 2.11.298604. All are running on Windows platforms.

The behavior of the DesiredCapabilities class in .NET changed in 2.40, in that you can no longer use the code snippet below to launch Chrome on the Selenium Grid. It will fail with the following error returned from the node:

unknown error: cannot parse capability: chromeOptions
from unknown error: unrecognized chrome option: Arguments
 
Through my own debugging, I've found that if I disable the method that sets the BrowserName capability, the error will no longer happen:

//capabilities.SetCapability(CapabilityType.BrowserName, "chrome");

However, since the browser name is not set in this case for Chrome, the Hub forwards the request to a random Node and a random browser is started. The Grid nodes are configured with capabilities specifying exactly what browser, browser version (in the case of IE), and operating system each is responsible for. As such, the capabilities have to be set exactly as in the code snippet below, or the initialization request may be rejected by the hub since it doesn't match any connected node capabilities, or it may initialize on the wrong node (as in this case).

I have spent a lot of time searching for a solution to this problem, to no avail. I've also been unable to locate any documentation detailing how to use DesiredCapabilities and/or ChromeOptions with RemoteWebDriver and the Selenium Grid in version 2.40 or later. The only remotely-promising information that keeps coming up is here https://code.google.com/p/selenium/issues/detail?id=7043, but the solution suggested there by James only works when using the ChromeOptions class itself.

Code snippet that works perfectly in Selenium 2.39 or earlier:

public SeleniumDriver(Platform platform)
{
    DesiredCapabilities capabilities = new DesiredCapabilities();

    switch (platform)
    {
        case Platform.ChromeWin7:
            {
                ChromeOptions options = new ChromeOptions();

                // Removes the "You are using an unsupported command-line flag: --ignore-certificate-errors. Stability and security will suffer." error
                options.AddArgument("test-type");

                capabilities.SetCapability(ChromeOptions.Capability, options);
                capabilities.SetCapability(CapabilityType.BrowserName, "chrome");
                capabilities.SetCapability(CapabilityType.Version, String.Empty);
                capabilities.SetCapability(CapabilityType.Platform, "WINDOWS");

                break;
            }
        case Platform.FirefoxWin7:
            {
                capabilities.SetCapability(CapabilityType.BrowserName, "firefox");
                capabilities.SetCapability(CapabilityType.Version, String.Empty);
                capabilities.SetCapability(CapabilityType.Platform, "WINDOWS");

                break;
            }
        case Platform.IE10Win8:
            {
                capabilities.SetCapability(CapabilityType.BrowserName, "internet explorer");
                capabilities.SetCapability(CapabilityType.Version, "10");
                capabilities.SetCapability(CapabilityType.Platform, "WIN8");

                break;
            }

       // and so on...
    }

    Driver = new RemoteWebDriverScreenshot(new Uri(Configuration.General.SeleniumHubUrl), capabilities, TimeSpan.FromMinutes(5));
    Driver.Manage().Window.Maximize();
}

Is there a way to specify both DesiredCapabilities and ChromeOptions with RemoteWebDriver version 2.40 or later?

Any help would be greatly appreciated. 

dennish

unread,
Oct 29, 2014, 3:31:55 PM10/29/14
to seleniu...@googlegroups.com
Just after I posted this, James replied with a solution here: https://code.google.com/p/selenium/issues/detail?id=7043#c11

It involves casting the object returned from the ToCapabilities() method to DesiredCapabilities.

So, in my case, the working solution is:

public SeleniumDriver(Platform platform)
{
    DesiredCapabilities capabilities = new DesiredCapabilities();

    switch (platform)
    {
        case Platform.ChromeWin7:
            {
                ChromeOptions options = new ChromeOptions();

                // Removes the "You are using an unsupported command-line flag: --ignore-certificate-errors. Stability and security will suffer." error
                options.AddArgument("test-type");

                // The fix
                capabilities = options.ToCapabilities() as DesiredCapabilities;
                
                capabilities.SetCapability(CapabilityType.BrowserName, "chrome");
                capabilities.SetCapability(CapabilityType.Version, String.Empty);
                capabilities.SetCapability(CapabilityType.Platform, "WINDOWS");

                break;
            }

Doodlekana

unread,
Feb 6, 2015, 4:34:47 PM2/6/15
to seleniu...@googlegroups.com
Thanks Dennish, It works great. I read that James' post and got little lost where I had this line

capabilities.SetCapability(ChromeOptions.Capability, options.ToCapabilities());

Earlier in this code I already had  
var capabilities = options.ToCapabilities() as DesiredCapabilities;

So I guess I did not need to make option.ToCapabilities() again.
Reply all
Reply to author
Forward
0 new messages