Example usage for org.openqa.selenium.firefox FirefoxProfile setPreference

List of usage examples for org.openqa.selenium.firefox FirefoxProfile setPreference

Introduction

In this page you can find the example usage for org.openqa.selenium.firefox FirefoxProfile setPreference.

Prototype

public void setPreference(String key, Object value) 

Source Link

Usage

From source file:org.terasoluna.gfw.tutorial.selenium.WebDriverCreator.java

License:Apache License

/**
 * Start WebDriver with download function enabled.
 * <p>/*from   ww  w . j a va 2s .  co  m*/
 * Supports FireFox only<br>
 * </p>
 * @param downloadTempDirectory : Temporary storage directory for download
 * @return WebDriver instance with download function enabled
 */
public WebDriver createDownloadableWebDriver(String downloadTempDirectory) {
    for (String activeProfile : getApplicationContext().getEnvironment().getActiveProfiles()) {
        if ("chrome".equals(activeProfile) || "ie".equals(activeProfile) || "phantomJs".equals(activeProfile)) {
            throw new UnsupportedOperationException(
                    "It is not possible to run tests using the download function on browsers other than FireFox.");
        }
    }
    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("browser.download.dir", downloadTempDirectory);
    profile.setPreference("browser.download.folderList", 2);
    profile.setPreference("browser.download.lastDir", downloadTempDirectory);
    profile.setPreference("browser.helperApps.alwaysAsk.force", false);
    profile.setPreference("browser.download.manager.showWhenStarting", false);
    profile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
    profile.setPreference("pdfjs.disabled", true);
    profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
            "application/pdf, text/csv, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, text/plain");
    profile.setPreference("brouser.startup.homepage_override.mstone", "ignore");
    profile.setPreference("network.proxy.type", 0);

    WebDriver webDriver = new FirefoxDriver(profile);
    webDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    return webDriver;
}

From source file:org.wso2.am.integration.ui.tests.APIMANAGER4006SampleApiDeploymentTestCase.java

License:Open Source License

@BeforeClass(alwaysRun = true)
public void setUp() throws Exception {
    FirefoxProfile firefoxProfile = new FirefoxProfile();
    firefoxProfile.setPreference("browser.download.folderList", 2);
    firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
    firefoxProfile.setPreference("browser.download.dir", System.getProperty("download.location"));
    firefoxProfile.setPreference("browser.helperApps.alwaysAsk.force", false);
    //Set browser settings to automatically download wsdl files
    firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/force-download");
    driver = new FirefoxDriver(firefoxProfile);
    wait = new WebDriverWait(driver, 60);
}

From source file:org.xwiki.test.ui.WebDriverFactory.java

License:Open Source License

public WebDriver createWebDriver(String browserName) {
    WebDriver driver;/*from  w  w w.j av a 2  s  .  c  o m*/
    if (browserName.startsWith("*firefox")) {
        // Native events are disabled by default for Firefox on Linux as it may cause tests which open many windows
        // in parallel to be unreliable. However, native events work quite well otherwise and are essential for some
        // of the new actions of the Advanced User Interaction. We need native events to be enable especially for
        // testing the WYSIWYG editor. See http://code.google.com/p/selenium/issues/detail?id=2331 .
        FirefoxProfile profile = new FirefoxProfile();
        profile.setEnableNativeEvents(true);
        // Make sure Firefox doesn't upgrade automatically on CI agents.
        profile.setPreference("app.update.auto", false);
        profile.setPreference("app.update.enabled", false);
        profile.setPreference("app.update.silent", false);
        driver = new FirefoxDriver(profile);

        // Hide the Add-on bar (from the bottom of the window, with "WebDriver" written on the right) because it can
        // prevent buttons or links from being clicked when they are beneath it and native events are used.
        // See https://groups.google.com/forum/#!msg/selenium-users/gBozOynEjs8/XDxxQNmUSCsJ
        // We need to load a page before sending the keys otherwise WebDriver throws ElementNotVisible exception.
        driver.get("data:text/plain;charset=utf-8,XWiki");
        driver.switchTo().activeElement().sendKeys(Keys.chord(Keys.CONTROL, "/"));
    } else if (browserName.startsWith("*iexplore")) {
        driver = new InternetExplorerDriver();
    } else if (browserName.startsWith("*chrome")) {
        driver = new ChromeDriver();
    } else if (browserName.startsWith("*phantomjs")) {
        DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
        capabilities.setCapability("handlesAlerts", true);
        try {
            driver = new PhantomJSDriver(ResolvingPhantomJSDriverService.createDefaultService(), capabilities);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    } else {
        throw new RuntimeException("Unsupported browser name [" + browserName + "]");
    }

    // Maximize the browser window by default so that the page has a standard layout. Individual tests can resize
    // the browser window if they want to test how the page layout adapts to limited space. This reduces the
    // probability of a test failure caused by an unexpected layout (nested scroll bars, floating menu over links
    // and buttons and so on).
    driver.manage().window().maximize();

    return driver;
}

From source file:pagelyzer.BrowserRep.java

License:Open Source License

/**
 * Set the selenium driver as local (webdriver instance)
 *//*from w w w. j  av  a 2  s  .co m*/
public void setLocalDriver() {
    switch (this.desc) {
    case "firefox":
        FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference("browser.download.lastDir", "/home/sanojaa/src/migration425/sources/inbox/pages");
        profile.setPreference("browser.download.manager.showWhenStarting", false);
        profile.setPreference("browser.download.dir", "/home/sanojaa/src/migration425/sources/inbox/pages");
        profile.setEnableNativeEvents(true);
        this.driver = new FirefoxDriver(profile);
        break;
    case "iexplorer":
        this.driver = new InternetExplorerDriver();
        break;
    case "chrome":
        this.driver = new ChromeDriver();
        break;
    case "opera":
        this.driver = new OperaDriver();
        break;
    case "htmlunit":
        this.driver = new HtmlUnitDriver();
        break;
    }
    setJSDriver();
    this.driver.manage().timeouts().pageLoadTimeout(MAX_WAIT_S, TimeUnit.SECONDS);
    this.driver.manage().timeouts().implicitlyWait(MAX_WAIT_S, TimeUnit.SECONDS);
}

From source file:pawl.webdriver.LocalizedWebDriverProvider.java

License:Apache License

/**
 * Provide new Firefox driver with setup of user language.
 *
 * @return firefox driver/* ww w  .jav a2 s .c o m*/
 */
protected FirefoxDriver createFirefoxDriver() {
    FirefoxProfile firefoxProfile = new FirefoxProfile();
    firefoxProfile.setPreference("intl.accept_languages", getSystemLanguage());
    FirefoxDriver firefoxDriver = new FirefoxDriver(firefoxProfile);
    firefoxDriver.manage().window().maximize();
    return firefoxDriver;
}

From source file:plugins.KerberosSsoTest.java

License:Open Source License

private FirefoxDriver getNegotiatingFirefox(KerberosContainer kdc, String tokenCache) {
    FirefoxProfile profile = new FirefoxProfile();
    profile.setAlwaysLoadNoFocusLib(true);
    // Allow auth negotiation for jenkins under test
    profile.setPreference("network.negotiate-auth.trusted-uris", jenkins.url.toExternalForm());
    profile.setPreference("network.negotiate-auth.delegation-uris", jenkins.url.toExternalForm());
    FirefoxBinary binary = new FirefoxBinary();
    // Inject config and TGT
    binary.setEnvironmentProperty("KRB5CCNAME", tokenCache);
    binary.setEnvironmentProperty("KRB5_CONFIG", kdc.getKrb5ConfPath());
    // Turn debug on
    binary.setEnvironmentProperty("KRB5_TRACE", diag.touch("tracelog").getAbsolutePath());
    binary.setEnvironmentProperty("NSPR_LOG_MODULES", "negotiateauth:5");
    binary.setEnvironmentProperty("NSPR_LOG_FILE", diag.touch("firefox.nego.log").getAbsolutePath());

    String display = FallbackConfig.getBrowserDisplay();
    if (display != null) {
        binary.setEnvironmentProperty("DISPLAY", display);
    }/*from w  w w  . j  ava  2s  . com*/
    final FirefoxDriver driver = new FirefoxDriver(binary, profile);
    cleaner.addTask(new Statement() {
        @Override
        public void evaluate() throws Throwable {
            try {
                driver.quit();
            } catch (UnreachableBrowserException ex) {
                System.err.println("Browser died already");
                ex.printStackTrace();
            }
        }

        @Override
        public String toString() {
            return "Close Kerberos WebDriver after test";
        }
    });
    return driver;
}

From source file:renascere.Renascere.java

License:Open Source License

/**
 * @Description Method that opens a specified browser
 *///from  ww  w  .  java2s  .  c o  m
static WebDriver openABrowser(browser bBrowserName) {
    WebDriver driver = null;
    String sDriverName = null;
    DesiredCapabilities DesireCaps = new DesiredCapabilities();

    try {
        //Creating browser instance
        switch (bBrowserName) {
        case FIREFOX:
            //Initializing Driver
            FirefoxProfile browserProfile = new FirefoxProfile();

            //Adding following preferences to download files
            browserProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");
            browserProfile.setPreference("browser.download.folderList", 2);
            browserProfile.setPreference("browser.download.dir", Vars.os_PathOut);
            browserProfile.setPreference("browser.download.manager.alertOnEXEOpen", false);
            browserProfile.setPreference("browser.helperApps.neverAsk.saveToDisk",
                    "application/msword, application/csv, "
                            + "application/ris, text/csv, image/png, application/pdf, text/html, "
                            + "text/plain, application/zip, application/x-zip, application/x-zip-compressed, "
                            + "application/download, application/octet-stream");
            browserProfile.setPreference("browser.download.manager.showWhenStarting", false);
            browserProfile.setPreference("browser.download.manager.focusWhenStarting", false);
            browserProfile.setPreference("browser.download.useDownloadDir", true);
            browserProfile.setPreference("browser.helperApps.alwaysAsk.force", false);
            browserProfile.setPreference("browser.download.manager.alertOnEXEOpen", false);
            browserProfile.setPreference("browser.download.manager.closeWhenDone", true);
            browserProfile.setPreference("browser.download.manager.showAlertOnComplete", false);
            browserProfile.setPreference("browser.download.manager.useWindow", false);
            browserProfile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting",
                    false);
            browserProfile.setPreference("pdfjs.disabled", true);
            browserProfile.setPreference("browser.download.manager.openDelay", 100000);
            browserProfile.setPreference("browser.download.animateNotifications", false);
            driver = new FirefoxDriver(browserProfile);
            break;
        case CHROME:
            //Determine driver to be used
            sDriverName = Vars.os_Name.toLowerCase().contains("win") ? "chromedriver-win.exe"
                    : (Vars.os_Name.toLowerCase().contains("mac") ? "chromedriver-mac" : "chromedriver-lnx");

            //Initializing Driver
            System.setProperty("webdriver.chrome.driver", Vars.os_PathRef + sDriverName);
            DesireCaps = DesiredCapabilities.chrome();
            driver = new ChromeDriver(DesireCaps);
            break;
        case IE:
            //Determine driver to be used
            System.setProperty("webdriver.ie.driver", Vars.os_PathRef + "IEDriverServer-win.exe");
            DesireCaps = Vars.os_Name.toLowerCase().contains("win") ? DesiredCapabilities.internetExplorer()
                    : null;
            if (DesireCaps == null) {
                throw new Exception("Invalid OS for browser.");
            }

            //Create instance
            driver = new InternetExplorerDriver(DesireCaps);
            break;
        case OPERA:
            //Determine driver to be used
            sDriverName = Vars.os_Name.toLowerCase().contains("win") ? "operadriver-win.exe"
                    : (Vars.os_Name.toLowerCase().contains("mac") ? "operadriver-mac" : "operadriver-lnx");

            //Initializing Driver
            System.setProperty("webdriver.chrome.driver", Vars.os_PathRef + sDriverName);
            driver = new ChromeDriver();
            break;
        case SAFARI:
            //Initializing Driver
            driver = new SafariDriver();
            break;
        case PHANTOMJS:
            //Determine driver to be used
            sDriverName = Vars.os_Name.toLowerCase().contains("win") ? "phantomjs-win.exe"
                    : (Vars.os_Name.toLowerCase().contains("mac") ? "phantomjs-mac" : "phantomjs-lnx");

            //Initializing Driver
            DesireCaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
                    Vars.os_PathRef + sDriverName);
            driver = new PhantomJSDriver();
            break;
        default:
            throw new Exception("Unsupported browser.");
        }

        //Getting additional browser information
        Vars.ts_BHandler = driver.getWindowHandle();

    } catch (Exception e) {
        frmError("creating a browser instance (" + bBrowserName + ")", e.getMessage(), severity.HIGH);
    }
    return driver;
}

From source file:selenium.WebDriverBuilder.java

public EventFiringWebDriver getFirefoxDriver(Site site) {
    FirefoxProfile ffp = new FirefoxProfile();
    ffp.setPreference("general.useragent.override", site.getUserAgent());
    DesiredCapabilities caps = DesiredCapabilities.firefox();
    caps.setJavascriptEnabled(true);/*from www  .j  a  v  a2s. c  o  m*/
    caps.setCapability(FirefoxDriver.PROFILE, ffp);
    caps.setCapability("takesScreenshot", true);
    // User Name & Password Settings
    webDriver = new FirefoxDriver(caps);
    if (site.getViewPortHeight() > 0 && site.getViewPortWidth() > 0) {
        Dimension s = new Dimension(site.getViewPortWidth(), site.getViewPortHeight());
        webDriver.manage().window().setSize(s);
    } else {
        webDriver.manage().window().maximize();
    }
    webDriver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    driver = new EventFiringWebDriver(webDriver);
    EventHandler handler = new EventHandler();
    driver.register(handler);
    return driver;
}

From source file:sf.wicklet.ext.test.arquillian.Test01.java

License:Apache License

@Test
public void test01(@ArquillianResource final URL httpContext) throws Exception {
    final StepWatch timer = new StepWatch(true);
    final FirefoxProfile profile = new FirefoxProfile(new File("../opt/firefox/7x16slsr.default"));
    profile.setPreference("network.dns.disableIPv6", true);
    final FirefoxDriver driver = new FirefoxDriver(profile);
    try {//from w  w w .j  a v a  2s  .  co  m
        if (Test01.DEBUG.isDebug()) {
            System.out.println(timer.toString("# Client start"));
        }
        driver.get(new URL(httpContext, TestAccordion01Page.MNT_PATH).toString());
        final String title = driver.getTitle();
        if (Test01.DEBUG.isDebug()) {
            System.out.println(timer.toString("Page title is: " + title));
            System.out.println(driver.getPageSource());
        }
        Assert.assertEquals("Test", driver.getTitle());
        final List<WebElement> p1 = driver.findElementsByLinkText("Panel1");
        final List<WebElement> p2 = driver.findElementsByLinkText("Panel2");
        final List<WebElement> p3 = driver.findElementsByLinkText("Panel3");
        final List<WebElement> c1 = driver.findElementsById("content1");
        final List<WebElement> c2 = driver.findElementsById("content2");
        final List<WebElement> c3 = driver.findElementsById("content3");
        Assert.assertEquals(1, p1.size());
        Assert.assertEquals(1, p2.size());
        Assert.assertEquals(1, p3.size());
        Assert.assertEquals(1, c1.size());
        Assert.assertEquals(1, c2.size());
        Assert.assertEquals(1, c3.size());
        Assert.assertEquals("", c2.get(0).getAttribute("style"));
        //
        p2.get(0).click();
        if (Test01.DEBUG.isDebug()) {
            System.out.println(driver.getPageSource());
        }
        final List<WebElement> cc1 = driver.findElementsById("content1");
        final List<WebElement> cc2 = driver.findElementsById("content2");
        final List<WebElement> cc3 = driver.findElementsById("content3");
        Assert.assertEquals(1, cc1.size());
        Assert.assertEquals(1, cc2.size());
        Assert.assertEquals(1, cc3.size());
        Assert.assertEquals("display: none;", cc2.get(0).getAttribute("style"));
        //
        p2.get(0).click();
        if (Test01.DEBUG.isDebug()) {
            System.out.println(driver.getPageSource());
        }
        final List<WebElement> ccc2 = driver.findElementsById("content2");
        Assert.assertEquals(1, ccc2.size());
        Assert.assertEquals("display: block;", ccc2.get(0).getAttribute("style"));
    } finally {
        if (Test01.DEBUG.isDebug()) {
            final String text = driver.getPageSource();
            FileUtil.writeFile(Test01.test01Html, false, text);
            SeleniumTestUtil.takeScreenshot(driver, Test01.test01Png);
            System.out.println(timer.toString("# Client done"));
        }
        if (Test01.DEBUG.isDebugServer()) {
            System.in.read();
        } else {
            driver.quit();
        }
    }
}

From source file:sf.wicklet.gwt.client.test.Test01.java

License:Apache License

@Test
public void testHome01() throws IOException {
    final StepWatch timer = new StepWatch(true);
    final FirefoxProfile profile = new FirefoxProfile(firefoxProfileDir);
    profile.setPreference("network.dns.disableIPv6", true);
    // final FirefoxDriver driver = new FirefoxDriver(new FirefoxBinary(firefoxBinary), profile);
    final FirefoxDriver driver = new FirefoxDriver(profile);
    try {/*from  w  ww  . j a v a  2  s .  c om*/
        if (DEBUG.isDebug()) {
            System.out.println(timer.toString("# Client start"));
        }
        driver.get(BASEURL);
        final String title = driver.getTitle();
        if (DEBUG.isDebug()) {
            System.out.println(timer.toString("Page title is: " + title));
        }
        assertEquals("HomePage", title);
        if (DEBUG.isDebug()) {
            final String text = driver.getPageSource();
            System.out.println(text);
            FileUtil.writeFile(testHome01Html, false, text);
            takeScreenshot(driver, testHome01Png);
        }
        toppanel(driver);
        logindialog(driver);
        menubar(driver);
        stackpanel(driver);
        ajaxpanel(driver);
    } catch (final Throwable e) {
        e.printStackTrace();
        throw e;
    } finally {
        if (DEBUG.isDebug()) {
            System.out.println(timer.toString("# Client done"));
        }
        if (DEBUG.isDebugServer()) {
            System.in.read();
        } else {
            driver.quit();
        }
    }
}