Saucelabs Selenium 不使用 RemoteWebDriver (C#) 的代理详细信息

Saucelabs Selenium not using proxy details for RemoteWebDriver (C#)

我正在使用 Saucelabs Selenium 实现来跨多个设备和平台自动化测试。

使用 SauceLabs 中的演示代码(如下)在代理后不起作用。

我已经尝试将代理详细信息添加到 DesiredCapibilities,但这似乎没有任何作用

[TestMethod]
public void TestSauceLabs()
{
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.SetCapability("browserName", "Safari");
    caps.SetCapability("platform", "macOS 10.13");
    caps.SetCapability("version", "11.1");
    caps.SetCapability("username", _sauceUserName);
    caps.SetCapability("accessKey", _sauceAccessKey);
    caps.SetCapability("name", _name);

    var tags = new List<string> {"demoTest", "sauceDemo"};
    caps.SetCapability("tags", tags);
    caps.SetCapability("maxDuration", 3600);
    caps.SetCapability("commandTimeout", 600);
    caps.SetCapability("idleTimeout", 1000);
    caps.SetCapability("build", "SauceDemo");

    /****************************************
     * Edited demo code here
     * Added proxy config to DesiredCapabilities **
     */
    var proxy = new Proxy
    {
        IsAutoDetect = false,
        HttpProxy = $"{_proxyScheme}://{_proxyHost}:{_proxyPort}",
        SslProxy = $"{_proxyScheme}://{_proxyHost}:{_proxyPort}",
        FtpProxy = $"{_proxyScheme}://{_proxyHost}:{_proxyPort}"
    };
    caps.SetCapability(CapabilityType.Proxy, proxy);
    /*
     *****************************************/

    var uri = new Uri("https://ondemand.eu-central-1.saucelabs.com/wd/hub");
    _driver = new RemoteWebDriver(uri,
        caps, TimeSpan.FromSeconds(600));

    _javascriptExecutor = ((IJavaScriptExecutor) _driver);

    _javascriptExecutor.ExecuteScript("sauce:context=Open SauceDemo.com");
    _driver.Navigate().GoToUrl(_url);

    _javascriptExecutor.ExecuteScript("sauce:context=Sleep for 10000ms");
    Thread.Sleep(10000);
    Assert.IsTrue(true);

    var passed = true;
    _javascriptExecutor.ExecuteScript("sauce:job-result=" + (passed ? "passed" : "failed"));
    _driver?.Quit();
}

发现解决方案是在代理后面使用 RemoteWebDriver 时使用 HttpCommandExecutor。

这是我的示例代码:


[TestMethod]
public void TestSauceLabs_Chrome()
{
    var remoteOptions = new Dictionary<string, object>
    {
        { "username", _sauceUserName },
        { "accessKey", _sauceAccessKey },
        { "name", _name },
        { "maxDuration", 3600 },
        { "commandTimeout", 600 },
        { "idleTimeout", 1000 }
    };

    var options = new ChromeOptions()
    {
        PlatformName = "Windows 10",
        BrowserVersion = "latest"
    };

    //Remote options need to be global
    options.AddAdditionalCapability("sauce:options", remoteOptions, true);

    var caps = options.ToCapabilities();

    /*
     Using the HttpCommandExecutor persists the proxy details 
     and allows you to pass in credentials if required
     */
    var executor = new HttpCommandExecutor(
        new Uri("https://ondemand.eu-central-1.saucelabs.com/wd/hub"), 
        TimeSpan.FromSeconds(600))
    {
        Proxy = GenerateProxy()
    };

    _driver = new RemoteWebDriver(executor, caps);

    _javascriptExecutor = ((IJavaScriptExecutor)_driver);

    _javascriptExecutor.ExecuteScript($"sauce:context=Open {_url}");
    _driver.Navigate().GoToUrl(_url);

    _javascriptExecutor.ExecuteScript("sauce:context=Sleep for 10000ms");
    Thread.Sleep(10000);
    Assert.IsTrue(true);

    var passed = true;
    _javascriptExecutor.ExecuteScript("sauce:job-result=" + (passed ? "passed" : "failed"));
    _driver?.Quit();
}


public WebProxy GenerateProxy()
{
    var proxy = new WebProxy
    {
        Address = new Uri($"{_proxyScheme}://{_proxyHost}:{_proxyPort}"),
        BypassProxyOnLocal = false,
        UseDefaultCredentials = _networkCredential != null,
        Credentials = _networkCredential
    };

    return proxy;
}