Chapter Contents


15 Build System & Framework Integrations

This kind of integration for Geb is typically focussed on managing the base url and reports dir as build systems tend to be able to provide this configuration (via the build adapter mechanism).

15.1 Grails

Grails is a popular web app framework. There is a grails-geb plugin available that allows you to use Geb for your Grails functional tests. This plugin simply manages the baseUrl and reportsDir configuration items.

You still need to include the appropriate Geb module for testing (i.e. geb-junit4, geb-spock or geb-easyb) yourself. You may also need to depend on other plugins like the grails-spock plugin to enable those kinds of tests.

For example, if you plan to use Spock with Geb you would need to add the following to the BuildConfig.groovy

dependencies {
    test "org.gebish:geb-spock:0.9.2"
}
plugins {
    test ":spock:0.7"
    test ":geb:0.9.2"
}

Where 0.9.2 and 0.7 are the versions of Geb and Spock you wish to use.

As Grails provides JUnit support out of the box, you only need to pull in the geb-junit4 jar to use Geb with JUnit.

Grails 1.3 and later use JUnit 4. Earlier versions of Grails than this use Groovy 1.6 which Geb no longer supports.

dependencies {
    test "org.gebish:geb-junit4:0.9.2"
}
plugins {
    test ":geb:0.9.2"
}

You only need the appropriate Geb test integration jar, as it will depend on geb-core and Grails’ dependency management will take care of getting that for you.

You will also of course need a driver and the selenium-support dependency, which you can also specify in BuildConfig.groovy.

dependencies {
    test "org.seleniumhq.selenium:selenium-support:«webdriver version»"
    test "org.seleniumhq.selenium:selenium-firefox-driver:«webdriver version»"
}

HTMLUnit depends on some XML processing libraries that cause issues with Grails. You can avoid this by excluding certain dependencies of the HTMLUnit driver…

test("org.seleniumhq.selenium:selenium-htmlunit-driver:«webdriver version»") {
    exclude 'xml-apis'
}

Recall that Geb looks for its configuration file as GebConfig.groovy on the classpath. A good location for this file is in a Grails project is the test/functional directory is on the classpath at test time. You do not need to set the baseUrl and reportsDir config entries in a Grails project as the plugin takes care of those for you. The baseUrl is set to the root of the Grails application, and the reportsDir is set to geb inside Grails’ test reports dir (which by default is target/test-reports).

There is nothing special about writing Geb tests with Grails. You subclass the same classes as usual (e.g. geb.spock.GebReportingSpec for Spock tests).

There is an example project available that uses geb-junit4 and geb-spock to test some Grails scaffold pages.

15.2 Gradle

Using Geb with Gradle simply involves pulling in the appropriate dependencies, and configuring the base URL and reports dir in the build script if they are necessary.

Below is a valid Gradle build.gradle file for working with Geb for testing.

apply plugin: "groovy"

repositories {
    mavenCentral()
}

configurations {
    testCompile.transitive = true
}

dependencies {
    groovy "org.codehaus.groovy:groovy-all:1.8.9"

    def gebVersion = "0.9.2"
    def seleniumVersion = "2.26.0"

    // If using Spock, need to depend on geb-spock
    testCompile "org.gebish:geb-spock:0.9.2"
    testCompile "org.spockframework:spock-core:0.7-groovy-1.8"

    // If using JUnit, need to depend on geb-junit (3 or 4)
    testCompile "org.gebish:geb-junit4:0.9.2"
    testCompile "junit:junit-dep:4.8.2"

    // Need a driver implementation
    testCompile "org.seleniumhq.selenium:selenium-firefox-driver:2.26.0"
    testRuntime "org.seleniumhq.selenium:selenium-support:2.26.0"
}

test {
    systemProperties "geb.build.reportsDir": "$reportsDir/geb"
}

There is a Gradle example project available.

15.3 Maven

Using Geb with Maven simply involves pulling in the appropriate dependencies, and configuring the base URL and reports dir in the build script if they are necessary.

Below is a valid pom.xml file for working with Geb for testing (with Spock).

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.gebish.example</groupId>
  <artifactId>geb-maven-example</artifactId>
  <packaging>jar</packaging>
  <version>1</version>
  <name>Geb Maven Example</name>
  <url>http://www.gebish.org</url>
  <dependencies>
    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
      <version>1.7.10</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.spockframework</groupId>
      <artifactId>spock-core</artifactId>
      <version>0.7-groovy-1.8</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.gebish</groupId>
      <artifactId>geb-spock</artifactId>
      <version>0.9.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-firefox-driver</artifactId>
      <version>2.26.0</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-support</artifactId>
      <version>2.26.0</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.9</version>
        <configuration>
          <includes>
            <include>*Spec.*</include>
          </includes>
          <systemPropertyVariables>
            <geb.build.baseUrl>http://google.com/ncr</geb.build.baseUrl>
            <geb.build.reportsDir>target/test-reports/geb</geb.build.reportsDir>
          </systemPropertyVariables>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.codehaus.gmaven</groupId>
        <artifactId>gmaven-plugin</artifactId>
        <version>1.3</version>
        <configuration>
          <providerSelection>1.7</providerSelection>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

There is a Maven example project available.


The Book of Geb - 0.9.2 - October, 2013
Licensed under the Apache License, Version 2.0