ifndef::snippets-dir[]
:snippets-dir: ../../../../manual-snippets/src/test/groovy
:snippets-res-dir: ../../../../manual-snippets/src/test/resources
:rb-snippets-dir: ../../../../manual-snippets/real-browser/src/test/groovy
:cm-snippets-dir: ../../../../../module/geb-core/src/main/groovy
:ct-snippets-dir: ../../../../../module/geb-core/src/test/groovy
endif::[]
[[binding]]
= Scripts and binding

Geb supports being used in scripting environments via both the `Browser.drive()` method, and by using the {binding-updater-link}[`geb.binding.BindingUpdater`] class that populates and updates a
{groovy-binding-link}[`groovy.lang.Binding`] that can be used with scripts.
This is also the same mechanism that can be used with <<cucumber-jvm, Cucumber-JVM>>.

== Setup

To use the binding support, you simply create a {binding-updater-link}[`BindingUpdater`] object with a {groovy-binding-link}[`Binding`] and `{browser-api}`...

[source,groovy]
----
include::{snippets-dir}/binding/BindingUpdatingSpec.groovy[tag=imports,indent=0]

include::{snippets-dir}/binding/BindingUpdatingSpec.groovy[tag=example,indent=0]
----
<1> Populate and start updating the browser.
<2> Run a script from a resource loaded from the classpath.
<3> Remove Geb bits from the binding and stop updating it.

== The binding environment

=== Browser methods and properties

The {binding-updater-link}[`BindingUpdater`] installs shortcuts into the binding for most of the `{browser-api}` public methods.

The following is an example script that will work if `BindingUpdater` is initialized on its binding...

[source,groovy]
----
include::{snippets-res-dir}/gebScript.groovy[tag=script,indent=0]
----

In a managed binding, all of the methods/properties that you can usually call in the {drive-method-link}[`Browser.drive()`] method are available.
This includes the `$()` method.

The following methods are available:

* `$()`
* `go()`
* `to()`
* `via()`
* `at()`
* `waitFor()`
* `withAlert()`
* `withNoAlert()`
* `withConfirm()`
* `withNoConfirm()`
* `download()`
* `downloadStream()`
* `downloadText()`
* `downloadBytes()`
* `downloadContent()`
* `report()`
* `reportGroup()`
* `cleanReportGroupDir()`

The JavaScript interface property <<js-object, `js`>> is also available. The `Browser` object itself is available as the `browser` property.

=== The current page

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

[source,groovy]
----
include::{snippets-res-dir}/gebScriptUsingPages.groovy[tag=script,indent=0]
----
