Monday, January 28, 2013

Playing with Spray

Today I have been playing a bit with Spray. I have an idea to build something, but therefore I also need to serve some pages, so I would like Spray to be some kind of website. Starting with the spray template project I added Twirl for templating, support for serving static file and Twitter Bootstrap for a nice layout.
Now this is a nice starting point to start a web application based on Spray.

For the onces interested, I shared my new 'Spray - Twirl - Twitter Bootstrap' template project on BitBucket: https://bitbucket.org/diversit/spray-twirl-bootstrap

Have fun.

Friday, January 25, 2013

Selenium testing with ScalaTest

The latest milestone of ScalaTest, 2.0M5b, now has support for creating Selenium tests.
Creating a Selenium test is now as easy as this:
class MyServiceBrowserTest extends FlatSpec with ShouldMatchers with BeforeAndAfterAll with Chrome {

  val homepage = "http://localhost:8080"

  behavior of "MyService application"

  it should "Say Hello" in {

    go to (homepage)
    pageTitle should be("Welcome to MyService")
  }

  override protected def afterAll() {
    quit
  }
}
Just by adding with Chrome you create a Selenium test which runs in a Chrome browser. Other supported drivers are Firefox, Safari, HtmlUnit and InternetExplorer.

ScalaTest comes with a very natural dsl for creating Selenium tests. You can anything Selenium supports: click on buttons, submit forms, go back, capture screenshots, execute JavaScript, etc. See the ScalaTest Selenium page for more info about this dsl.

There are just 2 things you need to do to start working with this:
- add the required dependencies to your build.sbt
- install the appropriate browser driver. This is not necessary if you use the HtmlUnit driver.

Adding dependencies
In your build file, add the dependencies to ScalaTest and Seleniu. For example in your build.sbt add:
  "org.scalatest"       %   "scalatest_2.10" % "2.0.M5b" % "test",
  "org.seleniumhq.selenium" % "selenium-java" % "2.28.0" % "test"
Install Chrome driver
For Chrome, download the chromedriver from https://code.google.com/p/selenium/wiki/ChromeDriver.
On a Mac (or Linux I guess) just unzip the driver and put it in /usr/bin.

Done. Now create your test, add with Chrome and run your test. Only remember to quit the browser after all tests so the browser gets closed again.

For other browsers. see the Selenium downloads page for other browser drivers.