WebDriver开启firefox时带用户配置

在使用selenium进行web自动化测试时,大家会发现,打开的页面中没有firefox原来的配置,比如:下载的插件firebug,收藏的网页等等
那么如何在启动网页后,仍保留这些个人配置信息呢?

  1. 打开Windows的cmd(按下Windows+R可快捷打开)
  2. cd到firefox.exe所在的路径,如下图
    WebDriver开启firefox时带用户配置_第1张图片
  3. 输入指令:firefox.exe -ProfileManager -no-remote,如下图
    WebDriver开启firefox时带用户配置_第2张图片
    4.其中default就是原来用户的配置信息,下面的Webdriver是我自己新建的用户配置。
    5.以下是测试代码
package seleniumAuto;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class FirefoxProfile {
    WebDriver driver;
    String baseUrl;
    @BeforeClass
    public void beforeclass(){
        baseUrl = "http://www.baidu.com";
        System.setProperty("webdriver.firefox.marionette","C:\\Program Files (x86)\\Mozilla Firefox\\geckodriver.exe");
    }

    @Test
    public void testFirefoxProfile(){
        ProfilesIni allProfiles = new ProfilesIni();
        org.openqa.selenium.firefox.FirefoxProfile profile = allProfiles.getProfile("default");
//加载firefox的default配置文件,这样用selenium打开的页面也会有firebug
        profile.setPreference("browser.startup.homepage", baseUrl);
//将百度设为主页面
        driver = new FirefoxDriver(profile);
        driver.findElement(By.id("kw")).sendKeys("Webdriver");
//在百度搜索框输入Webdriver
        driver.findElement(By.id("su")).click();//点击搜索按钮        
    }

    @AfterClass
    public void afterclass(){
        driver.quit();
    }
}

你可能感兴趣的:(selenium)