Run Selenium automation tests on LambdaTest cloud grid
Perform automation testing on 3000+ real desktop and mobile devices online.
package com.zel.market.crawler.downloader;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
/**
* Description:
*
* @author csy
* @version 1.0.0
* @since 2021/3/27
*/
public class FirefoxDriverRequest extends SeleniumRequest {
@Override
public void init(boolean headless) {
String driverpath = this.getClass().getClassLoader().getResource("driver/geckodriver.exe").getPath();
System.setProperty("webdriver.gecko.driver", driverpath);
DesiredCapabilities cap = new DesiredCapabilities();
if (proxy != null) {
cap.setCapability(CapabilityType.PROXY, proxy);
}
cap.setAcceptInsecureCerts(true);
FirefoxBinary firefoxBinary = new FirefoxBinary();
if (headless) {
firefoxBinary.addCommandLineOptions("--headless");
}
firefoxBinary.addCommandLineOptions("--disable-web-security");
//firefoxBinary.addCommandLineOptions("");
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setBinary(firefoxBinary);
firefoxOptions.addCapabilities(cap);
synchronized (this) {
driver = new FirefoxDriver(firefoxOptions);
}
//((FirefoxDriver) driver).setLogLevel(Level.INFO);
}
@Override
public String getHtml(String url) {
driver.get(url);
String html = (String) ((FirefoxDriver) driver).executeScript("return document.documentElement.outerHTML");
return html;
}
}
package com.ericsson.ei.config;
import com.ericsson.ei.frontend.exception.OSNotSupportedException;
import com.ericsson.ei.frontend.exception.PropertiesNotLoadedException;
import com.google.common.io.Files;
import org.apache.commons.lang3.SystemUtils;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.Properties;
import java.io.File;
public class SeleniumConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(SeleniumConfig.class);
private static String propertiesPath = String.join(File.separator, "src", "functionaltest", "resources");
private static String propertiesFile = "functional-test.properties";
private static String firefoxBZip2FileUrlLinux = "";
private static File tempDownloadDirectory = Files.createTempDir();
private static FirefoxDriver driver;
public static FirefoxDriver initFirefoxDriver() throws PropertiesNotLoadedException, OSNotSupportedException {
if (driver != null) {
return driver;
}
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.download.folderList", 2);
firefoxProfile.setPreference("browser.download.dir", tempDownloadDirectory.getPath());
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/json");
FirefoxOptions firefoxOptions = new FirefoxOptions()
.setHeadless(true)
.setProfile(firefoxProfile);
boolean successfullyLoadedProperties = loadProperties();
if (!successfullyLoadedProperties) {
LOGGER.error("Properties was not properly loaded.");
throw new PropertiesNotLoadedException();
};
if (SystemUtils.IS_OS_LINUX) {
FirefoxBinary firefoxBinary = installFirefoxBinary();
firefoxOptions.setBinary(firefoxBinary);
System.setProperty("webdriver.gecko.driver", "src/functionaltest/resources/geckodriver");
} else if (SystemUtils.IS_OS_WINDOWS) {
System.setProperty("webdriver.gecko.driver", "src/functionaltest/resources/geckodriver.exe");
} else {
LOGGER.error(SystemUtils.OS_NAME + " currently not supported.");
throw new OSNotSupportedException();
}
driver = new FirefoxDriver(firefoxOptions);
//Make sure all firefox browsers are closed after all tests have finished
Runtime.getRuntime().addShutdownHook(new Thread(() -> driver.quit()));
return driver;
}
public static File getTempDownloadDirectory() {
return tempDownloadDirectory;
}
public static String getBaseUrl(int randomServerPort) {
return "http://localhost:" + randomServerPort;
}
private static FirefoxBinary installFirefoxBinary() {
String firefoxBZip2FileNameLinux = FilenameUtils.getName(firefoxBZip2FileUrlLinux);
String firefoxBZip2FilePath = String.join(
File.separator, tempDownloadDirectory.getPath(), firefoxBZip2FileNameLinux);
Utils.downloadFileFromUrlToDestination(firefoxBZip2FileUrlLinux, firefoxBZip2FilePath);
Utils.extractBZip2InDir(firefoxBZip2FilePath, tempDownloadDirectory.getPath());
File firefoxBinaryFilePath = new File(
String.join(File.separator, tempDownloadDirectory.getPath(), "firefox", "firefox"));
Utils.makeBinFileExecutable(firefoxBinaryFilePath);
FirefoxBinary firefoxBinary = new FirefoxBinary(firefoxBinaryFilePath);
return firefoxBinary;
}
private static boolean loadProperties() {
final String propertiesFilePath = String.join(File.separator, propertiesPath, propertiesFile);
final Properties properties = Utils.getProperties(propertiesFilePath);
firefoxBZip2FileUrlLinux = properties.getProperty("test.selenium.firefox.BZip2File.url.linux");
if (StringUtils.isEmpty(firefoxBZip2FileUrlLinux)) {
LOGGER.error("Failed to load properties, firefoxBZip2FileUrlLinux is not set.");
return false;
} else {
LOGGER.debug("Properties have been loaded.");
return true;
}
}
}
package qa.TestBase;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
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.DesiredCapabilities;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import qa.Listners.EventHandler;
import qa.Report.LogStatus;
public class TestBase {
public static WebDriver driver;
private TestBase()
{
String browser=TestBase.get("Browser");
String headless=TestBase.get("HeadlessMode");
String imageDisable=TestBase.get("DisableImage");
if(browser.equalsIgnoreCase("chrome")|| browser.toUpperCase().contains("CHROME"))
{
try{
System.setProperty("webdriver.chrome.driver",TestBase.getPath(browser));
ChromeOptions options=new ChromeOptions();
options.addArguments("--incognito");
if(imageDisable.equalsIgnoreCase("yes"))
{
TestBase.disableImg(options);
}
/*if(headless.equalsIgnoreCase("yes"))
{
new HeadlessMode().headless(options);
}*/
DesiredCapabilities capabilites=DesiredCapabilities.chrome();
capabilites.setCapability(ChromeOptions.CAPABILITY, options);
driver=new ChromeDriver(options);
LogStatus.pass("Chrome drive launched with headless mode = "+headless.toUpperCase()+", Image Disable mode = "+imageDisable.toUpperCase());
}
catch (Exception e)
{
e.printStackTrace();
}
}
else if (browser.equalsIgnoreCase("FF")|| browser.toUpperCase().contains("FIRE"))
{
try
{
System.setProperty("webdriver.gecko.driver",TestBase.getPath(browser));
FirefoxOptions FFoptions=new FirefoxOptions();
if(imageDisable.equalsIgnoreCase("yes"))
{
TestBase.disableImg(FFoptions);
}
if(headless.equalsIgnoreCase("yes"))
{
TestBase.headless(FFoptions);
}
driver=new FirefoxDriver(FFoptions);
LogStatus.pass("FF drive launched with headless mode = "+headless.toUpperCase()+", Image Disable mode = "+imageDisable.toUpperCase());
}
catch(Exception e)
{
e.printStackTrace();
LogStatus.fail(e);
}
}
}
//Function to read the property file and load into application
public static String get(String PropertyName)
{
String returnProperty="";
Properties property = new Properties();
try {
FileInputStream file =
new FileInputStream(System.getProperty("user.dir")+"//src//main//resources//TestRunDetails.properties");
property.load(file);
returnProperty=property.getProperty(PropertyName);
if(returnProperty==null)
{
throw new Exception("Property with name : "+PropertyName+" not found in "+System.getProperty("user.dir")+"\\src//main//resources//TestRunDetails.properties Please check again");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("File not found");
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return returnProperty;
}
// Function to set the browser executable
public static String getPath(String browser)
{
String OS=System.getProperty("os.name");
String driverPath=null;
if(OS.toUpperCase().contains("Windows"))
{
if(browser.toUpperCase().contains("CHROME"))
{
driverPath=".//src//main//resources//chromedriver.exe";
}
else if(browser.toUpperCase().contains("FF")||browser.toUpperCase().contains("FIRE"))
{
driverPath=".//src//main//resources//geckodriver.exe";
}
}
else if(OS.toUpperCase().contains("MAC"))
{
if(browser.toUpperCase().contains("CHROME"))
{
driverPath=".//src//main//resources//chromedriver";
}
else if(browser.toUpperCase().contains("FF")||browser.toUpperCase().contains("FIRE"))
{
driverPath=".//src//main//resources//geckodriver";
}
}
else
{
if(browser.toUpperCase().contains("CHROME"))
{
driverPath="/home/deepak/Jars/chromedriver";
}
else if(browser.toUpperCase().contains("FF")||browser.toUpperCase().contains("FIRE"))
{
driverPath="home/deepak/Jars/geckodriver";
}
}
return driverPath;
}
// function to set up head less options
public static void headless(ChromeOptions options)
{
options.addArguments("window-size=1400,800");
options.addArguments("headless");
}
//Configures FireFox to run in headless mode
public static void headless(FirefoxOptions options)
{
FirefoxBinary firefoxBinary=new FirefoxBinary();
firefoxBinary.addCommandLineOptions("--headless");
options.setBinary(firefoxBinary);
}
public static void disableImg(ChromeOptions options)
{
HashMap<String, Object> images=new HashMap<String, Object>();
images.put("images", 2);
HashMap<String, Object> pref=new HashMap<String, Object>();
pref.put("profile.default_content_setting_values", images);
options.setExperimentalOption("prefs", pref);
}
//disables images in Firefox browser
public static void disableImg(FirefoxOptions options)
{
FirefoxProfile profile=new FirefoxProfile();
profile.setPreference("permissions.default.image", 2);
options.setProfile(profile);
options.setCapability(FirefoxDriver.PROFILE, profile);
}
public static void initialize()
{
new TestBase();
}
//quits browser
public static void quit()
{
driver.quit();
}
//initializes WebDriver EventListner
public void EventHandlerInit()
{
EventFiringWebDriver eventHandle=new EventFiringWebDriver(driver);
EventHandler handler=new EventHandler();
eventHandle.register(handler);
driver=eventHandle;
}
}
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.