Documentation

You are viewing the documentation for the 2.1.3 release in the 2.1.x series of releases. The latest stable release series is 3.0.x.

§Writing functional tests

§Testing a template

Since a template is a standard Scala function, you can execute it from your test, and check the result:

"render index template" in {
  val html = views.html.index("Coco")
  
  contentType(html) must equalTo("text/html")
  contentAsString(html) must contain("Hello Coco")
}

§Testing your controllers

You can call any Action code by providing a FakeRequest:

"respond to the index Action" in {
  val result = controllers.Application.index("Bob")(FakeRequest())
  
  status(result) must equalTo(OK)
  contentType(result) must beSome("text/html")
  charset(result) must beSome("utf-8")
  contentAsString(result) must contain("Hello Bob")
}

§Testing the router

Instead of calling the Action yourself, you can let the Router do it:

"respond to the index Action" in {
  val Some(result) = route(FakeRequest(GET, "/Bob"))
  
  status(result) must equalTo(OK)
  contentType(result) must beSome("text/html")
  charset(result) must beSome("utf-8")
  contentAsString(result) must contain("Hello Bob")
}

§Starting a real HTTP server

Sometimes you want to test the real HTTP stack from with your test, in which case you can start a test server:

"run in a server" in new WithServer {
  await(WS.url("http://localhost:" + port).get).status must equalTo(OK)
}

The port value contains the port number the server is running on, by default this is 19001, however you can change
this either by passing the port into the with WithServer constructor, or by setting the system property
testserver.port. This can be useful for integrating with continuous integration servers, so that ports can be
dynamically reserved for each build.

A custom FakeApplication can also be passed to the test server, for example:

"run in a server" in new WithServer(port = 3333, app = FakeApplication(additionalConfiguration = inMemoryDatabase)) {
  await(WS.url("http://localhost:3333").get).status must equalTo(OK)
}

§Testing from within a Web browser.

If you want to test your application using a browser, you can use Selenium WebDriver. Play will start the WebDriver for your, and wrap it in the convenient API provided by FluentLenium.

"run in a browser" in new WithBrowser {
  browser.goTo("/")
  browser.$("#title").getTexts().get(0) must equalTo("Hello Guest")
    
  browser.$("a").click()
    
  browser.url must equalTo("/")
  browser.$("#title").getTexts().get(0) must equalTo("Hello Coco")
}

Like WithServer, you can change the port, FakeApplication, and you can also select the web browser to use:

"run in a browser" in new WithBrowser(browser = FIREFOX) {
  ...
}

Next: Advanced topics