将SauceLabs用户名/访问键传递给DriverOptions类

时间:2019-02-05 17:28:32

标签: c# selenium-webdriver saucelabs

我正在尝试使用DriverOptions类将硒测试发送到平底锅。根据{{​​3}}链接,您需要一个sauce:options配置,并且根据this发布一个Dictionary即可。这是我的设置:

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

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

  1. 我得到了我想要的上限,即:

    已收到以下所需功能: {'browserName':'chrome',  'browserVersion':'最新',  'goog:chromeOptions':{'sauce:options':{'accessKey':'XXXXXXXX-XXXX-XXXX-XXXX-XXXX163edf42',                                           '名称':'DriverOptionsTest',                                           'seleniumVersion':'3.11.0',                                           'username':'kroe761'}}, 'platformName':'Windows 10'}

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

  1. 如果我删除字典并将用户名和访问密钥直接传递到RemoteDriver uri(http://{SauceUsername}:{SauceAccessKey}@ondemand ...),它将起作用,但是,我无法传递任何其他调味料选项。

谢谢!

1 个答案:

答案 0 :(得分:3)

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