在公司代理(Java)后面使用 Selenium RemoteWebDriver [英] Using Selenium RemoteWebDriver behind corporate proxy (Java)
问题描述
我正在尝试在一些远程自动化服务(Sauce Labs、Browserstack 等)上运行 Selenium 测试,但遇到了通过我的公司防火墙攻击其 API 的问题.
I'm trying to run Selenium tests on a few remote automation services (Sauce Labs, Browserstack, etc) and run into issues hitting their API through my corporate firewall.
请注意,我要测试的应用不位于此防火墙之后,它可以公开访问.
Just a note, the app I'm trying to test is not behind this firewall, it is publicly accessible.
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability("platform", "Windows 7");
caps.setCapability("version", "9.0");
caps.setCapability("idleTimeout", "300");
caps.setCapability("name", "Invitation Tests");
driver = new RemoteWebDriver(new URL("https://user:key@saucelabs.com), caps);
问题似乎是 Selenium 的管道将 url 中的 user:key 解释为代理凭据,因此它永远不会离开我们的网络.有没有什么具体的技巧来配置这个?它似乎在幕后使用 Apache HttpClient.
The issue seems to be the plumbing of Selenium interprets the user:key in the url as proxy credentials, so it never leaves our network. Are there any specific tricks to configuring this? It seems to use Apache HttpClient under the hood.
我认为我们正在使用 NTLM 代理,它似乎使用基本身份验证.这可能是这里的相同问题:https://code.google.com/p/selenium/issues/detail?id=7286
I think we're using an NTLM proxy, it seems to use basic auth. It might be the same issue from here: https://code.google.com/p/selenium/issues/detail?id=7286
推荐答案
您链接到的 Google 代码问题确实似乎是原因.请注意,问题已解决,因此您现在可以在创建 RemoteWebDriver 时注入自己的 CommandExecutor 实现.
The Google Code issue that you linked to does indeed appear to be the cause. Note that the issue has been resolved, so you can now inject your own implementation of CommandExecutor when creating a RemoteWebDriver.
具体来说,你可能会做这样的事情:
Specifically, you would probably do something like this:
- 编写一个
org.openqa.selenium.remote.http.HttpClient.Factory
的自定义实现,其行为类似于 https://github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/openqa/selenium/remote/internal/ApacheHttpClient.java,但允许您注入HttpClient
实例(或HttpClientFactory
实例,如果您想将其子类化).这是一个非常简单的界面,一个简单的实现可以复制,所以这应该很容易. - 使用不同主机的不同凭据创建
org.apache.http.impl.client.BasicCredentialsProvider
的实例(请参阅org.apache.http.auth.AuthScope
详情). - 使用
org.apache.http.impl.HttpClientBuilder
与您的凭据提供程序构建客户端. - 构造一个
HttpCommandExecutor
实例,传入自定义工厂的实例并注入客户端. - 构造一个
RemoteWebDriver
的实例,传入命令执行器.
- Write a custom implementation of
org.openqa.selenium.remote.http.HttpClient.Factory
that behaves similarly to the one at https://github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/openqa/selenium/remote/internal/ApacheHttpClient.java, but allows you to inject theHttpClient
instance (orHttpClientFactory
instance, if you want to subclass that). It's quite a simple interface, and a simple implementation to copy, so this should be easy. - Create an instance of
org.apache.http.impl.client.BasicCredentialsProvider
with different credentials for the different hosts (seeorg.apache.http.auth.AuthScope
for details). - Use
org.apache.http.impl.HttpClientBuilder
to construct a client with your credential provider. - Construct an instance of
HttpCommandExecutor
, passing in an instance of your custom factory and injecting your client. - Construct an instance of
RemoteWebDriver
, passing in the command executor.
这篇关于在公司代理(Java)后面使用 Selenium RemoteWebDriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!