Geb setup with automatic driver downloading


Got a new computer. Setting up geb can actually be easier than earlier described.

build.gradle

The webdriver-binaries-gradle-plugin will actually download the wanted drivers for you. First add dependency, add plugin and define dependencies:

buildscript {
    dependencies {
        classpath "gradle.plugin.com.github.erdi.webdriver-binaries:webdriver-binaries-gradle-plugin:2.7"
    }
}

apply plugin: 'com.github.erdi.webdriver-binaries'

dependencies {
    testImplementation "org.grails.plugins:geb"
    testImplementation "org.seleniumhq.selenium:selenium-remote-driver:4.2.1"
    testImplementation "org.seleniumhq.selenium:selenium-api:4.2.1"
    testImplementation "org.seleniumhq.selenium:selenium-support:4.2.1"

    testRuntimeOnly "net.sourceforge.htmlunit:htmlunit:2.62.0"
    testRuntimeOnly "org.seleniumhq.selenium:htmlunit-driver:3.62.0"
    testRuntimeOnly "org.seleniumhq.selenium:selenium-chrome-driver:4.2.1"
    testRuntimeOnly "org.seleniumhq.selenium:selenium-firefox-driver:4.2.1"
}

Configuration of the plugin tells which driver-versions to download.

webdriverBinaries {
    chromedriver '102.0.5005.61'
    geckodriver '0.31.0'
}

Defining and loading drivers i GebConfig.groovy

Defining the test-environments you want to test:

environments {

    firefox {
        driver = {
            def driver = new FirefoxDriver()
            driver.manage().window().maximize()     // optional
            driver
        }
    }

    htmlUnit {
        driver = {
            def driver =  new HtmlUnitDriver()
            driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
            driver.setJavascriptEnabled(true)
            driver
        }
    }

    chrome {
        driver = {
            def driver = new ChromeDriver()
            driver.manage().window().maximize()     // optional
            driver
        }
    }

}

Defining a default driver:

driver = {  // default driver
    def driver = new FirefoxDriver()
    driver.manage().window().maximize()     // optional
    driver
}