獲取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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM