ScalaTestには非常に優れたドキュメントがありますが、それらは短く、受け入れテストの例を示していません。
ScalaTest を使用して Web アプリケーションの受け入れテストを作成するにはどうすればよいですか?
Selenium 2を使用すると、ある程度のマイレージが得られます。ここにあるSeleniumDSLのバリエーションと組み合わせてSelenium2WebDriverを使用しています。
もともと、REPLからの実行を少し簡単にするためにDSLを変更しました(以下を参照)。ただし、このようなテストを構築する際の大きな課題の1つは、テストがすぐに無効になり、維持するのが悪夢になることです。
その後、アプリケーションのすべてのページのラッパークラスの作成を開始し、そのページに送信されるイベントを基になるWebDriver
呼び出しにマッピングする便利な操作を行いました。そうすれば、下にあるページが変更されるたびに、スクリプト全体を変更するのではなく、ページラッパーを変更するだけで済みます。これで、私のテストスクリプトは、個々のページラッパーでの呼び出しの観点から表現されるようになりました。ここで、すべての呼び出しは、UIの新しい状態を反映するページラッパーを返します。かなりうまくいくようです。
私はテストを作成する傾向がありFirefoxDriver
、テストをQA環境にロールする前に、HtmlUnit
ドライバーが同等の結果を示しているかどうかを確認します。HtmlUnit
それが当てはまる場合は、ドライバーを使用してテストを実行します。
これは、SeleniumDSLに対する私の最初の変更でした。
/**
* 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 を提供するようになりました: