开发者

How to write an acceptance test using ScalaTest?

ScalaTest has very good documentation but they are short and do not give an example of acceptance test.

How can I write acceptance test using ScalaTe开发者_JAVA百科st for a web application?


Using Selenium 2 gets you some mileage. I'm using Selenium 2 WebDriver in combination with a variation of the Selenium DSL found here.

Originally, I changed the DSL in order to make it a little easier to run in from the REPL (see down below). However, one of the bigger challenges of building tests like these is that they quickly get invalidated, and then become a nightmare to maintain.

Later on, I started creating a wrapper class for every page in the application, with convenience operations mapping the event to be sent to that page to the underlying WebDriver invocations. That way, whenever the underling page is changing, I just need to change my page wrapper, rather than changing the entire script. With that, my test scripts are now expressed in terms of invocations on the individual page wrappers, where every invocation returns a page wrapper reflecting the new state of the UI. Seems to work out quite well.

I tend to build my tests with the FirefoxDriver and then before rolling the test to our QA environment check if the HtmlUnit driver is giving comparable results. If that holds, then I run the test using the HtmlUnit driver.

This was my original modification to the 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 now offers a Selenium DSL:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜