Chapter Contents

11 Scripts and Binding

Geb supports being used in scripting environments via both the 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 can be used with Cucumber-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

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

// remove Geb bits from the binding and stop updating it

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" }

In a managed binding, all of the methods/properties that you can usually call in the 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) { $("") }

to SomePage
assert page instanceof SomePage
assert page instanceof OtherPage

The Book of Geb - 0.10.0 - October, 2014
Licensed under the Apache License, Version 2.0