All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rationaleemotions.web.DriverFactory Maven / Gradle / Ivy

package com.rationaleemotions.web;

import com.google.common.base.Preconditions;
import com.rationaleemotions.internal.utils.ReflectionHelper;
import org.openqa.selenium.remote.RemoteWebDriver;

public final class DriverFactory {
  static final String JVM_ARG = "creator";
  private static final String className =
      System.getProperty(JVM_ARG, NoOperationalBrowserCreator.class.getName());
  private static final ThreadLocal driverThreadLocal = new ThreadLocal<>();
  private static final IBrowserCreator creator = getCreator();

  private DriverFactory() {
    // Factory like implementation. Defeat instantiation.
  }

  public static RemoteWebDriver getDriver() {
    Preconditions.checkNotNull(driverThreadLocal.get(), "No valid webdriver found for the current thread.");
    return driverThreadLocal.get();
  }

  private static void setDriver(RemoteWebDriver driver) {
    driverThreadLocal.set(driver);
  }

  public static void createDriver() {
    if (driverThreadLocal.get() == null) {
      setDriver(creator.createDriver());
    }
  }

  /** Quits the driver object if exists */
  public static void quitDriver() {
    RemoteWebDriver driver = driverThreadLocal.get();
    if (driver != null) {
      driver.quit();
    }
    setDriver(null);
  }

  private static IBrowserCreator getCreator() {
    return ReflectionHelper.newInstance(className);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy