001/*
002 * (C) Copyright 2011-2016 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Sun Seng David TAN
016 *     Florent Guillaume
017 *     Benoit Delbosc
018 *     Antoine Taillefer
019 *     Anahide Tchertchian
020 *     Guillaume Renard
021 *     Mathieu Guillaume
022 *     Julien Carsique
023 */
024package org.nuxeo.functionaltests.drivers;
025
026import java.io.File;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.openqa.selenium.firefox.FirefoxDriver;
031import org.openqa.selenium.firefox.FirefoxProfile;
032import org.openqa.selenium.remote.DesiredCapabilities;
033import org.openqa.selenium.remote.RemoteWebDriver;
034
035import net.jsourcerer.webdriver.jserrorcollector.JavaScriptError;
036
037/**
038 * Driver provider for firefox.
039 *
040 * @since 8.3
041 */
042public class FirefoxDriverProvider implements DriverProvider {
043
044    private static final Log log = LogFactory.getLog(FirefoxDriverProvider.class);
045
046    protected FirefoxDriver driver;
047
048    public static FirefoxProfile getProfile() throws Exception {
049        FirefoxProfile profile = new FirefoxProfile();
050        // Disable native events (makes things break on Windows)
051        profile.setEnableNativeEvents(false);
052        // Set English as default language
053        profile.setPreference("general.useragent.locale", "en");
054        profile.setPreference("intl.accept_languages", "en");
055        // Set other confs to speed up FF
056
057        // Speed up firefox by pipelining requests on a single connection
058        profile.setPreference("network.http.keep-alive", true);
059        profile.setPreference("network.http.pipelining", true);
060        profile.setPreference("network.http.proxy.pipelining", true);
061        profile.setPreference("network.http.pipelining.maxrequests", 8);
062
063        // Try to use less memory
064        profile.setPreference("browser.sessionhistory.max_entries", 10);
065        profile.setPreference("browser.sessionhistory.max_total_viewers", 4);
066        profile.setPreference("browser.sessionstore.max_tabs_undo", 4);
067        profile.setPreference("browser.sessionstore.interval", 1800000);
068
069        // disable unresponsive script alerts
070        profile.setPreference("dom.max_script_run_time", 0);
071        profile.setPreference("dom.max_chrome_script_run_time", 0);
072
073        // don't skip proxy for localhost
074        profile.setPreference("network.proxy.no_proxies_on", "");
075
076        // prevent different kinds of popups/alerts
077        profile.setPreference("browser.tabs.warnOnClose", false);
078        profile.setPreference("browser.tabs.warnOnOpen", false);
079        profile.setPreference("extensions.newAddons", false);
080        profile.setPreference("extensions.update.notifyUser", false);
081
082        // disable autoscrolling
083        profile.setPreference("browser.urlbar.autocomplete.enabled", false);
084
085        // downloads conf
086        profile.setPreference("browser.download.useDownloadDir", false);
087
088        // prevent FF from running in offline mode when there's no network
089        // connection
090        profile.setPreference("toolkit.networkmanager.disable", true);
091
092        // prevent FF from giving health reports
093        profile.setPreference("datareporting.policy.dataSubmissionEnabled", false);
094        profile.setPreference("datareporting.healthreport.uploadEnabled", false);
095        profile.setPreference("datareporting.healthreport.service.firstRun", false);
096        profile.setPreference("datareporting.healthreport.service.enabled", false);
097        profile.setPreference("datareporting.healthreport.logging.consoleEnabled", false);
098
099        // start page conf to speed up FF
100        profile.setPreference("browser.startup.homepage", "about:blank");
101        profile.setPreference("pref.browser.homepage.disable_button.bookmark_page", false);
102        profile.setPreference("pref.browser.homepage.disable_button.restore_default", false);
103
104        // misc confs to avoid useless updates
105        profile.setPreference("browser.search.update", false);
106        profile.setPreference("browser.bookmarks.restore_default_bookmarks", false);
107
108        // misc confs to speed up FF
109        profile.setPreference("extensions.ui.dictionary.hidden", true);
110        profile.setPreference("layout.spellcheckDefault", 0);
111        // For FF > 40 ?
112        profile.setPreference("startup.homepage_welcome_url.additional", "about:blank");
113
114        // to ease up changing conf during tests
115        profile.setPreference("general.warnOnAboutConfig", false);
116
117        // webdriver logging
118        if (Boolean.TRUE.equals(Boolean.valueOf(System.getenv("nuxeo.log.webriver")))) {
119            String location = System.getProperty("basedir") + File.separator + "target";
120            File outputFolder = new File(location);
121            if (!outputFolder.exists() || !outputFolder.isDirectory()) {
122                outputFolder = null;
123            }
124            File webdriverlogFile = File.createTempFile("webdriver", ".log", outputFolder);
125            profile.setPreference("webdriver.log.file", webdriverlogFile.getAbsolutePath());
126            log.warn("Webdriver logs saved in " + webdriverlogFile);
127        }
128
129        return profile;
130    }
131
132    @Override
133    public RemoteWebDriver init(DesiredCapabilities dc) throws Exception {
134
135        FirefoxProfile profile = getProfile();
136
137        JavaScriptError.addExtension(profile);
138
139        dc.setCapability(FirefoxDriver.PROFILE, profile);
140        driver = new FirefoxDriver(dc);
141        return driver;
142    }
143
144    @Override
145    public RemoteWebDriver get() {
146        return driver;
147    }
148
149    @Override
150    public void quit() {
151        if (driver != null) {
152            driver.quit();
153            driver = null;
154        }
155    }
156
157}