Geb Programmer : How to execute Geb with different browsers

$ gradle --version

------------------------------------------------------------
Gradle 3.1
------------------------------------------------------------
$ mkdir gwebbot_greach
$ cd page gwebbot_greach
$ touch <i>build.gradle</i>

Add Groovy dependency to build.gradle

apply plugin: 'groovy'

Add Gradle wrapper

$ gradle wrapper --gradle-version 3.1

add Geb, Selenium, Drivers and Spock depependencies to build.gradle

apply plugin: 'groovy'

repositories {
    jcenter()
}
ext {
    seleniumVersion = '2.52.0'
    phantomJsDriverVersion = '1.2.1'
}
dependencies {
    compile "org.gebish:geb-core:1.0"
    compile "org.seleniumhq.selenium:selenium-support:${seleniumVersion}"
    compile "org.seleniumhq.selenium:selenium-firefox-driver:${seleniumVersion}"
    compile "org.seleniumhq.selenium:selenium-chrome-driver:${seleniumVersion}"
    compile("com.codeborne:phantomjsdriver:$phantomJsDriverVersion")
    compile 'org.spockframework:spock-core:1.0-groovy-2.4'
}

Create a test src/test/groovy/gebwebbot/greach/LegalSpec.groovy

package gebwebbot.greach.legal

import geb.Browser
import spock.lang.Specification

class LegalSpec extends Specification {

    def "we are able to fetch the legal information about greach"() {

        when:
        def browser = new Browser()
        browser.go 'http://2017.greachconf.com'

        then:
        browser.page.find('footer div.credits').text() == 'The Greach Network SL, 2011-2017 - CIF B86412491 - C/Valtravieso, 28023 Madrid (Spain)'
    }
}

We are going to create a Geb.Config file to leverage Geb environment sensitivity.

vi src/test/resources/GebConfig.groovy

import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.phantomjs.PhantomJSDriver

environments {
    firefox {
        driver = { new FirefoxDriver() }
    }
    phantomJs {
        driver = { new PhantomJSDriver() }
    }
    chrome {
        driver = { new ChromeDriver() }
    }
}

Pass Java system properties from the command-line to the gradle test task. In order to do that modify our build.gradle file as follows:

apply plugin: 'groovy'

repositories {
    jcenter()
}
ext {
    seleniumVersion = '2.52.0'
    phantomJsDriverVersion = '1.2.1'
}
dependencies {
    compile "org.gebish:geb-core:1.0"
    compile "org.seleniumhq.selenium:selenium-support:${seleniumVersion}"
    compile "org.seleniumhq.selenium:selenium-firefox-driver:${seleniumVersion}"
    compile "org.seleniumhq.selenium:selenium-chrome-driver:${seleniumVersion}"
    compile("com.codeborne:phantomjsdriver:$phantomJsDriverVersion")
    compile 'org.spockframework:spock-core:1.0-groovy-2.4'
}

test {
    systemProperties System.properties
}

Execute tests with firefox

./gradlew -Dgeb.env=firefox test

Execute tests with PhantomJS

You will need to download the drivers for your operating system in the PhantomJs Website.

./gradlew -Dgeb.env=phantomJs -Dphantomjs.binary.path=/Users/groovycalamari/Documents/phantomjs-2.1.1-macosx/bin/phantomjs test

Execute tests with Chrome

You will need to download the WebDriver for Chrome.

./gradlew -Dgeb.env=chrome -Dwebdriver.chrome.driver=/Users/groovycalamari/Documents/chromedriver test

Tags: #geb