selenium webdriver 以代理proxy方式启动firefox,ie,chrome

selenium webdriver 以署理proxy方式启动firefox,ie,chrome

本文是在Webdriver 2.12.0下面测试得到的结论


2. webdriver的maven配置

  <repositories> <repository> <id>selenium</id> <name>selenium</name> <url>;/url> </repository> </repositories> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.12.0</version> </dependency> </dependencies>




3. webdriver中firefox以代理方式启动
普通情况下,firefox的代理修改是直接修改其配置文件,即prefs.js,把对应的配置修改掉

  user_pref("network.proxy.ftp_port", 1000); user_pref("network.proxy.gopher", "10.0.0.0"); user_pref("network.proxy.gopher_port", 1000); user_pref("network.proxy.http", "10.0.0.0"); user_pref("network.proxy.http_port", 1000); user_pref("network.proxy.no_proxies_on", ""); user_pref("network.proxy.share_proxy_settings", true); user_pref("network.proxy.socks", "10.0.0.0"); user_pref("network.proxy.socks_port", 1000); user_pref("network.proxy.ssl", "10.0.0.0"); user_pref("network.proxy.ssl_port", 1000); user_pref("network.proxy.type", 1);



firefoxdriver初始化时,我们可以通过配置FirefoxProfile,来修改上面的配置,特别要注意的是localhost的配置,请看下述例子:

  String proxyIp = "localhost"; int proxyPort = 8080; FirefoxProfile profile = new FirefoxProfile(); // 使用代理 profile.setPreference("network.proxy.type", 1); // http协议代理配置 profile.setPreference("network.proxy.http", proxyIp); profile.setPreference("network.proxy.http_port", proxyPort); // 所有协议公用一种代理配置,如果单独配置,这项设置为false,再类似于http的配置 profile.setPreference("network.proxy.share_proxy_settings", true); // 对于localhost的不用代理,这里必须要配置,否则无法和webdriver通讯 profile.setPreference("network.proxy.no_proxies_on", "localhost"); // 以代理方式启动firefox FirefoxDriver ff = new FirefoxDriver(profile); ff.get(""); ff.quit();



4. webdriver中IE以代理方式启动,chrome类似
方式不同于firefox,这里是利用webdriver提供的Proxy和WindowsProxyManager来处理,这里也要特别注意localhost的处理。


处理步骤为:
1. InternetExplorerDriver初始化时,调用WindowsProxyManager的backupRegistrySettings方法保存老的代理配置
2. 调用WindowsProxyManager的changeRegistrySettings方法类修改代理配置
3. 程序运行结束后,调用WindowsProxyManager的restoreRegistrySettings来恢复到老的配置。
特别需要提醒的是第3条,我认为webdriver这里处理的不好。恢复到默认配置是在程序结束后,如果程序启动了多个InternetExplorerDriver,每个InternetExplorerDriver保存的是该InternetExplorerDriver启动时IE的配置,而程序结束是调用的shutdownhooker,同时恢复,线程的运行快慢不确定,最后是否恢复到初始配置还很难说,所以,如果同个程序只启动一个InternetExplorerDriver,使用webdriver自带的初始InternetExplorerDriver时修改代理是没有问题,如果启动多个,就要采取其他方式,可以参看本文的第5部分。

程序只启动一个InternetExplorerDriver,以代理模式启动的代码如下:

  String proxyIpAndPort= "localhost:8080"; // 代理配置 DesiredCapabilities cap = new DesiredCapabilities(); org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy(); // 配置http、ftp、ssl代理(注:当前版本只支持所有的协议公用http协议,下述代码等同于只配置http) proxy.setHttpProxy(proxyIpAndPort) .setFtpProxy(proxyIpAndPort) .setSslProxy(proxyIpAndPort); // 以下三行是为了避免localhost和selenium driver的也使用代理,务必要加,否则无法与iedriver通讯 cap.setCapability(CapabilityType.ForSeleniumServer.AVOIDING_PROXY, true); cap.setCapability(CapabilityType.ForSeleniumServer.ONLY_PROXYING_SELENIUM_TRAFFIC, true); System.setProperty("http.nonProxyHosts", "localhost"); cap.setCapability(CapabilityType.PROXY, proxy); WebDriver driver = new InternetExplorerDriver(cap); driver.get(""); driver.close();



InternetExplorerDriver初始化后,IE对应的代理配置为:
selenium webdriver 以代理proxy方式启动firefox,ie,chrome

打开上图中的代理配置文件,可以看到下述配置,具体的可以自己查阅资料:

  function FindProxyForURL(url, host) { if (shExpMatch(host, 'localhost')) { return 'DIRECT'; } if (shExpMatch(url, '*/selenium-server/*')) { return 'PROXY localhost:0; DIRECT'; } return 'PROXY 10.16.16.38:3229'; }



程序结束后,恢复原来配置
selenium webdriver 以代理proxy方式启动firefox,ie,chrome

5. ie和chrome,手动存储老的代理、修改代理、恢复代理
这里同样使用的是webdriver提供的Proxy和WindowsProxyManager,只不过是我们显示调用而已,不让webdriver帮我们处理。几个接口,我在上面也提起过,这里就直接上代码了。

  final WindowsProxyManager proxyManager = new WindowsProxyManager(true, "webdriver-ie", 0, 0); // 备份老代理配置 proxyManager.backupRegistrySettings(); // 增加hooker,jvm退出时,把代理修改为之前的。当然,这里可以自己决定什么时候恢复,比如,在每次InternetExplorerDriver关闭后掉用 new Thread() { @Override public void run() { proxyManager.restoreRegistrySettings(true); } }; // 修改代理 DesiredCapabilities cap = changeProxy("localhost",8080); proxyManager.changeRegistrySettings(cap); // 启动ie WebDriver driver1 = new InternetExplorerDriver(cap); driver1.get(""); driver1.close(); // 再次修改代理 DesiredCapabilities cap2 = changeProxy("localhost",80); proxyManager.changeRegistrySettings(cap); // 再次启动ie WebDriver driver2 = new InternetExplorerDriver(cap); driver2.get(""); driver2.close();