Reading Time: 2 minutes

In this blog I am going to demonstrate how to do Automated UI testing 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.