[Selenium] 使用自定义的FirefoxProfile

FirefoxProfile 用于定制待测试的Firefox 浏览器的特定属性,其中包括所存储的密码、书签、历史信息、Cookies等。某些测试用例需要用到特定的用户信息,因此可通过定制当前Firefox 运行实例的FirefoxProfile 来达到目标

1)如果需要查看当前Firefox 运行实例的FirefoxProfile, 可通过Help->Troubleshooting Information->Profile Folder来获取

2)如果希望在Firefox 启动的时候已经加载某个插件,可通过addExtension 提前加载以.xpi 为扩展名的插件

3)如果希望Firefox 以某些特定偏好设置启动,可通过setPreference 达到目的

4)如果希望Firefox 对SSL 证书的处理机制进行调整,可通过 setAssumeUntrustedCertificatelssur 和 setAcceptUntrustedCertificates 达到目的

5)如果希望将FirefoxProfile 导出成 JSON 格式,可通过toJson 来处理

示例代码如下:

public class testFirefoxProfile{

  public static void main(String[] args){

    String prodileInJson = " ";

    FirefoxProfile profile = new FirefoxProfile();

    try{

      profile.addExtension(new File("/path/to/extension.xpi"));

      profile.setPreference("browser.startup.homepage", "about:blank");

      profile.setAssumeUntrustedCertificatelssuer(false);

      profile.setAcceptUntrustedCertificates(false);

      profileInJson = profile.toJson();

      System.out.println(profileInJson);

    }catch(IOException e){

      e.printStackTrace();

    }

    WebDriver driver = new FirefoxDriver(profile);

    driver.get("http://www.baidu.com");

    driver.close();

  }

}

原文地址:https://www.cnblogs.com/feifeidxl/p/4538556.html