Created
March 5, 2018 11:03
-
-
Save Ankit-Laddha/62faa5543b5cae85b6c4c15c8dcc5d26 to your computer and use it in GitHub Desktop.
ThreadLocal Class for Webdriver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class DriverUtils { | |
| private static final String CHROME = "chrome"; | |
| private static final String INTERNET_EXPLORER = "internetexplorer"; | |
| public static String browserName = System.getenv("browser_name") == null ? "internetexplorer" : System.getenv("browser_name"); | |
| private static DriverUtils instance = new DriverUtils(); | |
| public static ThreadLocal<WebDriver> CURRENT_DRIVER = ThreadLocal.withInitial(() -> // thread local CURRENT_DRIVER object for webdriver | |
| { | |
| if (browserName.equalsIgnoreCase(CHROME)) { | |
| return createChromeDriver(); | |
| } else if (browserName.equalsIgnoreCase(INTERNET_EXPLORER)) { | |
| return createInternetExplorerDriver(); | |
| } else { | |
| throw new RuntimeException("Unknown WebDriver browser: " + browserName); | |
| } | |
| }); | |
| public static DriverUtils getInstance() { | |
| return instance; | |
| } | |
| public WebDriver getDriver() // call this method to get the CURRENT_DRIVER object and launch the browser | |
| { | |
| //Long callingThreadID = new Long(Thread.currentThread().getId()); | |
| //System.out.println("Current thread id :'" + callingThreadID + "')); | |
| WebDriver driver = CURRENT_DRIVER.get(); | |
| CURRENT_DRIVER.set(driver); | |
| return driver; | |
| } | |
| @AfterScenario | |
| public void removeDriver() // Quits the CURRENT_DRIVER and closes the browser | |
| { | |
| if (CURRENT_DRIVER.get() != null) { | |
| CURRENT_DRIVER.get().quit(); | |
| } | |
| CURRENT_DRIVER.remove(); | |
| } | |
| private static WebDriver createChromeDriver() { | |
| if (PlatformUtil.isMac()) { | |
| System.setProperty("webdriver.chrome.driver", System.getenv("webdriver_chrome_driver_mac")); | |
| } | |
| if (PlatformUtil.isWindows()) { | |
| System.setProperty("webdriver.chrome.driver", System.getenv("webdriver_chrome_driver_windows")); | |
| } | |
| ChromeOptions opts = new ChromeOptions(); | |
| opts.addArguments("disable-extensions"); | |
| opts.addArguments("--window-size=32000,32000"); | |
| return new ChromeDriver(opts); | |
| } | |
| private static WebDriver createInternetExplorerDriver() { | |
| System.setProperty("webdriver.ie.driver", System.getenv("webdriver_ie_driver")); | |
| InternetExplorerOptions opts = new InternetExplorerOptions(); | |
| opts.ignoreZoomSettings(); | |
| opts.introduceFlakinessByIgnoringSecurityDomains(); | |
| opts.destructivelyEnsureCleanSession(); | |
| opts.addCommandSwitches("--start-maximized"); | |
| return new InternetExplorerDriver(opts); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment