Templates and Testing in Play

Continuing to master the Play framework, this week we will talk about implementing templates and testing. We last left off Play with making a Digits application that can handle simple creation, reading, and updating of contacts. While the core functionality of the application does exist, the code can still be improved. For example, there is some repeat code for creating form fields in the HTML files. Having this section of repeat code is bad coding practice and should be replaced with a template.

WOD 1 Digits Test

The first WOD (link) is to remove the default integration testing and replace it with FluentLenium and HTMLUnit tests.  FluentLenium is an assertion framework that assists in testing with the Page Object design pattern. HTMLUnit is a library that provides a simulation for the browser. By combining these two libraries, we can write test cases that are more “realistic” and interface with the frontend of the application instead of the backend.

To do this WOD, my professor provided a demo Play application that introduces how to use the libraries effectively. By providing this demo, performing this WOD was not as difficult as I thought because most of the code I had to write could just be done by modifying the sample code from the demo.  I think the only issue I had with this WOD was that I was unfamiliar with the FluentLenium syntax for creating assertions. For example, the correct way for writing assertions is to write assertThat(pageSource()).contains(firstName) and not assertThat(pageSource().contains(firstName)). Overall, my time to finish this WOD was 29 minutes.

WOD 2 Digits Text Template

The second WOD (link) is to refactor the form fields in our NewContact page to use Scala templates to help reduce the clutter. Again, my professor provided another demo Play application that uses Scala templates in its pages. Refactoring the code was a simple matter of rewriting the HTML code using the template and filling in the required fields. My time to finish this WOD was a quick 10 minutes.

WOD 3 Digits Selection

The third WOD (link) involves using a Scala template to create a select field in our NewContact page. This select field will allow the user to choose the type of phone number (homework, mobile, or work) for each contact. Since a new field is being introduced, we also had to update the controller, models, and tests in the application. What I found difficult with this WOD was that it combined almost everything that we learned so far in creating the field. When writing the code, I had moments of being “lost” in what I was trying to do because many things had to be changed at once in order to support the new field. For example, updating the ContactFormData also requires updating the Contact model to support the new field. Since the Contact model was changed, I also had to update the controller to support it. Because everything was interconnected, making a small mistake (such as a typo) can cause repercussions in the other areas. Finding and fixing the mistake sometimes made me forget what I was trying to do in the first place. This experience taught me that I should do everything procedurally so I can have a trail I can backtrack to in case something goes wrong. Performing this WOD took me about 38 minutes to complete.

WOD 4 Digits Custom Template

The fourth WOD (link) involves creating a custom template that contains the telephone number and telephone type fields. Creating our own template was not a difficult task since it only involved modifying the previous template code. My time to finish this WOD was 12 minutes.

WOD 5 Digits Init

The last WOD (link) for this week is to initialize the application with some prior data. Play has a nifty global class we can write to initialize some data on start up. Play’s example on their website was easy to understand and I was able to implement what was needed quite quickly. The only problem I encountered was that I accidentally imported the wrong Play class through IntelliJ’s auto-import feature when overriding the onStart() method. My first try at the WOD took me 16 minutes while my second try took me 10 minutes.


Digits Repository


This week had a lot of material to cover since we wrote new test cases, introduced Scala templates, and introduced a new field. While I feel that this week wasn’t as difficult as the previous week, the amount of material that we covered was significantly higher. Especially with the third WOD since it involved everything that I knew about Play. I definitely need more practice in Play since our project for the class involves integrating everything that we learned.