【问题标题】:Pass SauceLabs username/accesskey into DriverOptions class将 SauceLabs 用户名/访问密钥传递给 DriverOptions 类
【发布时间】:2019-06-29 14:23:49
【问题描述】:

我正在尝试使用 DriverOptions 类将硒测试发送到酱实验室。根据this 链接,您需要一个酱汁:选项配置,根据this 发布一个字典就可以了。这是我的设置:

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', '浏览器版本':'最新的', 'goog:chromeOptions': {'sauce:options': {'accessKey': 'XXXXXXXX-XXXX-XXXX-XXXX-XXXX163edf42', 'name': 'DriverOptionsTest', 'seleniumVersion': '3.11.0', '用户名': 'kroe761'}}, “平台名称”:“Windows 10”}

我的 accesskey 的最后几位数字是正确的,那是我的用户名,所以很明显我发送了正确的凭据

  1. 如果我删除字典并将用户名和访问密钥直接传递到 RemoteDriver uri (http://{SauceUsername}:{SauceAccessKey}@ondemand...),它可以工作,但是,我不能传递任何其他酱汁选项。

谢谢!

【问题讨论】:

    标签: c# selenium-webdriver saucelabs


    【解决方案1】:

    使用带有三个参数而不是两个参数的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));
    

    【讨论】:

    • 抛出异常ChromeOptionsTest threw exception: System.InvalidOperationException: session not created: Missing or invalid capabilities
    • @kroe761 我已经更新了代码 sn-p。你不应该使用IDictionary&lt;string, string&gt;,而应该使用Dictionary&lt;string, object&gt;。尽管如此,注意到发送到 SauceLabs 远程端的有效负载会很有趣。除了您在异常消息中列出的内容之外,还有其他内容吗?我很确定上面代码的语法是正确的。
    • 感谢您的帮助 - 我复制了您的代码,但它抛出了同样的错误。这是完整的Message: Test method SauceDemo.UnitTest.DriverOptionsTest_Chrome threw exception: System.InvalidOperationException: session not created: Missing or invalid capabilities (Driver info: chromedriver=2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387),platform=Windows NT 10.0.10586 x86_64) (SessionNotCreated)........
    • .........当我调试 options 实例时,我得到这个:{{ "browserName": "chrome", "browserVersion": "latest", "platformName": "Windows 10", "goog:chromeOptions": {}, "sauce:options": { "username": "kroe761", "accessKey": "--------", "name": "DriverOptionsTest_Chrome", "seleniumVersion": "3.11.0" } }}
    • 我看到断开连接的位置。您需要在选项类中设置UseSpecCompliantProtocol 属性。 github.com/saucelabs-training/w3c-examples/blob/master/csharp/…
    猜你喜欢
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 2012-09-28
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    相关资源
    最近更新 更多