Skip to content
Workshop for UI testing with Geb and Spock.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Geb Testing Workshop

Build Status

Workshop for UI testing with Geb and Spock. The workshop won't go into any detail about Spock or testing in Groovy, so if you realize you don't understand the general structure of the test files, please checkout Spock manual, the Groovy documentation or my Spock Testing Workshop first ;).

Project Setup with Maven

In order to get Spock and Geb into our Maven project, we need to do the following in our [pom.xml].

  1. Add Groovy as a (test) dependency (see pom.xml).
  2. Add Spock dependency (see pom.xml).
  3. Add Geb dependency (see pom.xml).
  4. Add Selenium dependency (see pom.xml).
  5. Add something to actually get a WebDriver implementation. In this project I chose WebDriverManager, which can easily be utilized in Geb's configuration (see pom.xml). It is used in GebConfig to download and setup the driver binary (e.g. see GebConfig).
  6. Make Maven aware of /src/test/groovy being a test source directory since Maven’s default is /src/test/java (see pom.xml).
  7. Add GMavenPlus to compile Groovy sources, since Maven’s default settings only compile Java (see pom.xml).
    There are several alternatives to get Groovy code compiled in Maven. See the Groovy documentation on Groovy Maven integration for an overview.
  8. Make Maven Failsafe plugin aware of files ending with *UiSpec are test class files since its default is *IT and execute it during integration-test phase (see pom.xml).

Geb Configuration

This project already contains a GebConfig file. If you'd like to adjust the configuration for your own projects, please refer the the Geb manual config section.

Engine Check

In general this project should work just fine. However, we are dealing with quite a stack of technology here: you are using Geb, which relies on WebDriver to automate a browser that you have installed on your system (and which may or may not be of the latest version for your system).

So lets check if everything works fine first:

  • Open GoogleUiSpec and execute it via your IDE. If this did not work for you, please check the following things:
    • Can you (manually) reach the Google start page? If not, please check your internet connection.
    • Did your IDE understand the project structure? Is it fit for Groovy? It should generally work out of the box for IntelliJ Idea.
    • If your browser just starts and then does nothing, you probably need a new version of Selenium. So change the version in the pom.xml. Notice that you probably will also need a new version of WebDriverManager (see pom.xml). You can also try to configure a different browser in the GebConfig.
    • If the test fails but generally executes, the structure of the Google start page might have changed and the test needs adjustments. Please check for issues and feel free to raise a new one.
    • In any other case, please raise an issue.

Part 1: The Browser

First we'll use the Browser instance provided by GebReportingSpec to navigate to the main Google page at

  • Add a new feature method to GoogleUiSpec, that checks if "the Google logo can be found on the Google start page".
  • Write another one to check if "the main search input field can be found".

Part 2: Pages

If you did not refactor your test code, you now have at least one duplicate line of code for navigating to the Google page. Also you have put some knowledge about the page structure into your test, which will probably keep being duplicated: the selector for the main search input.

  • Create a GoogleStartPage class containing the URL. Now change your features to use to instead of go for navigating to the page.
  • Add an at check to your page. Use your selector for the bigger Google logo for this. Now shorten your first feature method to one single call.
  • Move your selector for the main search input field to the page's content. Refactor your features to use the GoogleStartPage and contain no more knowledge about the HTML structure.

Part 3: More interaction and waiting

Web pages often contain dynamic elements these days. While these may appear immediately to your eye, they actually appear at an undefined time (e.g. not after the browser regards the page as loaded).

  • Add a new feature method, which checks if "search suggestions are shown on typing one character".
    Note that the suggestions appear quite fast but not immediately. You might need to waitFor it.
  • Write a feature method to verify that "the search input content is changed to a suggestion selected by down key"
  • Write another feature method to check that "clicking on a suggestion opens the result page for the suggestion". Create a GoogleResultsPage with an at check for this test.
  • Add an url to your GoogleResultsPage, then create another feature method, which tests that "the results page can be reached via URL".

Part 4: Configuration

Let's explore the possibilities of the GebConfig file.

  • Configure the baseUrl in the GebConfig script and change your GoogleStartPage to rely on that.
  • Configure a reportsDir and make your GoogleUiSpec extend GebReportingSpec instead of GebSpec. Run your tests and check the directory.
  • Make Geb only create a report for failed tests. Check the effects in the reports directory.

Part 5: Modules

Web application often contain an element multiple times. E.g. quite many pages contain elements (text fields, checkboxes, radio buttons, buttons). Geb Modules describing the structure of those elements and hold possible interactions with them. It also allows to create your own Module classes.

  • Use a TextInput module for the search input field. Understand how this might be useful.
  • Create a ResultModule allows to access search result's title and URL. Add a list of ResultModules to your GoogleResultsPage. Add a feature to your GoogleUiSpec to check that "when searching for 'Wiki', the top result is 'Wikipedia'".
  • Create a SuggestionModule, which allows to get the text actually typed and the supplement assumed by Google. Write a test verifying that "all suggestions start with the user typed text"

Helpful Resources and Further Reading

You can’t perform that action at this time.