【问题标题】:How to set from my Java application the proxy settings for Selenium?如何从我的 Java 应用程序中设置 Selenium 的代理设置?
【发布时间】:2011-09-15 10:57:46
【问题描述】:

我想从我的 Java 应用程序中更改 selenium 服务器的代理。当我以 Selenium 服务器不使用此设置的常见方式设置代理时。 我的意思是当我启动 selenium 浏览器并访问 IP 检查服务(在谷歌搜索“我的 ip 是什么”)时,我希望出现代理 IP 而不是我的 IP 地址。

【问题讨论】:

  • 您是使用Selenium 类还是WebDriver 类来控制测试中的浏览器?

标签: java selenium


【解决方案1】:

如果你在 Selenium 2.0 中使用WebDriver API 来控制浏览器,你可以配置浏览器使用代理,使用org.openqa.selenium.Proxy 类来定义代理,在启动@ 时使用specify it as a Capability 987654329@ 实例。 Selenium FAQ addresses it in a question

问:我需要使用代理。我该如何配置?

A:代理配置是通过 org.openqa.selenium.Proxy 类完成的 像这样:

Proxy proxy = new Proxy();
proxy.setProxyAutoconfigUrl("http://youdomain/config");

// We use firefox as an example here.
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(CapabilityType.PROXY, proxy);

// You could use any webdriver implementation here
WebDriver driver = new FirefoxDriver(capabilities);

如果您使用的是 Selenium RC(Selenium 1;为了向后兼容,Selenium 2 中提供了 API),那么您需要配置 Selenium Server 以使用代理。这是因为 Selenium Server 本身被配置为浏览器的代理,因此 Selenium Server 必须通过代理将 HTTP 请求转发到 Web 应用程序。代理详细信息可以提供为JVM startup flags to Selenium Server, as noted in the Selenium documentation

代理配置

如果您的 AUT 位于需要身份验证的 HTTP 代理之后,那么 你应该配置 http.proxyHost、http.proxyPort、http.proxyUser 和 http.proxyPassword 使用以下命令。

$ java -jar selenium-server-standalone-<version-number>.jar -Dhttp.proxyHost=proxy.com -Dhttp.proxyPort=8080 -Dhttp.proxyUser=username -Dhttp.proxyPassword=password

【讨论】:

  • 只有当我决定使用 WebDriver 时,这个答案才会对我有所帮助......我正在使用 Selenium RC......实际上是 alfa 版本 2.0..我不想很快切换到 WebDriver
  • @edi66,在这种情况下,我假设您使用的是 Selenium Server,它可以提供-Dhttp.proxyHost=&lt;proxyhostname&gt; -Dhttp.proxyPort=&lt;proxyPort&gt; 标志来获取Selenium Server to forward the requests to the proxy instead of the application
猜你喜欢
  • 1970-01-01
  • 2015-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-29
  • 1970-01-01
  • 2015-11-10
相关资源
最近更新 更多