Making Web Tests Readable, Robust and Rapid

Tutorial: Groovy Functional Testing with Geb

Functional testing web applications effectively has always been a challenge. Tests are expensive to write, painful to read, and devilishly hard to maintain. Using Geb, a powerful Groovy browser automation tool, writing first-rate functional tests has never been easier.

I have been developing web applications my entire career. Since the beginning, I’ve held automated testing to be one of the pinnacles of software development best practices. Unit testing in particular has been part of my development process since I was an undergraduate. One of the challenges I’ve experienced when developing for the web is that traditional unit testing only goes so far. Some of the most serious bugs in a web application tend to be things a unit test has no way to check for; a JavaScript error in a particular version of a specific browser, or perhaps an API change in a third party RESTful service that your application is consuming. Detecting these kinds of regressions requires testing on a wholly different level.

Enter functional testing. Unlike unit testing, functional testing ignores the specifics of the underlying software component under test. A functional test merely asserts that providing certain input results in certain output. In the context of a web application, this usually means programmatically controlling a web browser to simulate the actions of a user on a web page. For example, a functional test might verify that navigating to a search engine page, inputting a particular search term in the search box, and then clicking the submit button takes you to a search results page that displays results with the selected term. The underlying application logic that actually performs the search is irrelevant to the test as long as the expected results show up.

This type of test can be exceptionally valuable if done correctly. If development on any component involved in the function being tested introduces a regression, the test will fail. It doesn’t matter whether the component lies in the front end (JavaScript or markup, primarily) or the back end—all of the application layers are being tested at the same time.

Unfortunately, tools for writing and executing functional tests have, historically, been notoriously cumbersome to use. Browser and environmental differences make it a challenge to run tests by developers on different computers, if they can be run at all. The intricacies of selecting and manipulating data in the DOM has also meant that, even after investing the time and effort into the writing of functional tests, they tend to be extremely brittle and hard for other developers to understand. As such, though I always yearned for my web applications to have a proper suite of automated functional tests to rely upon, I learned to do without.

Geb changed all of that. Not as clumsy or fragile as other browser automation frameworks, Geb is an elegant tool for a more civilized age. Geb’s Page Objects and Groovy DSL make tests readable to the point that they’re almost plain English. The encapsulation of content definition inside of those Page Objects also reduces test fragility by letting you reason about your web pages and components as though they were part of an object hierarchy. If the structure of a page changes, you need only update a selector in one place within your Page class—often, no changes to the test code itself are required. What’s more, Geb lets you define content using a powerful selector syntax familiar to anyone acquainted with jQuery.

In this article, I will provide an introduction to Geb and an overview of its use as a tool for functional testing. I will then present an example of Geb testing in action, and show how it can be used to mitigate problems in areas that other functional testing frameworks fall short on.


Ellery Crane

What do you think?

JAX Magazine - 2014 - 06 Exclucively for iPad users JAX Magazine on Android


Latest opinions