Organizing User Data In Your Geb Tests

Jan 4, 2014

gebGeb offers a number of conveniences for writing Selenium Webdriver tests but it draws the line at organizing data. Lacking a sanctioned solution, I rolled my own, making use of Groovy maps (hashes).

Here's an example of how I've been organizing my user data. The following code is a working example (thanks Moodle!) and you can download the full example code from GitHub.

package data

class UserData {
    // user role maps...
    static admin = \[username: 'admin', password: 'sandbox', fullname: 'Admin User'\]
    static manager = \[username: 'manager', password: 'sandbox'\]
    static teacher = \[username: 'teacher', password: 'sandbox', fullname: 'Terri Teacher'\]
    static student = \[username: 'student', password: 'sandbox'\]


Pretty simple. I created a package named data, added a UserData class file to it, and filled it with four maps, one for each user our example app.

Then we use it thusly...

import data.UserData
import geb.spock.GebReportingSpec
import pages.MoodleSandboxPage

class MoodleSandboxLoginSpec extends GebReportingSpec {
    static admin = UserData.admin

    def 'login to Moodle Sandbox'() {
        to MoodleSandboxPage


        and: 'if browser window is < 980px, expand navbar button'

        then: 'assert user logged in'
        // this currently fails on safari because bug in driver and/or moodle...
        loggedInAs.text() == admin.fullname

We assign one of the user maps to a static property, admin(line 6), then use that property to log in (line 13), and then to verify we're logged in (line 20).

This approach can really help improve code readability and offers one location to organize all of your data. It can also help with test maintenance, as changes to data need not require updating your tests. This works well for user data but should work with just about any kind of data.