原创文章,转载请注明出处:http://passerbyy.iteye.com/blog/1286292 作者:passer_by
1.题前话
没有发现之前,自己傻不垃圾的自己写了各个浏览器修改代理的方法,结果发现webdriver有现成,悔恨不已,希望其他同仁能够少走弯路。
本文是在Webdriver 2.12.0下面测试得到的结论
2. webdriver的maven配置
<repositories>
<repository>
<id>selenium</id>
<name>selenium</name>
<url>http://repo1.maven.org/maven2/</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("www.xxx.com");
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("www.baidu.com");
driver.close();
InternetExplorerDriver初始化后,IE对应的代理配置为:
打开上图中的代理配置文件,可以看到下述配置,具体的可以自己查阅资料:
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';
}
程序结束后,恢复原来配置
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("www.baidu.com");
driver1.close();
// 再次修改代理
DesiredCapabilities cap2 = changeProxy("localhost",80);
proxyManager.changeRegistrySettings(cap);
// 再次启动ie
WebDriver driver2 = new InternetExplorerDriver(cap);
driver2.get("www.google.com");
driver2.close();
- 大小: 37.8 KB
- 大小: 29.3 KB
分享到:
相关推荐
本书是一本从入门到精通模式的Selenium WebDriver实战经验分享书籍。全书共分为四个部分:第1部分基础篇主要讲解自动化测试相关的基础理论、WebDriver 环境安装、单元测试工具的使用方法以及 WebDrvier的入门使用...
关于Selenium WebDriver工作原理的介绍,原理相关描述
selenium RC与selenium webdriver的区别
selenium webdriver是web自动化的一本经典著作,吴老集合python java不同版本。本资源为java版本,内容较新,无论是入门还是提升都有很大帮助。
seleniumwebdriver(python) (第三版)
Acquire an understanding of the various WebDriver implementations such as Firefox Driver, IE Driver, and Chrome Driver Explore WebDriver EventListeners Design effective automation frameworks using ...
Selenium WebDriver 3 Practical Guide will walk you through the various APIs of Selenium WebDriver, which are used in automation tests, followed by a discussion of the various WebDriver implementations...
Selenium WebDriver 3.14.0 本来需要.net45 我改为.net2.0 希望喜欢的人需要。 dll 反编译,嵌入程序内。无需调用那个讨厌的dll。 绝对不骗人,骗人是小狗。架构复杂修改麻烦,各位体谅
Selenium Webdriver使用已打开的浏览器
selenium webdriver基于python源码案例,全是案例适合小白入门学习
selenium WebDriver 安装包及启动服务的一些命令 对于正在学习自动化测试的小伙伴们非常有用
selenium2.0自动化
Selenium WebDriver 学习笔记 API大全 中文API 元素 操作大全
Selenium WebDriver Recipes in C#(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Selenium 的chrome和 火狐 webdriver, windows及linux版本,附官方下载链接
自动化测试:Selenium webdriver学习笔记C#版
Selenium Webdriver自动化测试 operadriver win64位,最新版本(20150611)
对于Selenium更加详细的操作和使用,推荐一本书《selenium webdriver(python)第三版》,该书详细的讲解了webdriver的用法
支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera等。selenium 是一套完整的web应用程序测试系统,包含了测试的录制(selenium IDE),编写及运行(Selenium Remote Control)...
悠悠的selenium webdriver基于python源码案例,全是案例适合小白入门学习,现在有的资源都是只有4章