kroe761
kroe761

Reputation: 3534

Pass SauceLabs username/accesskey into DriverOptions class

I am trying to send selenium tests to saucelabs using the DriverOptions class. According to this link, you need a sauce:options configuration, and according to this post a Dictionary will do. Here is my setup:

DriverOptions options = new ChromeOptions
{
    PlatformName = "Windows 10",
    BrowserVersion = "latest"
};
IDictionary<string, string> sauceOptions = new Dictionary<string, string>
{
    { "username", SauceUsername },
    { "accessKey", SauceAccessKey },
    { "name", TestContext.TestName },
    { "seleniumVersion", "3.11.0" }
};
options.AddAdditionalCapability("sauce:options", sauceOptions);
_driver = new RemoteWebDriver(new Uri("http://@ondemand.saucelabs.com:80/wd/hub"),
    options.ToCapabilities(), TimeSpan.FromSeconds(600));

I get a WebDriverException on the RemoteWebDriverinit, saying Misconfigured -- Sauce Labs Authentication Error. You used username 'None' and access key 'None' to authenticate. This is weird because

  1. I am given back the desired caps I used, which were:

    The following desired capabilities were received: {'browserName': 'chrome', 'browserVersion': 'latest', 'goog:chromeOptions': {'sauce:options': {'accessKey': 'XXXXXXXX-XXXX-XXXX-XXXX-XXXX163edf42', 'name': 'DriverOptionsTest', 'seleniumVersion': '3.11.0', 'username': 'kroe761'}}, 'platformName': 'Windows 10'}

The last few digits of my accesskey are right and that is my username, so clearly I sent the correct credentials

  1. If I remove the dictionary and pass the username and accesskey directly into the RemoteDriver uri (http://{SauceUsername}:{SauceAccessKey}@ondemand...) it works, However, I can't pass in any other sauce options.

Thanks!

Upvotes: 0

Views: 986

Answers (1)

JimEvans
JimEvans

Reputation: 27496

Use the AddAdditionalCapability overload that takes three arguments, not two. This tells the ChromeOptions instance to add the dictionary to the top-level of the JSON payload, rather than as part of the goog:chromeOptions property. Here is what that would look like:

// Note, you must use the specific class here, rather than the
// base class, as the base class does not have the proper method
// overload. Also, the UseSpecCompliantProtocol property is required.
ChromeOptions options = new ChromeOptions
{
    PlatformName = "Windows 10",
    BrowserVersion = "latest",
    UseSpecCompliantProtocol = true
};
Dictionary<string, object> sauceOptions = new Dictionary<string, object>
{
    { "username", SauceUsername },
    { "accessKey", SauceAccessKey },
    { "name", TestContext.TestName },
    { "seleniumVersion", "3.11.0" }
};
options.AddAdditionalCapability("sauce:options", sauceOptions, true);
_driver = new RemoteWebDriver(new Uri("http://ondemand.saucelabs.com:80/wd/hub"),
    options.ToCapabilities(), TimeSpan.FromSeconds(600));

Upvotes: 4

Related Questions