获取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%MozillaFirefoxProfiles 来获取路径

详细可以参考

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

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

实例:

package com.test.mouse;

import java.io.File;
import java.util.Iterator;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

public class MouseOperation {

    public static void main(String[] args) {
        
        //通过加载配置文件使得由WebDriver启动的firefox浏览器也能共享之前安装过的插件以及保存的密码等信息
        FirefoxProfile profile = new FirefoxProfile(new File("C:\Users\huangch\AppData\Roaming\Mozilla\Firefox\Profiles\a6xwo0b1.default"));        
        WebDriver driver = new FirefoxDriver(profile);
        
        driver.get("http://c37.yunpan.360.cn");
        driver.manage().window().maximize();
        waitTime(5000);
        
        driver.findElement(By.xpath("//*[@id='infoPanel']/a[2]")).click();
        
//可以使用下面的方法将当前页面对应的Cookies内容打印出来
        Set<Cookie> setCookie = driver.manage().getCookies();
        Iterator iterator = setCookie.iterator();
        while(iterator.hasNext()){
            Cookie c = (Cookie) iterator.next();
            System.out.println(c.getDomain()+"---"+c.getName()+"---"+c.getPath()+"---"+c.getValue());
        }


    }
    
    static public void waitTime(int time) {

        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    

}
原文地址:https://www.cnblogs.com/xinxin1994/p/7289609.html