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

io.magentys.cinnamon.webdriver.factory.WebDriverFactory.scala Maven / Gradle / Ivy

There is a newer version: 0.2.0
Show newest version
package io.magentys.cinnamon.webdriver.factory

import java.io.File
import java.net.URL
import java.nio.file.{Files, Path, Paths}

import io.github.bonigarcia.wdm.{BrowserManager, WebDriverManager}
import io.magentys.cinnamon.webdriver.capabilities.DriverBinaryConfig
import org.openqa.selenium.WebDriver
import org.openqa.selenium.remote.{DesiredCapabilities, RemoteWebDriver}

import scala.util.Try

// helper interface around statics used in WebDriverManager
private[factory] trait WebDriverManagerFactory {
  def driverManagerClass(driverClass: Class[_ <: WebDriver]): BrowserManager = WebDriverManager.getInstance(driverClass)

  def webDriver(capabilities: DesiredCapabilities): WebDriver = DriverRegistry.locals.newInstance(capabilities)
}

class WebDriverFactory(factory: WebDriverManagerFactory) {

  /**
    * Create a new instance of a web driver
    *
    * @param capabilities the browser capabilities
    * @param hubUrl       optional hub url
    * @param exePath      optional driver exe path
    * @param binaryConfig optional driver binary configuration
    * @return
    */
  def getDriver(capabilities: DesiredCapabilities, hubUrl: Option[String], exePath: Option[String], binaryConfig: Option[DriverBinaryConfig]): WebDriver = {

    // if a hub url has been passed in then ignore WDM and return an instance of remote web driver
    if (hubUrl.isDefined && !hubUrl.get.isEmpty) {
      return new RemoteWebDriver(new URL(hubUrl.get), capabilities)
    }

    // if an exe path has been defined then check that it exists
    if (exePath.isDefined && !exePath.get.isEmpty) {
      require(Files.exists(Paths.get(exePath.get)), s"Cannot find the exe path that has been set by a webdriver property.")
    }

    val driverClass = DriverRegistry.getDriverClass(capabilities)

    if (driverClass.isDefined) {
      binaryConfig match {
        case Some(binConfig) => Try(factory.driverManagerClass(driverClass.get).version(binConfig.version).architecture(binConfig.arch).setup())
        case None => Try(factory.driverManagerClass(driverClass.get).setup())
      }
    }

    factory.webDriver(capabilities)
  }

}

object WebDriverFactory {
  def apply() = new WebDriverFactory(new WebDriverManagerFactory {})
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy