Implement a WebDriver Manager

This lesson is a step-by-step guide to create a WebDriver manager.

We have to create our web driver objects in a way that it can handle parallel runs. Below code will guide you step by step on creating a web driver manager which handles WebDriver object creation.

DriverManager #


import org.openqa.selenium.WebDriver;

public class DriverManager {

	private static final ThreadLocal<WebDriver> DRIVER = new ThreadLocal<WebDriver>();

	public static WebDriver getWebDriver() {
		return DRIVER.get();
	}

	public static void setWebDriver(WebDriver driver) {
		DRIVER.set(driver);
	}

	public static void closeDriver() {
		if (DRIVER.get() != null) {
			DRIVER.get().quit();
			DRIVER.remove();
		}
	}
}
 

Get hands-on with 1000+ tech skills courses.