This plugin integrates Geb with Grails to make it easy to write functional tests for your Grails applications.
If you are looking for examples on how to write Geb tests, check:
Geb/Grails example project or Grails functional test suite where Geb tests are used extensively. For further reference please see the Geb documentation.
To use the plugin, add the following dependencies to your build.gradle
file:
dependencies { // This is only needed to if you want to use the // create-functional-test command (see below) implementation 'org.apache.grails:grails-geb' // This is needed to compile and run the tests integrationTestImplementation testFixtures('org.apache.grails:grails-geb') }
To get started, you can use the create-functional-test
command to generate a new functional test using Geb:
./grailsw create-functional-test com.example.MyFunctionalSpec
This will create a new Geb test named MyFunctionalSpec
in the src/integration-test/groovy/com/example
directory.
There are two ways to use this plugin. Either extend your test classes with the ContainerGebSpec
class or with the GebSpec
class.
By extending your test classes with ContainerGebSpec
, your tests will automatically use a containerized browser using Testcontainers. This requires a compatible container runtime to be installed, such as:
If you choose to use the ContainerGebSpec
class, as long as you have a compatible container runtime installed, you don't need to do anything else. Just run ./gradlew integrationTest
and a container will be started and configured to start a browser that can access your application under test.
Parallel execution of ContainerGebSpec
specifications is not currently supported.
The annotation ContainerGebConfiguration
exists to customize the connection the container will use to access the application under test. The annotation is not required and ContainerGebSpec
will use the default values in this annotation if it's not present. A traditional GebConfig.groovy
can be provided to configure non-container specific settings.
To configure reporting, enable it using the recording
property on the annotation ContainerGebConfiguration
. The following system properties exist for reporting configuration:
grails.geb.reporting.directory
build/gebContainer/reports
By default, no test recording will be performed. Various system properties exist to change the recording behavior. To set them, you can set them in your build.gradle
file like so:
tasks.withType(Test) { useJUnitPlatform() systemProperty 'grails.geb.recording.mode', 'RECORD_ALL' }
grails.geb.recording.mode
SKIP
, RECORD_ALL
, or RECORD_FAILING
SKIP
grails.geb.recording.directory
build/gebContainer/recordings
grails.geb.recording.format
FLV
or MP4
MP4
Uploading a file is more complicated for Remote WebDriver sessions because the file you want to upload is likely on the host executing the tests and not in the container running the browser. For this reason, this plugin will setup a Local File Detector by default.
To customize the default, either:
ContainerFileDetector
and specify its fully qualified class name in a META-INF/services/grails.plugin.geb.ContainerFileDetector
file on the classpath (e.g., src/integration-test/resources
).ContainerGebConfiguration
annotation and set its fileDetector
property to your ContainerFileDetector
implementation class.Alternatively, you can access the BrowserWebDriverContainer
instance via the container
from within your ContainerGebSpec
to, for example, call .copyFileToContainer()
. An Example of this can be seen in ContainerSupport#createFileInputSource utility method.
grails.geb.timeouts.implicitlyWait
0
seconds, which means that if an element is not found, it will immediately return an error.grails.geb.timeouts.pageLoad
300
secondsgrails.geb.timeouts.script
30
secondsSelenium integrates with OpenTelemetry to support observability and tracing out of the box. By default, Selenium enables tracing.
This plugin, however, disables tracing by default since most setups lack an OpenTelemetry collector to process the traces.
To enable tracing, set the following system property:
grails.geb.tracing.enabled
true
or false
false
This allows you to opt in to tracing when an OpenTelemetry collector is available.
If you choose to extend GebSpec
, you will need to have a Selenium WebDriver installed that matches a browser you have installed on your system. This plugin comes with the selenium-chrome-driver
java bindings pre-installed, but you can also add additional browser bindings.
To set up additional bindings, you need to add them to your build.gradle
for example:
dependencies { integrationTestImplementation 'org.seleniumhq.selenium:selenium-firefox-driver' integrationTestImplementation 'org.seleniumhq.selenium:selenium-edge-driver' }
You also need to add a GebConfig.groovy
file in the src/integration-test/resources/
directory. For example:
/* This is the Geb configuration file. See: http://www.gebish.org/manual/current/#configuration */ /* ... */ import org.openqa.selenium.edge.EdgeDriver import org.openqa.selenium.firefox.FirefoxDriver environments { /* ... */ edge { driver = { new EdgeDriver() } } firefox { driver = { new FirefoxDriver() } } }
And pass on the geb.env
system property if running your tests via Gradle:
// build.gradle tasks.withType(Test) { useJUnitPlatform() systemProperty 'geb.env', System.getProperty('geb.env') }
Now you can run your tests with the browsers installed on your system by specifying the Geb environment you have set up in your GebConfig.groovy
file. For example:
./gradlew integrationTest -Dgeb.env=edge