Commits

Micha Kops committed 4b2336e

Initial import.

Comments (0)

Files changed (5)

+.project
+.settings
+.classpath
+.cache
+target
+project
+infinitest.filters
+# Scala Test Examples
+
+Some short examples on how to write test scenarios with Scala, Scala Test and Selenium WebDriver and how to add JUnit support.
+
+Frameworks used
+* scalatest 2.0.M5
+* junit 4.11
+* selenium-java 2.25.0
+
+Please feel free to take a look at my blog at [www.hascode.com] for the full tutorial and resources.
+
+---
+
+**2012 Micha Kops / hasCode.com**
+
+   [www.hascode.com]:http://www.hascode.com/
+name := "scala-test-examples"
+
+organization := "com.hascode.tutorial"
+
+version := "1.0.0"
+
+scalaVersion := "2.9.1"
+
+libraryDependencies += "org.scalatest" %% "scalatest" % "2.0.M5" % "test"
+
+libraryDependencies += "junit" % "junit" % "4.11" % "test"
+
+libraryDependencies += "org.seleniumhq.selenium" % "selenium-java" % "2.25.0" % "test"

src/test/scala/com/hascode/StringCalculatorSpec.scala

+package com.hascode
+import org.junit.runner.RunWith
+import org.scalatest.junit.JUnitRunner
+import org.scalatest.FlatSpec
+import org.scalatest.matchers.ShouldMatchers
+
+/**
+ * http://nimblepros.com/media/36628/string%20calculator%20kata.pdf
+ */
+@RunWith(classOf[JUnitRunner])
+class StringCalculatorSpec extends FlatSpec with ShouldMatchers {
+
+  "calc" should "return 0 given an emtpy string" in {
+    calc("", "") should equal(0)
+  }
+
+  it should "return 1 given a string '1' solely" in {
+    calc("1", "") should equal(1)
+    calc("", "1") should equal(1)
+  }
+
+  it should "return a 2 given two strings '1'" in {
+    calc("1", "1") should equal(2)
+  }
+
+  it should "return a 3 given a '1' and a string '2'" in {
+    calc("1", "2") should equal(3)
+  }
+
+  it should "return a 8 given three strings '1', '3', '4'" in {
+    calc("1", "3", "4") should equal(8)
+  }
+
+  it should "return 0 if all strings are empty" in {
+    calc("", "") should equal(0)
+    calc("", "", "") should equal(0)
+    calc("", "", "", "") should equal(0)
+    calc("", "", "", "", "") should equal(0)
+  }
+
+  it should "return a 64 given five strings '1','3','7','20','33'" in {
+    calc("1", "3", "7", "20", "33") should equal(64)
+  }
+
+  def calc(in: String*): Int = {
+    var ret = 0
+    in.filter(_ != "").foreach(ret += _.toInt)
+    ret
+  }
+}
+

src/test/scala/it/WebsiteSpec.scala

+package it
+import org.junit.runner.RunWith
+import org.openqa.selenium.firefox.FirefoxDriver
+import org.openqa.selenium.WebDriver
+import org.scalatest.junit.JUnitRunner
+import org.scalatest.matchers.ShouldMatchers
+import org.scalatest.selenium.WebBrowser
+import org.scalatest.FlatSpec
+import org.scalatest.GivenWhenThen
+
+@RunWith(classOf[JUnitRunner])
+class WebsiteSpec extends FlatSpec with GivenWhenThen with ShouldMatchers with WebBrowser {
+  implicit val webDriver: WebDriver = new FirefoxDriver
+
+  "My Website" should "load the start page correctly" in {
+    given("a browser")
+
+    when("the browser loads the home page")
+    go to ("http://www.hascode.com/")
+
+    then("the title should equal 'hasCode.com'")
+    title should be("hasCode.com")
+  }
+
+  ignore should "search for a given term" in {
+    given("we're on the start page")
+
+    when("the 'jee' is entered in the search box")
+    click on id("s")
+    textField("s").value = "jee"
+    submit()
+
+    then("the search results page should be displayed")
+    title should be("hasCode.com [» Search Results »] jee")
+  }
+}