GEB

Very Groovy browser automation. Web Testing, Screen Scraping and more

7.0
Latest Release
-
Stars
-
Forks
-
Watchers

What is it?

Geb is a browser automation solution.

It brings together the power of WebDriver, the elegance of jQuery content selection, the robustness of Page Object modelling and the expressiveness of the Groovy language.

It can be used for scripting, scraping and general automation — or equally as a functional/web/acceptance testing solution via integration with testing frameworks such as Spock, JUnit & TestNG.

The Book of Geb contains all the information you need to get started with Geb.

What does it look like?

Here's what a simple Geb script to log into an admin section of a website might look like...

This is what is known as the scripting style of Geb and it's great for quick automation.

See the guide
import geb.Browser

Browser.drive {
    go "http://myapp.com/login"
    
    assert $("h1").text() == "Please Login"
    
    $("form.login").with {
        username = "admin"
        password = "password"
        login().click()
    }
    
    assert $("h1").text() == "Admin Section"
}

Cross Browser Automation

Geb leverages the WebDriver library for browser automation. This means that Geb works with any browser that WebDriver works with, and the list of browsers that WebDriver works with is growing all the time.

Currently supported browsers are:

Supported
View Docs
Supported
View Docs
Supported
View Docs
Supported
View Docs

Remote Browsers

WebDriver also supports remote drivers. This allows you to automate a browser running on another machine! This means you can easily run your test suite against an IE browser from the comfort of your Mac or Linux machine (and vice versa).

Headless Browsers

You can also use the headless, in process, Java browser emulator HTMLUnit with WebDriver.

Navigating Content

Geb takes inspiration from jQuery to provide a concise and effective way to get at content. This is called the Navigator API.

The dollar function can be used anywhere to select content based on CSS selectors, attribute matchers and/or indexes.

// CSS 3 selectors
$("div.some-class p:first[title='something']")

// Find via index and/or attribute matching
$("h1", 2, class: "heading")
$("p", name: "description")
$("ul.things li", 2)

// 'text' is special attribute for the element text content
$("h1", text: "All about Geb")

// Use builtin matchers and regular expressions
$("p", text: contains("Geb"))
$("input", value: ~/\d{3,}-\d{3,}-\d{3,}/)

// Chaining
$("div").find(".b")
$("div").filter(".c").parents()
$("p.c").siblings()
            

Geb has handy shortcuts for reading and writing form control values.

assert $("form").name == "Jeb"
$("form").name = "Geb"
assert $("form").name == "Geb"
            

Asynchronicity

Modern web pages are full of asynchronous operations like AJAX requests and animations. Geb provides built in support for this fact of life.

Any content lookup, or operation can be wrapped in a waitFor clause.

waitFor { 
    $("p.status").text() == "Async operation complete!" 
}
                                    

This will keep testing the condition for a certain amount of time (which is configurable) until it passes.

The same technique can be used to just wait for the content, not necessarily for the content to have some characteristic.

def dynamicParagraph = waitFor { $("p.dynamically-added") }
dynamicParagraph.text() == "Added dynamically!" 
                                    

You can also define that content should be implicitly waited for in the Content DSL for page objects

With this definition, when dynamicParagraph is requested Geb will implictly wait for a certain amount of time for it to appear.

import geb.Page
                            
class DynamicPage extends Page {
    static content = {
        dynamicParagraph(wait: true) { 
            $("p.dynamically-added") 
        }
    }
}
                                    

Testing

Geb provides integration modules for popular testing frameworks such as Spock, JUnit, TestNG and Cucumber (via Cucmber JVM)

While Geb works great with all of these frameworks, it really shines with Spock. Spock is an innovative testing framework that is a great match for using with Geb. Using Spock + Geb gives you very clear, concise and easy to understand test specifications with very little effort.

Spock
import geb.Page
import geb.spock.GebSpec

class LoginSpec extends GebSpec {
    def "login to admin section"() {
        given:
        to LoginPage
        
        when:
        loginForm.with {
            username = "admin"
            password = "password"
        }
        
        and:
        loginButton.click()
        
        then:
        at AdminPage
    }
}
                            

Build System Integration

Geb is easy to integrate into any build system, and information and examples on integrating with the following build/project systems is available