将 SauceLabs username/accesskey 传递到 DriverOptions class

Pass SauceLabs username/accesskey into DriverOptions class

我正在尝试使用 DriverOptions class 将 selenium 测试发送到 saucelabs。根据 this link, you need a sauce:options configuration, and according to this post 字典就可以了。这是我的设置:

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));

我在 RemoteWebDriverinit 上得到一个 WebDriverException,说 Misconfigured -- Sauce Labs Authentication Error. You used username 'None' and access key 'None' to authenticate。这很奇怪,因为

  1. 我得到了我使用的想要的上限,它们是:

    已收到以下所需功能: {'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'}

我的访问密钥的最后几位是正确的,那是我的用户名,很明显我发送了正确的凭据

  1. 如果我删除字典并将用户名和 accesskey 直接传递到 RemoteDriver uri (http://{SauceUsername}:{SauceAccessKey}@ondemand...) 它会起作用,但是,我不能传递任何其他 sauce 选项。

谢谢!

使用带有三个而不是两个参数的 AddAdditionalCapability 重载。这告诉 ChromeOptions 实例将字典添加到 JSON 负载的顶层,而不是作为 goog:chromeOptions 属性 的一部分。这是它的样子:

// 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));