【发布时间】:2012-08-07 11:33:34
【问题描述】:
ScalaTest 有很好的文档,但是它们很短,没有给出一个例子 验收测试。
如何使用 ScalaTest 为 Web 应用程序编写验收测试?
【问题讨论】:
-
scalatest.org/getting_started_with_feature_spec 的页面给出了验收测试的示例。你到底在找什么?
ScalaTest 有很好的文档,但是它们很短,没有给出一个例子 验收测试。
如何使用 ScalaTest 为 Web 应用程序编写验收测试?
【问题讨论】:
使用 Selenium 2 可以获得一些里程。我将Selenium 2 WebDriver 与here 发现的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)
}
【讨论】:
ScalaTest 现在提供 Selenium DSL:
【讨论】: