Geb: Groovy Functional Testing on the Web

DZone 's Guide to

Geb: Groovy Functional Testing on the Web

· Java Zone ·
Free Resource
Have you ever wanted an automated, cross-browser testing tool for Groovy that also works as a screen scraping and process automation tool?  If you have, then you should consider test-driving Geb (pronounced "jeb").  The open source browser automation solution just made it to version 0.4.  Geb has a strong focus on web testing with first class support for Spock and JUnit.  It can also integrate with Grails.

Let me introduce you to Geb:
import geb.*

Browser.drive("http://www.google.com") {
assert title == "Google"

$("input", name: "q").value("wikipedia")
$("input", value: "Google Search").click()

assert title.endsWith("Google Search")

def firstResultLink = $("li.g", 0).find("a.l")
assert firstResultLink.text() == "Wikipedia, the free encyclopedia"

This basic example uses Geb without defining page objects - a more powerful feature.  The code will navigate to Google, search for "Wikipedia", and verify that the first result is for Wikipedia.  Geb can be used by itself for scripting or within an application's codebase.  

Geb combines the expressiveness of Groovy with WebDriver and a jQuery-inspired content navigation/inspection API .  It is compatible with IE, Firefox, Chrome, and HTMLUnit.  Some of Geb's more powerful features include page objects, modules, and a Content DSL that can be used for modules or pages.

About the page object pattern, the documentation states:

The implementation of how to search and how to find the results is going to have to be duplicated in every test, maybe many times per test. The Page Object Pattern allows us to apply the same principles of modularity, reuse and encapsulation that we use in other aspects of programming. … As anyone who has tried to knows, maintaining a large suite of functional web tests for a changing application can become an expensive and frustrating process. A core philosophy of Geb is to address this issue through its content definition DSL.

The Content Domain-Specific Language module is for defining reusable content templates.  There are no restrictions on what arguments can be passed to content templates.  The Content DSL used for modules is exactly the same as the one used for pages.

To learn more, check out the documentation and give it a try!

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}