Show Navigation

Run Grails Geb Functional Tests with multiple Browsers

Run tests with Firefox, HtmlUnit, Chrome

Authors: Sergio del Amo

Grails Version: 5.0.1

1 Grails Training

Grails Training - Developed and delivered by the folks who created and actively maintain the Grails framework!.

2 Getting Started

We are going to show how to run Geb tests with multiple browsers (Firefox, HtmlUnit and Chrome)

2.1 What you will need

To complete this guide, you will need the following:

  • Some time on your hands

  • A decent text editor or IDE

  • JDK 1.8 or greater installed with JAVA_HOME configured appropriately

2.2 How to complete the guide

To get started do the following:

or

The Grails guides repositories contain two folders:

  • initial Initial project. Often a simple Grails app with some additional code to give you a head-start.

  • complete A completed example. It is the result of working through the steps presented by the guide and applying those changes to the initial folder.

To complete the guide, go to the initial folder

  • cd into grails-guides/grails-geb-multiple-browsers/initial

and follow the instructions in the next sections.

You can go right to the completed example if you cd into grails-guides/grails-geb-multiple-browsers/complete

3 Writing the Application

3.1 Geb Config

We handle Geb Configuration with a GebConfig.groovy file

/src/integration-test/resources/GebConfig.groovy
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.htmlunit.HtmlUnitDriver

environments {

    htmlUnit {
        driver = { new HtmlUnitDriver() }
    }

    chrome {
        driver = { new ChromeDriver() }
    }

    firefox {
        driver = { new FirefoxDriver() }
    }
}

We need to add the different browser dependencies

/build.gradle
testImplementation "org.grails.plugins:geb"
testRuntime 'net.sourceforge.htmlunit:htmlunit:2.35.0'
testImplementation "org.seleniumhq.selenium:htmlunit-driver:2.35.1"
testImplementation "org.seleniumhq.selenium:selenium-remote-driver:3.141.59"
testImplementation "org.seleniumhq.selenium:selenium-api:3.141.59"
testImplementation "org.seleniumhq.selenium:selenium-support:3.141.59"
testRuntimeOnly "org.seleniumhq.selenium:selenium-chrome-driver:3.141.59"
testRuntimeOnly "org.seleniumhq.selenium:selenium-firefox-driver:3.141.59"

We are going to change the Geb environment passing a system property. Because of that, we pass system properties to integration tests:

/build.gradle
integrationTest {
    systemProperties System.properties
}

3.2 Geb Test

We do a simple Geb functional test. It verifies that there is <h1> header with the text Welcome to Grails when we visit the home page.

welcomeToGrails
/src/integration-test/groovy/grails/geb/multiple/browsers/DefaultHomePageSpec.groovy
package grails.geb.multiple.browsers

import geb.spock.GebSpec
import grails.testing.mixin.integration.Integration

@SuppressWarnings('JUnitPublicNonTestMethod')
@SuppressWarnings('MethodName')
@Integration
class DefaultHomePageSpec extends GebSpec {

    void 'verifies there is _<h1>_ header with the text _Welcome to Grails when we visit the home page.'() {
        when:
        go '/'

        then:

        $('h1').text() == 'Welcome to Grails'

    }
}

4 Running the Application

4.1 Run Tests with Firefox

./gradlew -Dgeb.env=firefox integrationTest

4.2 Run Tests with Chrome

To run the tests in Google Chrome, you need to download the appropriate drivers.

./gradlew -Dgeb.env=chrome -Dwebdriver.chrome.driver=/Users/sdelamo/Applications/chromedriver integrationTest

4.3 Run Tests with HtmlUnit

./gradlew -Dgeb.env=htmlUnit integrationTest

5 Do you need help with Grails?

Object Computing, Inc. (OCI) sponsored the creation of this Guide. A variety of consulting and support services are available.

OCI is Home to Grails

Meet the Team