<commit_before>/*
 * Copyright 2001-2013 Artima, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * TODO Check for licensing issues as the code below is based on code found in Scalatest
 */

package helpers.webbrowser

import org.openqa.selenium.WebElement
import org.openqa.selenium.Cookie
import org.openqa.selenium.WebDriver
import org.openqa.selenium.Alert
import org.openqa.selenium.By
import org.openqa.selenium.support.ui.Select
import org.openqa.selenium.TakesScreenshot
import org.openqa.selenium.OutputType
import org.openqa.selenium.JavascriptExecutor
import java.io.File
import java.io.FileOutputStream
import java.io.FileInputStream
import java.util.Date
import java.util.concurrent.TimeUnit
import scala.collection.JavaConverters.{asScalaBufferConverter, asScalaSetConverter}
import scala.language.implicitConversions

trait WebBrowserDSL {

  class CookiesNoun

  val cookies = new CookiesNoun

  sealed abstract class SwitchTarget[T] {
    def switch(driver: WebDriver): T
  }

  final class ActiveElementTarget extends SwitchTarget[Element] {

    def switch(driver: WebDriver): Element = {
      createTypedElement(driver.switchTo.activeElement)
    }
  }

  private def createTypedElement(element: WebElement): Element = {
    if (Helper.isTextField(element))
      new TextField(element)
    else if (Helper.isTextArea(element))
      new TextArea(element)
    else if (Helper.isPasswordField(element))
      new PasswordField(element)
    else if (Helper.isEmailField(element))
      new EmailField(element)
    else if (Helper.isColorField(element))
      new ColorField(element)
    else if (Helper.isDateField(element))
      new DateField(element)
    else if (Helper.isDateTimeField(element))
      new DateTimeField(element)
    else if (Helper.isDateTimeLocalField(element))
      new DateTimeLocalField(element)
    else if (Helper.isMonthField(element))
      new MonthField(element)
    else if (Helper.isNumberField(element))
      new NumberField(element)
    else if (Helper.isRangeField(element))
      new RangeField(element)
    else if (Helper.isSearchField(element))
      new SearchField(element)
    else if (Helper.isTelField(element))
      new TelField(element)
    else if (Helper.isTimeField(element))
      new TimeField(element)
    else if (Helper.isUrlField(element))
      new UrlField(element)
    else if (Helper.isWeekField(element))
      new WeekField(element)
    else if (Helper.isCheckBox(element))
      new Checkbox(element)
    else if (Helper.isRadioButton(element))
      new RadioButton(element)
    else if (element.getTagName.toLowerCase == "select") {
      val select = new Select(element)
      if (select.isMultiple)
        new MultiSel(element)
      else
        new SingleSel(element)
    }
    else
      new Element() {
        val underlying = element
      }
  }

  object go {

    def to(url: String)(implicit driver: WebDriver) {
      driver.get(url)
    }

    def to(page: Page)(implicit driver: WebDriver) {
      driver.get(page.url)
    }
  }

  def close()(implicit driver: WebDriver) {
    driver.close()
  }

  sealed trait Query {

    val by: By

    val queryString: String

    def element(implicit driver: WebDriver): Element = {
      try {
        createTypedElement(driver.findElement(by))
      }
      catch {
        case e: org.openqa.selenium.NoSuchElementException =>
          throw new TestFailedException("Element '" + queryString + "' not found.")
      }
    }

    def findElement(implicit driver: WebDriver): Option[Element] =
      try {
        Some(createTypedElement(driver.findElement(by)))
      }
      catch {
        case e: org.openqa.selenium.NoSuchElementException => None
      }

    def findAllElements(implicit driver: WebDriver): Iterator[Element] =
      driver.findElements(by).asScala.toIterator.map { e => createTypedElement(e)}

    def webElement(implicit driver: WebDriver): WebElement = {
      try {
        driver.findElement(by)
      }
      catch {
        case e: org.openqa.selenium.NoSuchElementException =>
          throw new TestFailedException("WebElement '" + queryString + "' not found.")
      }
    }
  }

  case class IdQuery(queryString: String) extends Query {
    val by = By.id(queryString)
  }

  case class NameQuery(queryString: String) extends Query {
    val by = By.name(queryString)
  }

  case class XPathQuery(queryString: String) extends Query {
    val by = By.xpath(queryString)
  }

  // TODO: Are these case classes just to get at the val?

  case class ClassNameQuery(queryString: String) extends Query {
    val by = By.className(queryString)
  }

  case class CssSelectorQuery(queryString: String) extends Query {
    val by = By.cssSelector(queryString)
  }

  case class LinkTextQuery(queryString: String) extends Query {
    val by = By.linkText(queryString)
  }

  case class PartialLinkTextQuery(queryString: String) extends Query {
    val by = By.partialLinkText(queryString)
  }

  def id(elementId: String): IdQuery = new IdQuery(elementId)

  def name(elementName: String): NameQuery = new NameQuery(elementName)

  def find(query: Query)(implicit driver: WebDriver): Option[Element] = query.findElement

  def find(queryString: String)(implicit driver: WebDriver): Option[Element] =
    new IdQuery(queryString).findElement match {
      case Some(element) => Some(element)
      case None => new NameQuery(queryString).findElement match {
        case Some(element) => Some(element)
        case None => None
      }
    }<commit_msg>US1580: Removing redundant WebBrowserDSL. name<commit_after>/*
 * Copyright 2001-2013 Artima, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * TODO Check for licensing issues as the code below is based on code found in Scalatest
 */

package helpers.webbrowser

import org.openqa.selenium.WebElement
import org.openqa.selenium.Cookie
import org.openqa.selenium.WebDriver
import org.openqa.selenium.Alert
import org.openqa.selenium.By
import org.openqa.selenium.support.ui.Select
import org.openqa.selenium.TakesScreenshot
import org.openqa.selenium.OutputType
import org.openqa.selenium.JavascriptExecutor
import java.io.File
import java.io.FileOutputStream
import java.io.FileInputStream
import java.util.Date
import java.util.concurrent.TimeUnit
import scala.collection.JavaConverters.{asScalaBufferConverter, asScalaSetConverter}
import scala.language.implicitConversions

trait WebBrowserDSL {

  class CookiesNoun

  val cookies = new CookiesNoun

  sealed abstract class SwitchTarget[T] {
    def switch(driver: WebDriver): T
  }

  final class ActiveElementTarget extends SwitchTarget[Element] {

    def switch(driver: WebDriver): Element = {
      createTypedElement(driver.switchTo.activeElement)
    }
  }

  private def createTypedElement(element: WebElement): Element = {
    if (Helper.isTextField(element))
      new TextField(element)
    else if (Helper.isTextArea(element))
      new TextArea(element)
    else if (Helper.isPasswordField(element))
      new PasswordField(element)
    else if (Helper.isEmailField(element))
      new EmailField(element)
    else if (Helper.isColorField(element))
      new ColorField(element)
    else if (Helper.isDateField(element))
      new DateField(element)
    else if (Helper.isDateTimeField(element))
      new DateTimeField(element)
    else if (Helper.isDateTimeLocalField(element))
      new DateTimeLocalField(element)
    else if (Helper.isMonthField(element))
      new MonthField(element)
    else if (Helper.isNumberField(element))
      new NumberField(element)
    else if (Helper.isRangeField(element))
      new RangeField(element)
    else if (Helper.isSearchField(element))
      new SearchField(element)
    else if (Helper.isTelField(element))
      new TelField(element)
    else if (Helper.isTimeField(element))
      new TimeField(element)
    else if (Helper.isUrlField(element))
      new UrlField(element)
    else if (Helper.isWeekField(element))
      new WeekField(element)
    else if (Helper.isCheckBox(element))
      new Checkbox(element)
    else if (Helper.isRadioButton(element))
      new RadioButton(element)
    else if (element.getTagName.toLowerCase == "select") {
      val select = new Select(element)
      if (select.isMultiple)
        new MultiSel(element)
      else
        new SingleSel(element)
    }
    else
      new Element() {
        val underlying = element
      }
  }

  object go {

    def to(url: String)(implicit driver: WebDriver) {
      driver.get(url)
    }

    def to(page: Page)(implicit driver: WebDriver) {
      driver.get(page.url)
    }
  }

  def close()(implicit driver: WebDriver) {
    driver.close()
  }

  sealed trait Query {

    val by: By

    val queryString: String

    def element(implicit driver: WebDriver): Element = {
      try {
        createTypedElement(driver.findElement(by))
      }
      catch {
        case e: org.openqa.selenium.NoSuchElementException =>
          throw new TestFailedException("Element '" + queryString + "' not found.")
      }
    }

    def findElement(implicit driver: WebDriver): Option[Element] =
      try {
        Some(createTypedElement(driver.findElement(by)))
      }
      catch {
        case e: org.openqa.selenium.NoSuchElementException => None
      }

    def findAllElements(implicit driver: WebDriver): Iterator[Element] =
      driver.findElements(by).asScala.toIterator.map { e => createTypedElement(e)}

    def webElement(implicit driver: WebDriver): WebElement = {
      try {
        driver.findElement(by)
      }
      catch {
        case e: org.openqa.selenium.NoSuchElementException =>
          throw new TestFailedException("WebElement '" + queryString + "' not found.")
      }
    }
  }

  case class IdQuery(queryString: String) extends Query {
    val by = By.id(queryString)
  }

  case class NameQuery(queryString: String) extends Query {
    val by = By.name(queryString)
  }

  case class XPathQuery(queryString: String) extends Query {
    val by = By.xpath(queryString)
  }

  // TODO: Are these case classes just to get at the val?

  case class ClassNameQuery(queryString: String) extends Query {
    val by = By.className(queryString)
  }

  case class CssSelectorQuery(queryString: String) extends Query {
    val by = By.cssSelector(queryString)
  }

  case class LinkTextQuery(queryString: String) extends Query {
    val by = By.linkText(queryString)
  }

  case class PartialLinkTextQuery(queryString: String) extends Query {
    val by = By.partialLinkText(queryString)
  }

  def id(elementId: String): IdQuery = new IdQuery(elementId)

  def find(query: Query)(implicit driver: WebDriver): Option[Element] = query.findElement

  def find(queryString: String)(implicit driver: WebDriver): Option[Element] =
    new IdQuery(queryString).findElement match {
      case Some(element) => Some(element)
      case None => new NameQuery(queryString).findElement match {
        case Some(element) => Some(element)
        case None => None
      }
    }
{'fraction_non_alphanumeric': 0.09221447543160691, 'fraction_numerical': 0.0023240371845949536, 'mean_word_length': 3.407095830285296, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7139985', 'n_tokens_mistral': 3550, 'n_tokens_neox': 3449, 'n_words': 944}