Skip to content

Instantly share code, notes, and snippets.

@hemanth22
Forked from marcinfkns/ChromeDriverTest.java
Created January 4, 2020 08:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hemanth22/f0f1cc34dc081cb4bef182ca6f080427 to your computer and use it in GitHub Desktop.
Save hemanth22/f0f1cc34dc081cb4bef182ca6f080427 to your computer and use it in GitHub Desktop.
Selenium + Firefoxes and other browsers
apply plugin: 'java'
/*
* gradle -DmainClassName=WebDriverTest runMain
*/
task runMain(type:JavaExec) {
main = System.getProperty("mainClassName")
classpath = sourceSets.main.runtimeClasspath
}
repositories {
mavenCentral()
}
/*
Binaries:
---------
https://github.com/mozilla/geckodriver/releases/download/v0.19.1/geckodriver-v0.19.1-macos.tar.gz
https://chromedriver.storage.googleapis.com/2.35/chromedriver_mac64.zip
*/
def SeleniumVersion = "3.11.0"
dependencies {
compile "org.seleniumhq.selenium:selenium-firefox-driver:"+SeleniumVersion
compile "org.seleniumhq.selenium:selenium-chrome-driver:"+SeleniumVersion
compile "org.seleniumhq.selenium:selenium-support:"+SeleniumVersion
compile "ru.yandex.qatools.ashot:ashot:1.5.4"
}
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class ChromeDriverTest {
// Configuration (CHANGE IT!):
private static final String ChromeDriverPath = "/Users/marcinf/bin/chromedriver/2.35/chromedriver"; // https://chromedriver.storage.googleapis.com/2.35/chromedriver_mac64.zip
private static final String ChromePath = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome";
public static WebDriver Chrome() {
System.setProperty("webdriver.chrome.driver", ChromeDriverPath);
ChromeOptions options = new ChromeOptions();
options.setBinary(ChromePath);
WebDriver driver = new ChromeDriver(options);
return driver;
}
/**
Headless Chrome (v59)
https://developers.google.com/web/updates/2017/04/headless-chrome
https://www.google.com/chrome/browser/beta.html
https://sites.google.com/a/chromium.org/chromedriver/getting-started
https://intoli.com/blog/running-selenium-with-headless-chrome/
*/
public static WebDriver ChromeHeadless() {
System.setProperty("webdriver.chrome.driver", ChromeDriverPath);
ChromeOptions options = new ChromeOptions();
options.setBinary(ChromePath);
options.addArguments("--headless", "--disable-gpu", "--window-size=1600x600");
WebDriver driver = new ChromeDriver(options);
return driver;
}
}
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
public class MarionetteDriverTest {
// Configuration (CHANGE IT!):
private static final String SmartHome = "/smart/WorkdaySmart1/";
private static final String FirefoxPath_40 = "/Applications/Firefox40.app/Contents/MacOS/firefox-bin";
private static final String FirefoxPath = "/Applications/Firefox.app/Contents/MacOS/firefox-bin";
private static final String GeckoDriverPath = "/Users/marcinf/bin/geckodriver/v0.19.1/geckodriver"; // https://github.com/mozilla/geckodriver/releases/download/v0.19.1/geckodriver-v0.19.1-macos.tar.gz
private static FirefoxProfile mkFFProfile() {
System.clearProperty("webdriver.firefox.marionette");
System.clearProperty("webdriver.gecko.driver");
System.clearProperty("webdriver.firefox.bin");
FirefoxProfile fp = new FirefoxProfile();
fp.setPreference("plugins.force.wmode", "transparent");
fp.setPreference("browser.helperApps.neverAsk.saveToDisk", "image/jpeg, application/pdf, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
File extension = new File(SmartHome+"testframework/src/main/resources/extensions/smartAutoAuth.xpi");
fp.addExtension(extension);
return fp;
}
public static WebDriver FF40() {
final FirefoxProfile fp = mkFFProfile();
FirefoxOptions ffOpts = new FirefoxOptions();
ffOpts.setProfile(fp);
ffOpts.setLegacy(true); //false: use Marionette (required for FF>=48)
ffOpts.setBinary(FirefoxPath_40);
FirefoxDriver driver = new FirefoxDriver(ffOpts);
System.out.println(driver.getCapabilities());
return driver;
}
public static WebDriver FFLatest() {
final FirefoxProfile fp = mkFFProfile();
// System.setProperty("webdriver.firefox.marionette", "true"); //necessary for running Marionette-enabled FF >= 47
System.setProperty("webdriver.gecko.driver", GeckoDriverPath);
// System.setProperty("webdriver.firefox.bin", FirefoxPath);
FirefoxOptions ffOpts = new FirefoxOptions();
ffOpts.setProfile(fp);
ffOpts.setLegacy(false); //false: use Marionette (required for FF>=48)
ffOpts.setBinary(FirefoxPath);
WebDriver driver = new FirefoxDriver(ffOpts);
return driver;
}
// Headless Firefox works on Fx55+ on Linux, and 56+ on Windows/Mac
public static WebDriver FFLatestHeadless() {
final FirefoxProfile fp = mkFFProfile();
// System.setProperty("webdriver.firefox.marionette", "true"); //necessary for running Marionette-enabled FF >= 47
System.setProperty("webdriver.gecko.driver", GeckoDriverPath);
// System.setProperty("webdriver.firefox.bin", FirefoxPath);
FirefoxOptions ffOpts = new FirefoxOptions();
ffOpts.setProfile(fp);
ffOpts.setLegacy(false); //false: use Marionette (required for FF>=48)
ffOpts.setBinary(FirefoxPath);
ffOpts.setHeadless(true);
WebDriver driver = new FirefoxDriver(ffOpts);
return driver;
}
}
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import com.google.common.io.Files;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
public class WebDriverTest {
public static void main(String[] args) throws Exception {
//test(MarionetteDriverTest.FF40(), "FF40");
//test(MarionetteDriverTest.FFLatest(), "FF"); // cleanup: ps aux | grep geckodriver ; pkill -9 geckodriver
//test(MarionetteDriverTest.FFLatestHeadless(), "FFHeadless"); // cleanup: ps aux | grep geckodriver ; pkill -9 geckodriver
//test(ChromeDriverTest.ChromeHeadless(), "ChromeHeadless");
//test(ChromeDriverTest.Chrome(), "Chrome");
parallelTests(
MarionetteDriverTest.FFLatest(), "FF_1",
MarionetteDriverTest.FFLatest(), "FF_2"
);
}
// running 2 WebDrivers in parallel:
private static void parallelTests(WebDriver driver1, String browserName1, WebDriver driver2, String browserName2) throws Exception {
Thread thread1 = new Thread(() -> {
try {
test(driver1, browserName1);
} catch (Exception e) {
e.printStackTrace();
}
});
Thread thread2 = new Thread(() -> {
try {
test(driver2, browserName2);
} catch (Exception e) {
e.printStackTrace();
}
});
thread1.start();
thread2.start();
thread1.join();
thread2.join();
}
private static void test(WebDriver driver, String browserName) throws IOException {
String browserWinHandle = driver.getWindowHandle();
driver.switchTo().window(browserWinHandle);
driver.manage().window().maximize();
driver.navigate().to("http://www.google.pl");
//System.console().readLine("Press ENTER...");
// type "kainos" in the search prompt:
WebElement el = driver.findElement(By.id("lst-ib")); //find element by ID
el.sendKeys(" kai");
el = driver.findElement(By.xpath("//*[@id='lst-ib']")); //find element by XPath
el.sendKeys("nos");
el.sendKeys(Keys.ESCAPE); // close suggestions (if any)
// hit the "Search" button:
el = driver.findElement(By.xpath("/html/body/div/div[3]/form/div[2]/div[3]/center/input[1]"));
el.click();
//WebDriver default screenshot (view port only):
File screenshot1 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
Files.move(screenshot1, new File(".", browserName+"-webdriver-scrn.png"));
//Full-page screenshots (using external library which scrolls the page and joins the partial screenshots together):
Screenshot screenshot2 = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
ImageIO.write(screenshot2.getImage(), "PNG", new File(browserName+"-FullPageScreenshot.png"));
driver.quit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment