无法在firefox配置文件中设置restclient的首选项

时间:2014-02-06 07:31:16

标签: java selenium selenium-webdriver

我已经厌倦了在selenium webdriver测试中添加restclient扩展。但扩展不会在运行时实例firefox中添加。有人可以帮助我如何在浏览器中添加插件吗?

    FirefoxProfile profile = new FirefoxProfile();

    // add any custom firefox configurations...
    profile.setPreference("general.useragent.override", "some UA string");
    profile.setPreference("javascript.options.showInConsole", true);
    profile.setPreference("dom.max_script_run_time", 0);

    // might have to uninstall, search for *.xpi, then reinstall, then search 
    // again and compare to find the location on your system
    // ...you should probably copy this into your selenium resources directory!
    File modifyHeadersXpi = new File("/home/nvenkat/.mozilla/firefox/djmkcg3c.default/extensions/{ad0d925d-88f8-47f1-85ea-8463569e756e}.xpi");
    if (modifyHeadersXpi.exists()) {
        profile.addExtension(modifyHeadersXpi);             
        profile.setPreference("modifyheaders.config.active", true);
        profile.setPreference("modifyheaders.config.openNewTab", true);
        profile.setPreference("extensions.sce.bypass_domain_mismatch", true);
        profile.setPreference("webdriver_assume_untrusted_issuer", false);
        profile.setEnableNativeEvents(true);
        profile.setPreference("modifyheaders.config.migrated", true);
        profile.setPreference("modifyheaders.autocomplete.name.defaults", 
                "[\"Accept\",\"Cache-Control\",\"Cookie\",\"Content-Length\",\"Content-Type\",\"Date\",\"Host\",\"Pragma\",\"Referer\",\"User-Agent\",\"Via\",\"X-Requested-With\",\"X-Forwarded-For\",\"X-Do-Not-Track\"]");
        profile.setPreference("modifyheaders.headers.count", 1);
        profile.setPreference("modifyheaders.headers.action0", "Add");
        profile.setPreference("modifyheaders.headers.name0", "sox");
        profile.setPreference("modifyheaders.headers.value0", "305471");
        profile.setPreference("modifyheaders.headers.enabled0", true);
        profile.setPreference("modifyheaders.config.active", true);
        profile.setPreference("modifyheaders.config.alwaysOn", true);

    }
    Thread.sleep(3000);
    WebDriver driver = new FirefoxDriver(profile);

3 个答案:

答案 0 :(得分:0)

您使用的是哪个版本的修改标头? 您的个人资料中的xpi可能与您用于安装扩展程序的xpi不同。 这是它的工作原理:

    FirefoxProfile fp = new FirefoxProfile();
    try {
        fp.addExtension(new File("/foo/bar/modify_headers-0.7.1.1-fx.xpi"));
    } catch (IOException e) {
        //handle
    }
    fp.setPreference("modifyheaders.headers.count", 1);
    fp.setPreference("modifyheaders.headers.action0", "Add");
    fp.setPreference("modifyheaders.headers.name0", "X-Testing");
    fp.setPreference("modifyheaders.headers.value0", "your_value");
    fp.setPreference("modifyheaders.headers.enabled0", true);
    fp.setPreference("modifyheaders.config.active", true);
    fp.setPreference("modifyheaders.config.alwaysOn", true);

    WebDriver driver = new FirefoxDriver(fp);

答案 1 :(得分:0)

首先创建Firefox配置文件:运行Firefox -p,创建新的FFprofile。

ProfilesIni profile = new ProfilesIni(); FirefoxProfile ffprofile = profile.getProfile("default"); WebDriver driver = new FirefoxDriver(ffprofile);

答案 2 :(得分:0)

另一种方式:

    driver = new FirefoxDriver();       
    driver.get("https://addons.mozilla.org/en-S/firefox/addon/xxx/");
    driver.findElement(By.xpath("Add To FF button")).click();
    Thread.sleep(9000);

    Robot R = new Robot();
    R.keyPress(KeyEvent.VK_ALT);
    R.keyPress(KeyEvent.VK_I);

    R.keyRelease(KeyEvent.VK_ALT );
    R.keyRelease(KeyEvent.VK_I);
    Thread.sleep(1000);

    R.keyPress(KeyEvent.VK_ALT);
    R.keyPress(KeyEvent.VK_R);
    R.keyRelease(KeyEvent.VK_ALT);
    R.keyRelease(KeyEvent.VK_R);

    R.keyPress(KeyEvent.VK_ALT);
    R.keyPress(KeyEvent.VK_R);
    R.keyRelease(KeyEvent.VK_ALT);
    R.keyRelease(KeyEvent.VK_R);

    R.keyPress(KeyEvent.VK_ALT+KeyEvent.VK_R);
    R.keyPress(KeyEvent.VK_ENTER);

    R.keyRelease(KeyEvent.VK_ALT+KeyEvent.VK_R);
    //R.keyRelease(KeyEvent.VK_ALT + KeyEvent.VK_R);
    R.keyRelease(KeyEvent.VK_ENTER);

    ProfilesIni Newdriver = new ProfilesIni();
    FirefoxProfile wdprofile = Newdriver.getProfile("default");
    driver = new FirefoxDriver();




//enter code here`
    driver = new FirefoxDriver(wdprofile);`
    driver.get("chrome://restclient/content/restclient.html");`

`

    //FYI... webelement can't be identify by webdriver.Thats an open issue.