无法使用Appium,Browserstack,Proxies和Okhttp,Java,Eclipse创建新的远程会话

发布于 2025-02-03 14:54:33 字数 2185 浏览 4 评论 0 原文

我是自动化的新手,我正在尝试在Java中编写代码以使用代理和OKHTTP设置远程连接。但是,它一直在投掷错误: org.openqa.selenium.SessionNotCreatedException:无法创建新的远程会话。原始错误:尝试过多的隧道连接:21

在代码段的最后一行中获取错误,即返回语句中的错误。 请求您的帮助以在我的代码中查找问题。以下是我的代码段:

函数如下:

 General.driver = connectViaProxy(caps);

ConnectViaproxy的实现:

public static AndroidDriver<AndroidElement> connectViaProxy(DesiredCapabilities caps) throws FileNotFoundException, IOException {
    
    String proxyHost = Config.getValue("proxy.host");
    int proxyPort = Config.getValueint("proxy.port");
    String proxyUserDomain = Config.getValue("proxy.user-domain");
    String proxyUser = Config.getValue("proxy.user");
    String proxyPassword=EncryptionUtil.passwordDecoder(Config.getValue("proxy.encrypted-password").getBytes());

 URL url;

    try {
        url = new URL("https://"+Config.getValue("BrowserStack.userName")+":"+Config.getValue("BrowserStack.accessKey")+"@hub-cloud.browserstack.com/wd/hub");
    } catch (MalformedURLException e) {
        throw new RuntimeException(e.getMessage(), e);
    }

 Authenticator proxyAuthenticator = new Authenticator()
 {
        @Override

        public Request authenticate(Route route, Response response) throws IOException {
        String credential = Credentials.basic(proxyUserDomain + "\\" + proxyUser, proxyPassword);
        return response.request().newBuilder()
        .header("Proxy-Authorization", credential)
        .build();
        }
        };
         
    okhttp3.OkHttpClient.Builder client = new okhttp3.OkHttpClient.Builder()
    .connectTimeout(60, TimeUnit.SECONDS)
    .writeTimeout(60, TimeUnit.SECONDS)
    .readTimeout(60, TimeUnit.SECONDS)
    .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)))
    .proxyAuthenticator(proxyAuthenticator);
    
    Factory factory = new MyHttpClientFactory(new org.openqa.selenium.remote.internal.OkHttpClient(client.build(), url));
    HttpCommandExecutor executor = new AppiumCommandExecutor(MobileCommand.commandRepository, url, factory);
    return new AndroidDriver<AndroidElement>(executor, caps);
   
}   

I'm very new to automation and I'm trying to write a code in Java to setup remote connection using proxy and okhttp. But, it keeps throwing error:
org.openqa.selenium.SessionNotCreatedException: Unable to create a new remote session. Original error: Too many tunnel connections attempted: 21

Getting the error on the last line of the code snippet, i.e. on the return statement.
Request your help in finding the issue in my code. Below is my code snippet:

Function is being called as below:

 General.driver = connectViaProxy(caps);

Implementation of connectViaProxy:

public static AndroidDriver<AndroidElement> connectViaProxy(DesiredCapabilities caps) throws FileNotFoundException, IOException {
    
    String proxyHost = Config.getValue("proxy.host");
    int proxyPort = Config.getValueint("proxy.port");
    String proxyUserDomain = Config.getValue("proxy.user-domain");
    String proxyUser = Config.getValue("proxy.user");
    String proxyPassword=EncryptionUtil.passwordDecoder(Config.getValue("proxy.encrypted-password").getBytes());

 URL url;

    try {
        url = new URL("https://"+Config.getValue("BrowserStack.userName")+":"+Config.getValue("BrowserStack.accessKey")+"@hub-cloud.browserstack.com/wd/hub");
    } catch (MalformedURLException e) {
        throw new RuntimeException(e.getMessage(), e);
    }

 Authenticator proxyAuthenticator = new Authenticator()
 {
        @Override

        public Request authenticate(Route route, Response response) throws IOException {
        String credential = Credentials.basic(proxyUserDomain + "\\" + proxyUser, proxyPassword);
        return response.request().newBuilder()
        .header("Proxy-Authorization", credential)
        .build();
        }
        };
         
    okhttp3.OkHttpClient.Builder client = new okhttp3.OkHttpClient.Builder()
    .connectTimeout(60, TimeUnit.SECONDS)
    .writeTimeout(60, TimeUnit.SECONDS)
    .readTimeout(60, TimeUnit.SECONDS)
    .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)))
    .proxyAuthenticator(proxyAuthenticator);
    
    Factory factory = new MyHttpClientFactory(new org.openqa.selenium.remote.internal.OkHttpClient(client.build(), url));
    HttpCommandExecutor executor = new AppiumCommandExecutor(MobileCommand.commandRepository, url, factory);
    return new AndroidDriver<AndroidElement>(executor, caps);
   
}   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱影子 2025-02-10 14:54:33

我相信您的网络中的代理不会传递您的测试脚本以建立与Browserstack的连接的第一个请求。

尝试在端口80和443的代理中使用Whitelist *.browserstack.com。

如果这不起作用,请使用本地测试并遵循以下文档中的步骤,以便代理人对BrowserStack的请求予以解决。

I believe the proxy in your network is not passing the first request made by your test script to establish connection to BrowserStack.

Try to whitelist *.browserstack.com in your proxy for ports 80 and 443.

If that does not work, use local testing and follow the steps from the below documentation so that the request to BrowserStack is by-passed by the proxy.

https://www.browserstack.com/docs/automate/selenium/test-behind-proxy/configure-settings

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文