Skip to content

Driver Manager

Simon Sai edited this page Sep 23, 2024 · 7 revisions

In Selenium Java, Driver Manager refers to a utility or class responsible for managing the lifecycle and configuration of WebDriver instances (like ChromeDriver, FirefoxDriver, etc.). The Driver Manager helps abstract away the repetitive tasks of setting up and tearing down WebDriver objects. It improves code reusability, reduces duplication, and makes it easier to manage driver configurations.

(All the examples below are not from the framework; they are simple versions to help understand the concept)

Purpose and Role of Driver Manager

  1. Simplifying WebDriver Initialization
  • Driver Manager centralizes the logic for WebDriver creation and configuration. Instead of instantiating WebDriver objects manually in every test class, you use the Driver Manager to do this consistently and efficiently. Example:
public class DriverManager {
    private WebDriver driver;

    public WebDriver getDriver() {
        if (driver == null) {
            // Example with ChromeDriver
            System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
            driver = new ChromeDriver();
        }
        return driver;
    }

    public void quitDriver() {
        if (driver != null) {
            driver.quit();
            driver = null;
        }
    }
}
  1. Managing WebDriver Sessions
  • It ensures that WebDriver sessions are properly created and closed. The Driver Manager is typically responsible for closing the driver instance (quit()) after test execution to free up resources and prevent memory leaks.
  1. Supporting Multiple Browsers
  • A well-designed Driver Manager can manage different browser types based on configurations, making the tests more flexible. For instance, based on an environment variable or configuration file, it can switch between Chrome, Firefox, Edge, etc. Example:
public WebDriver getDriver(String browser) {
    if (browser.equalsIgnoreCase("chrome")) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
    } else if (browser.equalsIgnoreCase("firefox")) {
        System.setProperty("webdriver.gecko.driver", "path/to/geckodriver");
        driver = new FirefoxDriver();
    }
    return driver;
}
  1. Handling Driver Configuration and Timeouts
  • Driver Manager can also manage common WebDriver configurations, such as setting timeouts (implicit or explicit waits), window maximization, and cookies management. Example:
public WebDriver getDriver() {
    if (driver == null) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
    }
    return driver;
}
  1. Centralized Driver Management in Parallel Testing
  • When running tests in parallel (e.g., using TestNG or JUnit), the Driver Manager ensures that each thread or test gets its own WebDriver instance, preventing test interference.
  • In parallel test execution, using ThreadLocal is common to ensure that each test has its own isolated WebDriver instance.
  • Example:
public class DriverManager {
    private static ThreadLocal<WebDriver> driver = new ThreadLocal<>();

    public static WebDriver getDriver() {
        if (driver.get() == null) {
            // Example with ChromeDriver
            System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
            driver.set(new ChromeDriver());
        }
        return driver.get();
    }

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

Benefits of Using a Driver Manager

  • Code Reusability: Centralizing WebDriver instantiation reduces code duplication across different tests.
  • Maintainability: Managing WebDriver configurations and browser setups from one location makes it easier to update or modify settings (e.g., changing browser versions).
  • Parallel Execution: Ensures proper management of WebDriver instances in parallel test runs by isolating driver instances per test/thread.
  • Consistency: Ensures that every test session starts with a properly configured WebDriver, reducing inconsistencies across test runs.