Fork me on Github

Run Grails Geb Functional Tests with multiple Browsers

Run tests with Firefox, HtmlUnit, Chrome and PhantomJs

Authors: Sergio del Amo

Grails Version: 3.3.0

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, Chrome and PhantomJs)

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.7 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 additional some 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/groovy/GebConfig.groovy
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.htmlunit.HtmlUnitDriver
import org.openqa.selenium.phantomjs.PhantomJSDriver

environments {

    htmlUnit {
        driver = { new HtmlUnitDriver() }
    }

    chrome {
        driver = { new ChromeDriver() }
    }

    firefox {
        driver = { new FirefoxDriver() }
    }

    phantomJs {
        driver = { new PhantomJSDriver() }
    }
}

We need to add the different browser dependencies

/build.gradle
testCompile "org.grails.plugins:geb"
testRuntime "net.sourceforge.htmlunit:htmlunit:2.18"
testRuntime "org.seleniumhq.selenium:selenium-support:2.53.1"
testCompile "org.seleniumhq.selenium:selenium-htmlunit-driver:2.47.1"
testCompile "org.seleniumhq.selenium:selenium-firefox-driver:2.53.1"
testCompile "org.seleniumhq.selenium:selenium-chrome-driver:2.53.1"
testCompile "com.codeborne:phantomjsdriver:1.3.0"

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('MethodName')
@Integration
class DefaultHomePageSpec extends GebSpec {

    def '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

4.4 Run Tests with PhantomJs

To run the tests in PhantomJs, we need to download the appropriate drivers for our operating system and provide them as a System property.

./gradlew -Dgeb.env=phantomJs -Dphantomjs.binary.path=/Users/sdelamo/Applications/phantomjs-2.1.1-macosx/bin/phantomjs integrationTest

5 Do you need help with Grails?

OCI sponsored the creation of this Guide. OCI offers several Grails services:

Free consultation

The OCI Grails Team includes Grails co-founders, Jeff Scott Brown and Graeme Rocher. Check our Grails courses and learn from the engineers who developed, matured and maintain Grails.

Grails OCI Team