2011-09-15 53 views
4

我想从我的Java应用程序更改硒服务器的代理。当我将代理设置为Selenium服务器不使用此设置的常见方式时。 我的意思是,当我启动硒浏览器,我去一个IP检查服务(谷歌搜索“什么是我的IP”)我希望代理IP出现,而不是我的IP地址。如何从我的Java应用程序设置Selenium的代理设置?

+0

您使用的是'Selenium'类或'WebDriver'类控制在测试的浏览器? –

+0

我正在使用Selenium – edi66

回答

5

如果您使用的是WebDriver API中硒2.0,控制浏览器,你可以在开始WebDriver实例时配置浏览器使用代理,使用org.openqa.selenium.Proxy类来定义代理,和specify it as a CapabilitySelenium FAQ addresses it in a question

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

答:代理服务器配置是通过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(硒1;该API可在Selenium 2用于向后兼容),那么您需要配置Selenium Server以使用代理。这是因为Selenium Server本身被配置为浏览器的代理,因此Selenium Server必须通过代理将HTTP请求转发到Web应用程序。如果您的AUT的背后是需要身份验证,然后 你应该配置http.proxyHost,把http.proxyPort,http.proxyUser HTTP代理

代理配置

:代理服务器的详细信息可以提供为JVM startup flags to Selenium Server, as noted in the Selenium documentation和http.proxyPassword使用以下命令。

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

只有当我决定使用WebDriver时,这个答案才会帮助我...我使用Selenium RC ...实际上是阿尔法版本2.0 ..我不想很快切换到WebDriver – edi66

+0

@ edi66,在这种情况下,我会假设您使用的是Selenium Server,它可以通过'-Dhttp.proxyHost = -Dhttp.proxyPort = '标志来获得[Selenium Server将请求转发给代理而不是应用程序](http://seleniumhq.org/docs/05_selenium_rc.html#proxy-configuration)。 –

相关问题