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.
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.
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.