栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在公司代理后面使用Selenium RemoteWebDriver

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在公司代理后面使用Selenium RemoteWebDriver

我设法根据链接的问题中的可接受答案得到了一些解决方案,如果有人遇到相同问题,这是我的实现:

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/508730.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号