51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 2149|回复: 2
打印 上一主题 下一主题

[转贴] selenium webdriver学习 15 – 如何处理FirefoxProfile

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2017-7-18 14:21:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
什么是Firefox profile
要了解Firefox profile请访问这里(http://support.mozilla.org/zh-CN ... E%E6%96%87%E4%BB%B6),它详细解绍了Firefox proflie。在Firefox里,如何管理Firefox profile 请访问这里(http://support.mozilla.org/zh-CN ... E%E6%96%87%E4%BB%B6)。看完它们,相信你对Firefox profile会有所了解。好了,必备的知识准备完了,让我们来看看selenium webdriver 是怎么操作Firefox profile的吧。
设置profile中的一个preference
CODE:
  1. <font size="4">FirefoxProfile profile = new FirefoxProfile();
  2. profile.setPreference("aaa", "bbbb");
  3. WebDriver driver = new FirefoxDriver(profile);</font>
复制代码
以上代码在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)而已经存在了。我们用下面的方法:
CODE:
  1. <font size="4">ProfilesIni allProfiles = new ProfilesIni();
  2. FirefoxProfile profile = allProfiles.getProfile("WebDriver");
  3. WebDriver driver = new FirefoxDriver(profile);</font>
复制代码
如果你想启动你平时用的firefox浏览器,可以把上面”WebDriver”替换成”default”,代码如下:
CODE:
  1. <font size="4">ProfilesIni allProfiles = new ProfilesIni();
  2. FirefoxProfile profile = allProfiles.getProfile("default");
  3. WebDriver driver = new FirefoxDriver(profile);</font>
复制代码
另一种是没有在自己的firefox里面注册过的,比如从另一台机子中的firefox得到的,我们可以用下面的代码:
CODE:
  1. <font size="4">File profileDir = new File("path/to/your/profile");  
  2. FirefoxProfile profile = new FirefoxProfile(profileDir);  
  3. WebDriver driver = new FirefoxDriver(profile);</font>
复制代码
临时指定插件
有时我们需要临时让启动的firefox带一个插件,如firebug,来定位问题等。首先我们要下载这个插件的xpi安装包。剩下的就让selenium webdriver 来完成,如下:
CODE:
  1. <font size="4">File file = new File("<span style="background-color: rgb(255, 255, 255);">path/to/your/</span>firebug-1.8.1.xpi");
  2.   
  3. FirefoxProfile firefoxProfile = new FirefoxProfile();
  4. firefoxProfile.addExtension(file);
  5. firefoxProfile.setPreference("extensions.firebug.currentVersion", "1.8.1"); //避免启动画面
  6. WebDriver driver = new FirefoxDriver(firefoxProfile);</font>
复制代码
这样启动的firefox中就安装了插件firebug.
启用默认情况下被firefox禁用的功能
以本地事件例,很简单直接设置为true就可以了。
CODE:
  1. <font size="4">FirefoxProfile profile = new FirefoxProfile();
  2. profile.setEnableNativeEvents(true);
  3. WebDriver driver = new FirefoxDriver(profile);</font>
复制代码
其它设置见selenium webdriver
API(http://selenium.googlecode.com/svn/trunk/docs/api/java/index.html)中的org.openqa.selenium.firefox.FirefoxProfile.
启用firefox代理
这个更简单,直接上代码了。
CODE:
  1. <font size="4">String PROXY = "localhost:8080";//如果不是本机,localhost替换成IP地址
  2.   
  3. org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
  4. proxy.setHttpProxy(PROXY)
  5.      .setFtpProxy(PROXY)
  6.      .setSslProxy(PROXY);
  7. DesiredCapabilities cap = new DesiredCapabailities();
  8. cap.setPreference(CapabilityType.PROXY, proxy);
  9. WebDriver driver = new FirefoxDriver(cap);</font>
复制代码
over !

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-5-10 11:08 , Processed in 0.065717 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表