Automated UI testing in a Play Scala Application using FluentLenium


In this blog I am going to demonstrate how to do automated testing of UI of a Play Scala application using FluentLenium.

FluentLenium is a framework that helps to write Selenium tests. FluentLenium provides us a fluent interface to the Selenium Web Driver. FluentLenium allows us use the assertion framework we like, either jUnit assertions or Fest-assert.

To start building testcases using FluentLenium follow the below steps:

1) Add following dependencies in build.sbt file.

"org.seleniumhq.selenium" % "selenium-java" % "2.41.0" % "test",
"org.fluentlenium" % "fluentlenium-core" % "0.9.2" % "test",
"org.fluentlenium" % "fluentlenium-festassert" % "0.9.2" % "test"

2) Make an object of the page which is to be tested. For example :-

In test folder add a file IndexPage.scala of index.scala.html page & add following code to it:

import org.fluentlenium.core.FluentPage
import org.openqa.selenium.WebDriver
import org.fluentlenium.core.filter.FilterConstructor.withText
import org.fluentlenium.core.filter.FilterConstructor.withId

object IndexPage extends FluentPage {
 var URL = ""

 def IndexPage (webDriver: WebDriver, port: Int) {
  super.initFluent(webDriver)
  this.URL = "http://localhost:" + port
 }

 override def getUrl(): String = {
  this.URL
 }

 override def isAt() {
  assert(title().equals("Sign Up"))
 }

}

This will create an Object of the Index page which can be used anywhere while making the testcases for the Application.

3) Now we can write testcases to test UI of Application. For example :-

In IntegrationTest.scala  remove anything that is written previously & add following code to test Index page in it:

import org.junit.Test
import play.test.TestBrowser
import play.libs.F.Callback
import play.test.Helpers.FIREFOX
import play.test.Helpers.inMemoryDatabase
import play.test.Helpers.fakeApplication
import play.test.Helpers.testServer
import play.test.Helpers.running
import org.fest.assertions.Assertions.assertThat

class IntegrationTest {

 @Test
 def test() {
  running(testServer(9000, fakeApplication(inMemoryDatabase())), FIREFOX,
   new Callback[TestBrowser]() {
   def invoke(browser: TestBrowser) {
    val indexPage = IndexPage.IndexPage(browser.getDriver(), 9000)
    browser.goTo(IndexPage.getUrl)
    val email = "himanshu@knoldus.com"
    val password = "123456"
    val confirm_password = "123456"
    browser.$("#email").text(email)
    browser.$("#password").text(password)
    browser.$("#confirm_password").text(confirm_password)
    browser.$("#submit").click()
    assertThat(browser.pageSource()).contains("email")
   }
  });
 }
}

For Code & Live Example click here :- FluentLenium Play Scala Example

After downloading the code just run this command :- play test

Then you can see how it runs the application on its own.

This entry was posted in Scala, Web, Play Framework and tagged , , , . Bookmark the permalink.

3 Responses to Automated UI testing in a Play Scala Application using FluentLenium

  1. Uttej G says:

    Is there any specific reason why you chose junit instead of specs2?

    • There is no specific reason to use jUnit instead of Specs2. This is just an example to show how FluentLenium works. You can use either jUnit or Specs2 for testing your application.

  2. Reblogged this on Play!ng with Scala and commented:

    This is best implementation for testing application in Play Framework.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s