Skip to content

Instantly share code, notes, and snippets.

@borodicht
Created January 9, 2025 17:06
Show Gist options
  • Save borodicht/780c2128c6c590576e7997dbdd4cfe42 to your computer and use it in GitHub Desktop.
Save borodicht/780c2128c6c590576e7997dbdd4cfe42 to your computer and use it in GitHub Desktop.
package ui.drivers;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
public class DriverManager {
private static ThreadLocal <WebDriver> driver = new ThreadLocal<>();
private DriverManager() {};
public static WebDriver getDriver() {
if (driver.get() == null) {
iniztialiazeDriver();
}
return driver.get();
}
private static void iniztialiazeDriver() {
String browser = System.getProperty("browser", "chrome");
switch (browser.toLowerCase()) {
case "chrome":
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
driver.set(new ChromeDriver(options));
case "firefox":
FirefoxOptions options1 = new FirefoxOptions();
options1.addArguments("--headless");
driver.set(new FirefoxDriver(options1));
default:
throw new IllegalArgumentException(browser);
}
}
public void tearDown() {
if(driver.get() != null) {
driver.get().quit();
driver.remove();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment