获取FirefoxProfile配置文件以及使用方法介绍

使用默认方式构建的(WebDriver)FirefoxDriver实例:

WebDriver driver = new FirefoxDriver();

这种方式下,打开的Firefox浏览器将是不带任何插件的浏览器,和初始安装一样的状态。有时在测试中需要使用到预先保留的一些信息,比如Cookie中的用户名和密码等,显然这种方式不适用了。

 

这里可以采用下面的方式来构建(WebDriver)FirefoxDriver实例:

String firefoxProfileDir = "C:\\Users\\XXXXX\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\a6xwo0b1.default";

FirefoxProfile profile = new FirefoxProfile(new File(firefoxProfileDir));

WebDriver driver = new FirefoxDriver(profile);

 

这里firefoxProfileDir的获取方式是:

通过在开始菜单中的“搜索程序和文件”中输入%APPDATA%\Mozilla\Firefox\Profiles\ 来获取路径

详细可以参考

http://www.tuicool.com/articles/NJv6Nj

https://support.mozilla.org/zh-CN/kb/用户配置文件

 

实例:

 1 package com.test.mouse;
 2 
 3 import java.io.File;
 4 import java.util.Iterator;
 5 import java.util.Set;
 6 
 7 import org.openqa.selenium.By;
 8 import org.openqa.selenium.Cookie;
 9 import org.openqa.selenium.WebDriver;
10 import org.openqa.selenium.firefox.FirefoxDriver;
11 import org.openqa.selenium.firefox.FirefoxProfile;
12 
13 public class MouseOperation {
14 
15     public static void main(String[] args) {
16         
17         //通过加载配置文件使得由WebDriver启动的firefox浏览器也能共享之前安装过的插件以及保存的密码等信息
18         FirefoxProfile profile = new FirefoxProfile(new File("C:\\Users\\huangch\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\a6xwo0b1.default"));        
19         WebDriver driver = new FirefoxDriver(profile);
20         
21         driver.get("http://c37.yunpan.360.cn");
22         driver.manage().window().maximize();
23         waitTime(5000);
24         
25         driver.findElement(By.xpath("//*[@id='infoPanel']/a[2]")).click();
26         
27 //可以使用下面的方法将当前页面对应的Cookies内容打印出来
28         Set<Cookie> setCookie = driver.manage().getCookies();
29         Iterator iterator = setCookie.iterator();
30         while(iterator.hasNext()){
31             Cookie c = (Cookie) iterator.next();
32             System.out.println(c.getDomain()+"---"+c.getName()+"---"+c.getPath()+"---"+c.getValue());
33         }
34 
35 
36     }
37     
38     static public void waitTime(int time) {
39 
40         try {
41             Thread.sleep(time);
42         } catch (InterruptedException e) {
43             // TODO Auto-generated catch block
44             e.printStackTrace();
45         }
46     }
47     
48     
49 
50 }

 

posted @ 2016-07-11 08:26  月色深潭  阅读(9584)  评论(1编辑  收藏  举报