java 在企业代理背后使用Selenium RemoteWebDriver

ffvjumwh  于 2023-03-28  发布在  Java
关注(0)|答案(5)|浏览(145)

如何通过RemoteWebDriver从企业代理后面连接到Selenium网格(如BrowserStack)?
被测应用程序位于代理外部,可从BrowserStack自由访问。
这个Using Selenium RemoteWebDriver behind corporate proxy (Java) stackoverflow问题问了同样的问题,但我无法遵循公认的答案。

zpjtge22

zpjtge221#

我设法让一些工作的基础上接受的答案在链接的stackoverflow问题,这是我的实现的情况下,其他任何人都停留在同样的问题:

示例

import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.NTCredentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.openqa.selenium.remote.CommandInfo;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.HttpCommandExecutor;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.http.HttpClient.Factory;

public class Example {
    public RemoteWebDriver connectViaProxy(DesiredCapabilities caps) {
        String proxyHost = "?";
        int proxyPort = 8080;
        String proxyUserDomain = "?";
        String proxyUser = "?";
        String proxyPassword = "?";

        URL url;

        try {
            url = new URL("http://bsuser:bspassword@hub.browserstack.com/wd/hub");
        } catch (MalformedURLException e) {
            throw new RuntimeException(e.getMessage(), e);
        }

        HttpClientBuilder builder = HttpClientBuilder.create();

        HttpHost proxy = new HttpHost(proxyHost, proxyPort);

        CredentialsProvider credsProvider = new BasicCredentialsProvider();

        credsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), new NTCredentials(proxyUser, proxyPassword, getWorkstation(), proxyUserDomain));

        if (url.getUserInfo() != null && !url.getUserInfo().isEmpty()) {
            credsProvider.setCredentials(new AuthScope(url.getHost(), (url.getPort() > 0 ? url.getPort() : url.getDefaultPort())), new UsernamePasswordCredentials(url.getUserInfo()));
        }

        builder.setProxy(proxy);
        builder.setDefaultCredentialsProvider(credsProvider);

        Factory factory = new MyHttpClientFactory(builder);

        HttpCommandExecutor executor = new HttpCommandExecutor(new HashMap<String, CommandInfo>(), url, factory);

        return new RemoteWebDriver(executor, caps);
    }

    private String getWorkstation() {
        Map<String, String> env = System.getenv();

        if (env.containsKey("COMPUTERNAME")) {
            // Windows
            return env.get("COMPUTERNAME");         
        } else if (env.containsKey("HOSTNAME")) {
            // Unix/Linux/MacOS
            return env.get("HOSTNAME");
        } else {
            // From DNS
            try
            {
                return InetAddress.getLocalHost().getHostName();
            }
            catch (UnknownHostException ex)
            {
                return "Unknown";
            }
        }
    }
}

MyHttpClientFactory

import java.net.URL;

import org.apache.http.impl.client.HttpClientBuilder;
import org.openqa.selenium.remote.internal.ApacheHttpClient;

public class MyHttpClientFactory implements org.openqa.selenium.remote.http.HttpClient.Factory {
    final HttpClientBuilder builder; 

    public MyHttpClientFactory(HttpClientBuilder builder) {
        this.builder = builder;
    }

    @Override
    public org.openqa.selenium.remote.http.HttpClient createClient(URL url) {
        return new ApacheHttpClient(builder.build(), url);
    }
}
vyswwuz2

vyswwuz22#

添加到Andrew上面的答案中,要使此操作与Appium一起工作,请更改

HttpCommandExecutor executor = new HttpCommandExecutor(new HashMap<String, CommandInfo>(), url, factory);

HttpCommandExecutor executor = new HttpCommandExecutor(MobileCommand.commandRepository, url, factory);
gtlvzcf8

gtlvzcf83#

我已经稍微修改了Andrew Sumner的解决方案,并采取了一些措施,以防像我这样的人想通过Fiddler快速引导他们的WebDriver流量来查看流量。

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

import org.apache.http.HttpHost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.openqa.selenium.ie.InternetExplorerOptions;
import org.openqa.selenium.remote.CommandInfo;
import org.openqa.selenium.remote.HttpCommandExecutor;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpClient.Factory;
import org.openqa.selenium.remote.internal.ApacheHttpClient;

public class ProxiedRemoteExample {
    private static final String PROXY_HOST = "localhost";
    private static final int PROXY_PORT = 8888;

    public ProxiedRemoteExample() throws MalformedURLException {
        InternetExplorerOptions ieOptions = new InternetExplorerOptions();
        RemoteWebDriver driver = new RemoteWebDriver(new HttpCommandExecutor(new HashMap<String, CommandInfo>(),
                new URL("http://localhost:5555/"), new Factory() {
                    private HttpClientBuilder builder;
                    {
                        builder = HttpClientBuilder.create();
                        builder.setProxy(new HttpHost(PROXY_HOST, PROXY_PORT));
                    }
                    @Override
                    public HttpClient createClient(URL url) {
                        return new ApacheHttpClient(builder.build(), url);
                    }

                }), ieOptions);
    }

}
rbl8hiat

rbl8hiat4#

对于org.seleniumhq.selenium:selenium-java:4.0.0-beta-3,我必须以以下方式应用代理设置:

  • 配置异步http客户端以使用代理设置
  • org/asynchttpclient/config文件夹下创建ahc.properties文件
  • 文件内容:org.asynchttpclient.useProxyProperties = true
  • 配置JVM代理属性
  • System.getProperties().setProperty(“http.proxyHost”,“yourProxyHost”)
  • System.getProperties().setProperty(“http.proxyPort”,“yourProxyPort”)
mspsb9vt

mspsb9vt5#

在v4.0.0-alpha-7之后,HTTP客户端的okhttp实现已经被删除。但是在重读Andrew的答案后,发现了一种简单的设置代理的方法。

var url = new URL("Your grid endpoint like browserstack or saucelabs");
var proxy = new java.net.Proxy(java.net.Proxy.Type.HTTP,
                    new InetSocketAddress("Your proxy", 8080));
var executor = new HttpCommandExecutor(ClientConfig.defaultConfig().baseUrl(url).proxy(proxy));
var driver = new RemoteWebDriver(executor, browserOptions);

相关问题