Chapter Contents


11 Scripts and Binding

Geb supports being used in scripting environments via both the Browser.drive() method, and by using the geb.binding.BindingUpdater class that populates and updates a groovy.lang.Binding that can be used with scripts. This is also the same mechanism that is used by the EasyB Geb plugin and can be used with Cuke4Duke (Cucumber for the JVM).

11.1 Setup

To use the binding support, you simply create a BindingUpdater object with a Binding and Browser

import geb.Browser
import geb.binding.BindingUpdater
import groovy.lang.Binding

def binding = new Binding()
def browser = new Browser()
def updater = new BindingUpdater(binding, browser)

// populate and start updating the browser
updater.initialize()

// Run a script from the filesystem
new GroovyShell(binding).evaluate(new File("someScript.groovy"))

// remove Geb bits from the binding and stop updating it
updater.remove()

11.2 The binding environment

11.2.1 Browser methods & properties

The BindingUpdater installs shortcuts into the binding for most of the browser object's public methods.

For example…

go "some/page"
assert at(SomePage)
waitFor { $("p.status").text() == "ready" }
js.someJavaScriptFunction()
downloadText($("a.csvFile"))

In a managed binding, all of the methods/properties that you can usually call in the Browser.drive() method are available. This includes the $() function.

The following methods are available:

The javascript interface property js is also available. The browser object itself is available as the browser property.

11.2.2 The current page

The binding updater also updates the page property of the binding to be the browser's current page…

import geb.Page

class SomePage extends Page {
    static content = {
        button(to: OtherPage) { $("input.do-stuff") }
    }
}

to SomePage
assert page instanceof SomePage
page.button.click()
assert page instanceof OtherPage

The Book of Geb - 0.6.3 - March, 2012
Licensed under the Apache License, Version 2.0