Skip to content

Instantly share code, notes, and snippets.

@pragmatictesters
Last active July 14, 2022 08:22
Show Gist options
  • Save pragmatictesters/269316979341885b309d17cb8e4a63f0 to your computer and use it in GitHub Desktop.
Save pragmatictesters/269316979341885b309d17cb8e4a63f0 to your computer and use it in GitHub Desktop.
Selenium Browser Manager
package com.pragmatic.cucumber;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.opera.OperaDriver;
import org.openqa.selenium.safari.SafariDriver;
import java.util.HashMap;
import java.util.Map;
public class BrowserManager {
private static WebDriver driver;
private static final String BROWSER_TYPE = "iPad Air";
public static void setupBrowserDriver() {
switch (BROWSER_TYPE.toLowerCase()) {
case "chrome" , "iphone 4", "iphone 6", "chrome-headless", "ipad air"-> {
WebDriverManager.chromedriver().setup();
}
case "firefox", "firefox-headless" -> {
WebDriverManager.firefoxdriver().setup();
}
case "safari" -> {
WebDriverManager.safaridriver().setup();
}
case "opera" -> {
WebDriverManager.operadriver().setup();
}
case "ie" -> {
WebDriverManager.iedriver().setup();
}
case "edge" -> {
WebDriverManager.edgedriver().setup();
}
default -> {
System.out.println(String.format("Browser %s is not supported or invalid browser type", BROWSER_TYPE));
System.exit(128);
}
}
}
public static WebDriver launchBrowser() {
switch (BROWSER_TYPE.toLowerCase()) {
case "chrome" -> {
driver = new ChromeDriver();
}
case "firefox" -> {
driver = new FirefoxDriver();
}
case "safari" -> {
driver = new SafariDriver();
}
case "opera" -> {
driver = new OperaDriver();
}
case "ie" -> {
driver = new InternetExplorerDriver();
}
case "edge" -> {
driver = new EdgeDriver();
}
case "chrome-headless" -> {
ChromeOptions options = new ChromeOptions();
options.setHeadless(true);
driver = new ChromeDriver(options);
}
case "iphone 6","iphone 4" , "ipad air"-> {
Map<String, String> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceName", BROWSER_TYPE);
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
driver = new ChromeDriver(chromeOptions);
}
case "firefox-headless" -> {
FirefoxOptions options = new FirefoxOptions();
options.setHeadless(true);
driver = new FirefoxDriver(options);
}
default -> {
System.out.println(String.format("Browser %s is not supported or invalid browser type", BROWSER_TYPE));
System.exit(-1);
}
}
maximize();
return driver;
}
public static void maximize() {
driver.manage().window().maximize();
}
public static void minimize() {
driver.manage().window().minimize();
}
public static void quitBrowser() {
if (driver != null) {
driver.quit();
}
}
public static WebDriver getDriver() {
return driver;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment