Run Selenium automation tests on LambdaTest cloud grid
Perform automation testing on 3000+ real desktop and mobile devices online.
package firefox;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.io.File;
public class Test {
@org.junit.Test
public void FirefoxHeadlessTest() {
final String geckodriver = this.getClass().getResource("/geckodriver").getFile();
new File(geckodriver).setExecutable(true);
System.setProperty("webdriver.gecko.driver", geckodriver);
final FirefoxBinary firefoxBinary = new FirefoxBinary();
firefoxBinary.addCommandLineOptions("--headless");
final DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setCapability("acceptInsecureCerts", true);
capabilities.setCapability("disable-popup-blocking", true);
final FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true);
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.http", "localhost");
profile.setPreference("network.proxy.http_port", "8080");
profile.setPreference("network.proxy.ssl", "localhost");
profile.setPreference("network.proxy.ssl_port", "8080");
profile.setPreference("network.proxy.no_proxies_on", "");
final FirefoxOptions firefoxOptions = new FirefoxOptions(capabilities);
firefoxOptions.setBinary(firefoxBinary);
firefoxOptions.setProfile(profile);
firefoxOptions.addPreference("network.proxy.http", "localhost");
firefoxOptions.addPreference("network.proxy.http_port", "8080");
firefoxOptions.addPreference("network.proxy.https", "localhost");
firefoxOptions.addPreference("network.proxy.https_port", "8080");
final FirefoxDriver driver = new FirefoxDriver(firefoxOptions);
driver.quit();
}
}
package com.hybris.guestbook.selenium;
import com.hybris.guestbook.TestPropertiesHelper;
import com.hybris.service.exception.FailureException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.springframework.core.io.ClassPathResource;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
import static com.google.common.primitives.Ints.checkedCast;
import static com.hybris.guestbook.TestPropertiesHelper.enableFirebugDebugTabs;
public class FirefoxDriverBuilder {
private static final long FIREFOX_STARTUP_TIMEOUT = TimeUnit.MINUTES.toMillis(2);
private static final long FIREFOX_MAX_SCRIPT_RUN_TIME = TimeUnit.SECONDS.toSeconds(30);
public RemoteWebDriver build() {
FirefoxProfile firefoxProfile = new FirefoxProfile();
configureFirefoxMaxScriptRunTime(firefoxProfile);
configureFirefoxToSaveFilesToDiskWithoutAsking(firefoxProfile);
/* add Firefox extensions if needed */
if (TestPropertiesHelper.addFireFoxExtentions()) {
addFirefoxExtensions(firefoxProfile);
}
final FirefoxBinary firefoxBinary = TestPropertiesHelper.useExecutableDriverPath()?
new FirefoxBinary(FileUtils.getFile(TestPropertiesHelper.specificExecutablePath())):
new FirefoxBinary();
firefoxBinary.setTimeout(FIREFOX_STARTUP_TIMEOUT);
return new FirefoxDriver(firefoxBinary, firefoxProfile);
}
private void configureFirefoxMaxScriptRunTime(FirefoxProfile profile) {
profile.setPreference("dom.max_script_run_time", checkedCast(FIREFOX_MAX_SCRIPT_RUN_TIME));
profile.setPreference("dom.max_chrome_script_run_time", checkedCast(FIREFOX_MAX_SCRIPT_RUN_TIME));
}
/**
* Download certain MIME types without asking so we can assert on those from FitNesse.
* <p>
* Original research: http://stackoverflow.com/a/7983487/68119
*/
private void configureFirefoxToSaveFilesToDiskWithoutAsking(FirefoxProfile profile) {
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", TestPropertiesHelper.browserDownloadDirectory());
profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
TestPropertiesHelper.firefoxMimeTypeListToSaveToDiskWithoutAsking());
}
/**
* Load extensions (currently FireBug and FireCookie) .
* <p>
* Original research: http://stackoverflow.com/questions/3421793/how-do-i-run-firebug-within-selenium-2
*/
private void addFirefoxExtensions(FirefoxProfile profile) {
try {
File extDir = new ClassPathResource("selenium/firefox/extensions").getFile();
// get all .xpi files from extensions directory recursively
Collection<File> extFiles = FileUtils.listFiles(extDir, new String[]{"xpi"}, true);
for (File extFile : extFiles) {
profile.addExtension(extFile);
}
} catch (IOException e) {
throw new FailureException("Unable to load Firefox extensions", e);
}
/*
* Hack to prevent FireBug from opening its Release Notes tab breaking our tests. With this property set it
* will only open that tab when FireBug version 301 is released. :-)
*/
profile.setPreference("extensions.firebug.currentVersion", "300");
if (enableFirebugDebugTabs()) {
// Firebug properties from http://getfirebug.com/wiki/index.php/Firebug_Preferences
profile.setPreference("extensions.firebug.script.enableSites", "true");
profile.setPreference("extensions.firebug.console.enableSites", "true");
}
}
}
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import java.io.File;
import java.net.URL;
/**
* Selenium Manager Class
* It should manage all Selenium actions
*/
public class Demo1SeleniumManager {
// Local path to Firefox application binary
private static final String FIREFOX_BINARY_PATH = "C:\\Program Files\\Mozilla Firefox\\firefox.exe";
// Private Global Firefox Web Driver
private WebDriver mFirefoxWebDriver;
/**
* SeleniumManager Class Constructor
*/
public Demo1SeleniumManager() {
// Acquiring GeckoDriver binary file URL
URL geckoDriverUrl = this.getClass().getClassLoader().getResource("geckodriver.exe");
if (geckoDriverUrl == null) {
System.out.println("[ ERROR ] Fail to get geckodriver.exe URL");
return;
}
// GeckoDriver Setup
System.setProperty("webdriver.gecko.driver", geckoDriverUrl.getFile());
// Firefox Application Setup
File firefoxBinaryPath = new File(FIREFOX_BINARY_PATH);
FirefoxBinary firefoxBinary = new FirefoxBinary(firefoxBinaryPath);
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setBinary(firefoxBinary);
// Instantiating Global Firefox Driver
mFirefoxWebDriver = new FirefoxDriver(firefoxOptions);
}
/**
* openFirefox Method
* It should spawn firefox browser in parameter URL
* @param Url URL to be opened in Firefox browser
*/
public void openFirefox(String Url) {
mFirefoxWebDriver.get(Url);
}
/**
* Print Current Page Info Method
*/
public void printCurrentPageInfo() {
System.out.println("\n");
System.out.println("+===============================+");
System.out.println("| Current Page Info |");
System.out.println("+===============================+");
System.out.println("URL: " + mFirefoxWebDriver.getCurrentUrl());
System.out.println("Title: " + mFirefoxWebDriver.getTitle());
System.out.println("+===============================+");
System.out.println("\n");
}
}
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.