public static DesiredCapabilities of(Browser browser) {
    if (browser == null) {
      return null;
    }

    DesiredCapabilities caps;

    switch (browser) {
      case chrome:
        caps = DesiredCapabilities.chrome();
        break;

      case ff:
        caps = DesiredCapabilities.firefox();
        //        caps.setCapability(FirefoxDriver.MARIONETTE,
        //
        // Boolean.getBoolean(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE));
        break;

      case htmlunit:
        caps = DesiredCapabilities.htmlUnit();
        caps.setJavascriptEnabled(false);
        break;

      case htmlunit_js:
        caps = DesiredCapabilities.htmlUnit();
        caps.setJavascriptEnabled(true);
        break;

      case ie:
        caps = DesiredCapabilities.internetExplorer();
        break;

      case operablink:
        caps = DesiredCapabilities.operaBlink();
        break;

      case phantomjs:
        caps = DesiredCapabilities.phantomjs();
        break;

      case safari:
        caps = DesiredCapabilities.safari();
        break;

      default:
        throw new RuntimeException("Cannot determine browser config to use");
    }

    String version = System.getProperty("selenium.browser.version");
    if (version != null) {
      caps.setVersion(version);
    }

    caps.setCapability(HAS_NATIVE_EVENTS, Boolean.getBoolean("selenium.browser.native_events"));

    return caps;
  }
public class DefaultDriverSessions implements DriverSessions {

  private static final Logger LOG = Logger.getLogger(DefaultDriverSessions.class.getName());

  private final DriverFactory factory;
  private final Clock clock;

  protected final Map<SessionId, Session> sessionIdToDriver = new ConcurrentHashMap<>();

  private static List<DriverProvider> defaultDriverProviders =
      new ImmutableList.Builder<DriverProvider>()
          .add(
              new DefaultDriverProvider(
                  DesiredCapabilities.chrome(), "org.openqa.selenium.chrome.ChromeDriver"))
          .add(
              new DefaultDriverProvider(
                  DesiredCapabilities.firefox(), "org.openqa.selenium.firefox.FirefoxDriver"))
          .add(
              new DefaultDriverProvider(
                  DesiredCapabilities.internetExplorer(),
                  "org.openqa.selenium.ie.InternetExplorerDriver"))
          .add(
              new DefaultDriverProvider(
                  DesiredCapabilities.opera(), "com.opera.core.systems.OperaDriver"))
          .add(
              new DefaultDriverProvider(
                  DesiredCapabilities.operaBlink(), "org.openqa.selenium.opera.OperaDriver"))
          .add(
              new DefaultDriverProvider(
                  DesiredCapabilities.safari(), "org.openqa.selenium.safari.SafariDriver"))
          .add(
              new DefaultDriverProvider(
                  DesiredCapabilities.phantomjs(), "org.openqa.selenium.phantomjs.PhantomJSDriver"))
          .add(
              new DefaultDriverProvider(
                  DesiredCapabilities.htmlUnit(), "org.openqa.selenium.htmlunit.HtmlUnitDriver"))
          .build();

  public DefaultDriverSessions() {
    this(Platform.getCurrent(), new DefaultDriverFactory());
  }

  public DefaultDriverSessions(DriverFactory factory) {
    this(Platform.getCurrent(), factory);
  }

  protected DefaultDriverSessions(Platform runningOn, DriverFactory factory) {
    this(runningOn, factory, new SystemClock());
  }

  protected DefaultDriverSessions(Platform runningOn, DriverFactory factory, Clock clock) {
    this.factory = factory;
    this.clock = clock;
    registerDefaults(runningOn);
    registerServiceLoaders(runningOn);
  }

  private void registerDefaults(Platform current) {
    for (DriverProvider provider : defaultDriverProviders) {
      registerDriverProvider(current, provider);
    }
  }

  private void registerServiceLoaders(Platform current) {
    for (DriverProvider provider : ServiceLoader.load(DriverProvider.class)) {
      registerDriverProvider(current, provider);
    }
  }

  private void registerDriverProvider(Platform current, DriverProvider provider) {
    Capabilities caps = provider.getProvidedCapabilities();
    if (!platformMatches(current, caps)) {
      LOG.info(
          String.format(
              "Driver provider %s registration is skipped:%n"
                  + "registration capabilities %s does not match the current platform %s",
              provider, caps, current));
      return;
    }

    factory.registerDriverProvider(provider);
  }

  private boolean platformMatches(Platform current, Capabilities caps) {
    return caps.getPlatform() == null
        || caps.getPlatform() == Platform.ANY
        || current.is(caps.getPlatform());
  }

  public void registerDriver(Capabilities capabilities, Class<? extends WebDriver> driverClass) {
    factory.registerDriverProvider(new DefaultDriverProvider(capabilities, driverClass));
  }

  public SessionId newSession(Capabilities desiredCapabilities) throws Exception {
    SessionId sessionId = new SessionId(UUID.randomUUID().toString());
    Session session = DefaultSession.createSession(factory, clock, sessionId, desiredCapabilities);

    sessionIdToDriver.put(sessionId, session);

    return sessionId;
  }

  public Session get(SessionId sessionId) {
    return sessionIdToDriver.get(sessionId);
  }

  public void deleteSession(SessionId sessionId) {
    final Session removedSession = sessionIdToDriver.remove(sessionId);
    if (removedSession != null) {
      removedSession.close();
    }
  }

  public Set<SessionId> getSessions() {
    return Collections.unmodifiableSet(sessionIdToDriver.keySet());
  }
}