How to use getFile method of org.openqa.selenium.firefox.FirefoxBinary class

Best Selenium code snippet using org.openqa.selenium.firefox.FirefoxBinary.getFile

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1package firefox;
2
3import org.openqa.selenium.firefox.FirefoxBinary;
4import org.openqa.selenium.firefox.FirefoxDriver;
5import org.openqa.selenium.firefox.FirefoxOptions;
6import org.openqa.selenium.firefox.FirefoxProfile;
7import org.openqa.selenium.remote.CapabilityType;
8import org.openqa.selenium.remote.DesiredCapabilities;
9
10import java.io.File;
11
12public class Test {
13    @org.junit.Test
14    public void FirefoxHeadlessTest() {
15        final String geckodriver = this.getClass().getResource("/geckodriver").getFile();
16        new File(geckodriver).setExecutable(true);
17        System.setProperty("webdriver.gecko.driver", geckodriver);
18
19        final FirefoxBinary firefoxBinary = new FirefoxBinary();
20        firefoxBinary.addCommandLineOptions("--headless");
21
22        final DesiredCapabilities capabilities = new DesiredCapabilities();
23        capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
24        capabilities.setCapability("acceptInsecureCerts", true);
25        capabilities.setCapability("disable-popup-blocking", true);
26
27        final FirefoxProfile profile = new FirefoxProfile();
28        profile.setAcceptUntrustedCertificates(true);
29        profile.setPreference("network.proxy.type", 1);
30        profile.setPreference("network.proxy.http", "localhost");
31        profile.setPreference("network.proxy.http_port", "8080");
32        profile.setPreference("network.proxy.ssl", "localhost");
33        profile.setPreference("network.proxy.ssl_port", "8080");
34        profile.setPreference("network.proxy.no_proxies_on", "");
35
36        final FirefoxOptions firefoxOptions = new FirefoxOptions(capabilities);
37        firefoxOptions.setBinary(firefoxBinary);
38        firefoxOptions.setProfile(profile);
39        firefoxOptions.addPreference("network.proxy.http", "localhost");
40        firefoxOptions.addPreference("network.proxy.http_port", "8080");
41        firefoxOptions.addPreference("network.proxy.https", "localhost");
42        firefoxOptions.addPreference("network.proxy.https_port", "8080");
43        final FirefoxDriver driver = new FirefoxDriver(firefoxOptions);
44        driver.quit();
45    }
46}
47
Full Screen
copy
1package com.hybris.guestbook.selenium;
2
3import com.hybris.guestbook.TestPropertiesHelper;
4import com.hybris.service.exception.FailureException;
5import org.apache.commons.io.FileUtils;
6import org.openqa.selenium.firefox.FirefoxBinary;
7import org.openqa.selenium.firefox.FirefoxDriver;
8import org.openqa.selenium.firefox.FirefoxProfile;
9import org.openqa.selenium.remote.RemoteWebDriver;
10import org.springframework.core.io.ClassPathResource;
11
12import java.io.File;
13import java.io.IOException;
14import java.util.Collection;
15import java.util.concurrent.TimeUnit;
16
17import static com.google.common.primitives.Ints.checkedCast;
18import static com.hybris.guestbook.TestPropertiesHelper.enableFirebugDebugTabs;
19
20public class FirefoxDriverBuilder {
21
22    private static final long FIREFOX_STARTUP_TIMEOUT = TimeUnit.MINUTES.toMillis(2);
23    private static final long FIREFOX_MAX_SCRIPT_RUN_TIME = TimeUnit.SECONDS.toSeconds(30);
24
25    public RemoteWebDriver build() {
26
27        FirefoxProfile firefoxProfile = new FirefoxProfile();
28
29        configureFirefoxMaxScriptRunTime(firefoxProfile);
30
31        configureFirefoxToSaveFilesToDiskWithoutAsking(firefoxProfile);
32
33        /* add Firefox extensions if needed */
34        if (TestPropertiesHelper.addFireFoxExtentions()) {
35            addFirefoxExtensions(firefoxProfile);
36        }
37
38        final FirefoxBinary firefoxBinary = TestPropertiesHelper.useExecutableDriverPath()?
39                new FirefoxBinary(FileUtils.getFile(TestPropertiesHelper.specificExecutablePath())):
40                new FirefoxBinary();
41
42
43        firefoxBinary.setTimeout(FIREFOX_STARTUP_TIMEOUT);
44
45        return new FirefoxDriver(firefoxBinary, firefoxProfile);
46    }
47
48    private void configureFirefoxMaxScriptRunTime(FirefoxProfile profile) {
49
50        profile.setPreference("dom.max_script_run_time", checkedCast(FIREFOX_MAX_SCRIPT_RUN_TIME));
51        profile.setPreference("dom.max_chrome_script_run_time", checkedCast(FIREFOX_MAX_SCRIPT_RUN_TIME));
52    }
53
54    /**
55     * Download certain MIME types without asking so we can assert on those from FitNesse.
56     * <p>
57     * Original research: http://stackoverflow.com/a/7983487/68119
58     */
59    private void configureFirefoxToSaveFilesToDiskWithoutAsking(FirefoxProfile profile) {
60
61        profile.setPreference("browser.download.folderList", 2);
62        profile.setPreference("browser.download.dir", TestPropertiesHelper.browserDownloadDirectory());
63        profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
64                TestPropertiesHelper.firefoxMimeTypeListToSaveToDiskWithoutAsking());
65    }
66
67    /**
68     * Load extensions (currently FireBug and FireCookie) .
69     * <p>
70     * Original research: http://stackoverflow.com/questions/3421793/how-do-i-run-firebug-within-selenium-2
71     */
72    private void addFirefoxExtensions(FirefoxProfile profile) {
73        try {
74            File extDir = new ClassPathResource("selenium/firefox/extensions").getFile();
75
76            // get all .xpi files from extensions directory recursively
77            Collection<File> extFiles = FileUtils.listFiles(extDir, new String[]{"xpi"}, true);
78
79            for (File extFile : extFiles) {
80                profile.addExtension(extFile);
81            }
82        } catch (IOException e) {
83            throw new FailureException("Unable to load Firefox extensions", e);
84        }
85
86        /*
87         * Hack to prevent FireBug from opening its Release Notes tab breaking our tests. With this property set it
88         * will only open that tab when FireBug version 301 is released. :-)
89         */
90        profile.setPreference("extensions.firebug.currentVersion", "300");
91
92        if (enableFirebugDebugTabs()) {
93            // Firebug properties from http://getfirebug.com/wiki/index.php/Firebug_Preferences
94            profile.setPreference("extensions.firebug.script.enableSites", "true");
95            profile.setPreference("extensions.firebug.console.enableSites", "true");
96        }
97    }
98
99}
100
Full Screen
copy
1import org.openqa.selenium.WebDriver;
2import org.openqa.selenium.firefox.FirefoxBinary;
3import org.openqa.selenium.firefox.FirefoxDriver;
4import org.openqa.selenium.firefox.FirefoxOptions;
5
6import java.io.File;
7import java.net.URL;
8
9/**
10 * Selenium Manager Class
11 * It should manage all Selenium actions
12 */
13public class Demo1SeleniumManager {
14
15    // Local path to Firefox application binary
16    private static final String FIREFOX_BINARY_PATH = "C:\\Program Files\\Mozilla Firefox\\firefox.exe";
17
18    // Private Global Firefox Web Driver
19    private WebDriver mFirefoxWebDriver;
20
21    /**
22     * SeleniumManager Class Constructor
23     */
24    public Demo1SeleniumManager() {
25
26        // Acquiring GeckoDriver binary file URL
27        URL geckoDriverUrl = this.getClass().getClassLoader().getResource("geckodriver.exe");
28        if (geckoDriverUrl == null) {
29            System.out.println("[ ERROR ] Fail to get geckodriver.exe URL");
30            return;
31        }
32
33        // GeckoDriver Setup
34        System.setProperty("webdriver.gecko.driver", geckoDriverUrl.getFile());
35
36        // Firefox Application Setup
37        File firefoxBinaryPath = new File(FIREFOX_BINARY_PATH);
38        FirefoxBinary firefoxBinary = new FirefoxBinary(firefoxBinaryPath);
39        FirefoxOptions firefoxOptions = new FirefoxOptions();
40        firefoxOptions.setBinary(firefoxBinary);
41
42        // Instantiating Global Firefox Driver
43        mFirefoxWebDriver = new FirefoxDriver(firefoxOptions);
44    }
45
46    /**
47     * openFirefox Method
48     * It should spawn firefox browser in parameter URL
49     * @param Url URL to be opened in Firefox browser
50     */
51    public void openFirefox(String Url) {
52        mFirefoxWebDriver.get(Url);
53    }
54
55    /**
56     * Print Current Page Info Method
57     */
58    public void printCurrentPageInfo() {
59        System.out.println("\n");
60        System.out.println("+===============================+");
61        System.out.println("|       Current Page Info       |");
62        System.out.println("+===============================+");
63        System.out.println("URL:   " + mFirefoxWebDriver.getCurrentUrl());
64        System.out.println("Title: " + mFirefoxWebDriver.getTitle());
65        System.out.println("+===============================+");
66        System.out.println("\n");
67    }
68}
69
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger getFile code on LambdaTest Cloud Grid

Execute automation tests with getFile on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie