2011-07-13 52 views
4

ScalaTest有非常好的文档,但他们很短,并没有给出一个 验收测试的例子。如何使用ScalaTest编写验收测试?

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

+7

上的网页http://www.scalatest.org/getting_started_with_feature_spec给出了一个验收测试的例子。你在找什么? – paradigmatic

回答

3

使用Selenium 2可以获得一定的里程数。我将Selenium 2 WebDriver与Selenium DSL的变体here结合使用。

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

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

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

这是我原来的修改硒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) 

}