【问题标题】:How to write an acceptance test using ScalaTest?如何使用 ScalaTest 编写验收测试?
【发布时间】:2012-08-07 11:33:34
【问题描述】:

ScalaTest 有很好的文档,但是它们很短,没有给出一个例子 验收测试。

如何使用 ScalaTest 为 Web 应用程序编写验收测试?

【问题讨论】:

标签: scala scalatest


【解决方案1】:

使用 Selenium 2 可以获得一些里程。我将Selenium 2 WebDriverhere 发现的Selenium DSL 的变体结合使用。

最初,我更改了 DSL 以使其更容易从 REPL 运行(见下文)。然而,构建此类测试的更大挑战之一是它们很快就会失效,然后成为维护的噩梦。

后来,我开始为应用程序中的每个页面创建一个包装类,使用便捷操作将要发送到该页面的事件映射到底层的WebDriver 调用。这样,每当底层页面发生变化时,我只需要更改我的页面包装器,而不是更改整个脚本。有了这个,我的测试脚本现在以对单个页面包装器的调用表示,每个调用都返回一个反映 UI 新状态的页面包装器。似乎效果很好。

我倾向于使用 FirefoxDriver 构建我的测试,然后在将测试滚动到我们的 QA 环境之前检查 HtmlUnit 驱动程序是否提供了可比较的结果。如果这成立,那么我使用 HtmlUnit 驱动程序运行测试。

这是我对 Selenium DSL 的原始修改:

/**
 * Copied from [[http://comments.gmane.org/gmane.comp.web.lift/44563]], adjusting it to no longer be a trait that you need to mix in,
 * but an object that you can import, to ease scripting.
 *
 * With this object's method imported, you can do things like:
 *
 * {{"#whatever"}}: Select the element with ID "whatever"
 * {{".whatever"}}: Select the element with class "whatever"
 * {{"%//td/em"}}: Select the "em" element inside a "td" tag
 * {{":em"}}: Select the "em" element
 * {{"=whatever"}}: Select the element with the given link text
 */
object SeleniumDsl {

  private def finder(c: Char): String => By = s => c match {
    case '#' => By id s
    case '.' => By className s
    case '$' => By cssSelector s
    case '%' => By xpath s
    case ':' => By name s
    case '=' => By linkText s
    case '~' => By partialLinkText s
    case _ => By tagName c + s
  }

  implicit def str2by(s: String): By = finder(s.charAt(0))(s.substring(1))

  implicit def by2El[T](t: T)(implicit conversion: (T) => By, driver: WebDriver): WebElement = driver / (conversion(t))

  implicit def el2Sel[T <% WebElement](el: T): Select = new Select(el)

  class Searchable(sc: SearchContext) {
    def /[T <% By](b: T): WebElement = sc.findElement(b)

    def /?[T <% By](b: T): Box[WebElement] = tryo(sc.findElement(b))

    def /+[T <% By](b: T): Seq[WebElement] = sc.findElements(b)
  }

  implicit def scDsl[T <% SearchContext](sc: T): Searchable = new Searchable(sc)

}

【讨论】:

    【解决方案2】:

    ScalaTest 现在提供 Selenium DSL:

    http://www.scalatest.org/user_guide/using_selenium

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-20
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      • 2018-11-21
      • 2013-01-22
      • 2018-12-15
      • 2011-11-05
      相关资源
      最近更新 更多