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

im.yagni.driveby.browser.UnSafeBrowser.scala Maven / Gradle / Ivy

The newest version!
package im.yagni.driveby.browser

import im.yagni.common.Wait._
import im.yagni.driveby.{By, Condition, Describer}
import im.yagni.driveby.driver.NakedDriver
import collection.JavaConversions._

class UnSafeBrowser private[driveby] (driver: NakedDriver) {
  def title = driver.title
  def text(by: By) = findUniqueElement(by).text
  def attribute(by: By, attribute: String) = findUniqueElement(by).attribute(attribute)

  def exists(by: By) = {
    try {
      findUniqueElement(by)
      true
    } catch {
      case e: Exception => false
    }
  }

  def findUniqueElement(by: By) = {
    val condition = UniqueElement(by)
    waitUpTo().forCondition(condition.isSatisfied(this), condition.describeFailure(this))
    driver.findElements(by).get(0)
  }

  def findUniqueInteractableElement(by: By) = {
    val condition = UniqueInteractableElement(by)
    waitUpTo().forCondition(condition.isSatisfied(this), condition.describeFailure(this))
    driver.findElements(by).get(0)
  }

  def findElements(by: By) =
    try {
      driver.findElements(by).toList
    } catch {
      case e: Exception => Nil
    }

  def findOption(selectBy: By, value: String) = findUniqueInteractableElement(selectBy).option(value)

  case class UniqueElement(by: By) extends Condition {
    import Describer._

    def expectation = expect("UniqueElement", List(by.toString))
    def isSatisfied(browser: UnSafeBrowser) = { browser.findElements(by).size == 1 }
    def describeFailure(browser: UnSafeBrowser) = { "expected 1 element" + butWas(() => browser.findElements(by).size.toString) }
  }

  case class UniqueInteractableElement(by: By) extends Condition {
    import Describer._

    override def expectation = expect("UniqueInteractableElement", List(by.toString))

    override def isSatisfied(browser: UnSafeBrowser) = {
      val elements = driver.findElements(by)
      elements.size == 1 && elements.head.isDisplayed
    }

    override def describeFailure(browser: UnSafeBrowser) = {
      "expected 1 interactable element" + butWas(() => browser.findElements(by).filter(_.isDisplayed).size.toString) }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy