这一节主要涉及 selenium webdriver处理Firefox profile的一些知识。
什么是Firefox profile
要了解Firefox profile请访问这里,它详细解绍了Firefox proflie。在Firefox里,如何管理Firefox profile 请访问这里。看完它们,相信你对Firefox profile会有所了解。好了,必备的知识准备完了,让我们来看看selenium webdriver 是怎么操作Firefox profile的吧。
设置profile中的一个preference
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("aaa", "bbbb");
WebDriver driver = new FirefoxDriver(profile);
以上代码在Firefox Profile文件中设置一个名aaa,值为bbb的preference.(ps:这个preference只是一个举例,没有任何意义。要看firefox profile有哪些preference,可以在firefox浏览器地址栏中输入:about:config). 代码运行后,在firefox浏览器地址栏中输入:about:config,可以看到它。
启用已经存在的profile
首先来了解一下为什么要已经存在的profile,其中一个原因是已经存在的profile里面保存有cookie等信息,可以保持用户的登录状态。
启动已经存在的profile,因profile不同而有两种方法。一种是如果这个profile使用firefox配置管理器(Firefox's profile manager)而已经存在了。我们用下面的方法:
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("WebDriver");
WebDriver driver = new FirefoxDriver(profile);
如果你想启动你平时用的firefox浏览器,可以把上面"WebDriver"替换成"default",代码如下:
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("default");
WebDriver driver = new FirefoxDriver(profile);
另一种是没有在自己的firefox里面注册过的,比如从另一台机子中的firefox得到的,我们可以用下面的代码:
File profileDir = new File("path/to/your/profile");
FirefoxProfile profile = new FirefoxProfile(profileDir);
WebDriver driver = new FirefoxDriver(profile);
临时指定插件
有时我们需要临时让启动的firefox带一个插件,如firebug,来定位问题等。首先我们要下载这个插件的xpi安装包。剩下的就让selenium webdriver 来完成,如下:
File file = new File("path/to/your/firebug-1.8.1.xpi");
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.addExtension(file);
firefoxProfile.setPreference("extensions.firebug.currentVersion", "1.8.1"); //避免启动画面
WebDriver driver = new FirefoxDriver(firefoxProfile);
这样启动的firefox中就安装了插件firebug.
启用默认情况下被firefox禁用的功能
以本地事件例,很简单直接设置为true就可以了。
FirefoxProfile profile = new FirefoxProfile();
profile.setEnableNativeEvents(true);
WebDriver driver = new FirefoxDriver(profile);
其它设置见selenium webdriver API中的org.openqa.selenium.firefox.FirefoxProfile.
启用firefox代理
这个更简单,直接上代码了。
String PROXY = "localhost:8080";//如果不是本机,localhost替换成IP地址
org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy(PROXY)
.setFtpProxy(PROXY)
.setSslProxy(PROXY);
DesiredCapabilities cap = new DesiredCapabailities();
cap.setPreference(CapabilityType.PROXY, proxy);
WebDriver driver = new FirefoxDriver(cap);
over !
分享到:
相关推荐
WebtestRecorder插件---Selenium Webdriver脚本录制工具--For Java
1.1 selenium webdriver学习(一)------------快速开始 . . . . . . . . . ....1.2 selenium webdriver...1.12 selenium webdriver学习(十二)------------如何利用selenium-webdriver截图 . . . . . . . . . . . .38
博客“ruby+selenium-webdriver测试--第一个例子源代码”
博客“ruby+selenium-webdriver测试--多个测试用例”源代码
selenium-webdriver原创selenium-webdriver原创selenium-webdriver原创selenium-webdriver原创
selenium-server-standalone-2.40.0.jar 、selenium-java-2.40.0-srcs.jar 、selenium-java-2.40.0.jar
Selenium.WebDriver.4.0.0-alpha02.nupkg,Selenium.WebDriver 4.0,Selenium is a set of different software tools each with a different approach to supporting browser automation. These tools are highly ...
selenium-server-standalone-4.0.0-alpha-2.zip
selenium-server-standalone-2.53.1.jar用于支持selenium webdriver的开发,也可用于分布式测试连接
selenium-api-4.0.0- alpha- 6.jar 文件下载
Selenium WebDriver 学习笔记 API大全 中文API 元素 操作大全
本资源是selenium-webdriver-practical-guide-2014.pdf文档的源代码
最新版selenium-java,selenium-server-standalone-3.141.0.jar
selenium-server-standalone-2.44.0, selenium最新服务器,
selenium-java-2.44.0.jar selenium-java-2.44.0-srcs.jar selenium-server-standalone-2.44.0.jar
ruby2.0.0+selenium-webdriver+watir-webdriver+rails离线备份,设置环境变量后可使用(未测试)
注意selenium-webdriver只支持1.8.7以上的ruby版本; 使用gem安装selenium-webdriver;打开命令行,输入下列代码完成安装。注意,如果你的开发环境需要http proxy的话,请注意在gem命令中加入--http_proxy参数; ...
Selenium WebDriver Practical Guide will guide you through the various APIs of WebDriver which should be used in automation tests, followed by a discussion of the various WebDriver implementations ...