Categories
Grails Moving to Grails

Geb testing on linux

I started with downloading https://guides.grails.org/grails-geb-multiple-browsers/guide/index.html and followed the guide.

Running the test with HtmlUnit worked at once.

The Chrome test worked once the drivers had been installed.

Firefox test gave a few error messages. After some searching, and re-reading the message I noticed this “The path to the driver executable must be set by the webdriver.gecko.driver system property” in the exception.

Configuring for firefox testing

First I run locate geckodriver from the terminal:

$ locate geckodriver

/home/johnny/.gradle/webdriver/geckodriver
/home/johnny/.gradle/webdriver/geckodriver/0.24.0/geckodriver-v0.24.0-linux64.tar
/home/johnny/.gradle/webdriver/geckodriver/0.24.0/geckodriver-v0.24.0-linux64.tar/1gar6w1umbo767dkaafmitn8s
/home/johnny/.gradle/webdriver/geckodriver/0.24.0/geckodriver-v0.24.0-linux64.tar/1gar6w1umbo767dkaafmitn8s/geckodriver
/home/johnny/.gradle/webdriver/geckodriver/0.24.0/geckodriver-v0.24.0-linux64.tar/1gar6w1umbo767dkaafmitn8s/geckodriver-v0.24.0-linux64.tar.gz
/home/johnny/.gradle/webdriver/geckodriver/0.24.0/geckodriver-v0.24.0-linux64.tar/1gar6w1umbo767dkaafmitn8s/geckodriver-v0.24.0-linux64.tar.gz.lck
/home/johnny/.gradle/webdriver/geckodriver/0.24.0/geckodriver-v0.24.0-linux64.tar/1gar6w1umbo767dkaafmitn8s/geckodriver-v0.24.0-linux64.tar.gz.ok

Copy the path to the most relevant driver and change GebConfig.groovy:

 firefox {
        driver = {
            System.setProperty('webdriver.gecko.driver', "${System.getProperty('user.home')}/.gradle/webdriver/geckodriver/0.24.0/geckodriver-v0.24.0-linux64.tar/1gar6w1umbo767dkaafmitn8s/geckodriver")
            def driverInstance = new FirefoxDriver()
            driverInstance.manage().window().maximize()     // optional
            driverInstance
        }
    }

Configuring for Chrome testing

First you need to check Chrome version and download the according chromedriver from https://sites.google.com/a/chromium.org/chromedriver/downloads

You can also find the chromedriver here: http://chromedriver.chromium.org/downloads

You can e.g. copy the appropriate link and:

$ cd /tmp
/tmp$ wget https://chromedriver.storage.googleapis.com/85.0.4183.87/chromedriver_linux64.zip

/tmp$ unzip chromedriver_linux64.zip
/tmp$ sudo mv chromedriver /usr/bin/chromedriver
/tmp$ sudo chown root:root /usr/bin/chromedriver 
/tmp$ sudo chmod +x /usr/bin/chromedriver 

I also changed GebConfig.groovy:

chrome {
        driver = {
            System.setProperty('webdriver.chrome.driver', '/usr/bin/chromedriver')
            def driver = new ChromeDriver()
            driver.manage().window().maximize()     // optional
            driver
        }
    }

Complete 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 = {
            System.setProperty('webdriver.chrome.driver', '/usr/bin/chromedriver')
            def driver = new ChromeDriver()
            driver.manage().window().maximize()     // optional
            driver
        }
    }

    firefox {
        driver = {
            System.setProperty('webdriver.gecko.driver', "${System.getProperty('user.home')}/.gradle/webdriver/geckodriver/0.24.0/geckodriver-v0.24.0-linux64.tar/1gar6w1umbo767dkaafmitn8s/geckodriver")
            def driverInstance = new FirefoxDriver()
            driverInstance.manage().window().maximize()     // optional
            driverInstance
        }
    }
}

Run/Debug Configurations in IntelliJ Idea

If left alone the integrationTest will run using HtmlUnit driver when started from inside IntelliJ Idea.

You can add configurations for Chrome and Firefox easily by:

  1. Run integrationTest

2. Edit Configurations:

Copy the configuration two times. Edit Chrome:

And Firefox:

Final touches to gradle configuration

Use gradle.properties to set seleniumVersion:

seleniumVersion=3.141.59

Use this in build.gradle:

dependencies {
    testCompile "org.grails:grails-gorm-testing-support"
    testCompile "org.mockito:mockito-core"
    testCompile "org.grails:grails-web-testing-support"
    testCompile "org.grails.plugins:geb"
    testCompile "org.seleniumhq.selenium:selenium-remote-driver:$seleniumVersion"
    testCompile "org.seleniumhq.selenium:selenium-api:$seleniumVersion"
    testCompile "org.seleniumhq.selenium:selenium-support:$seleniumVersion"
    testCompile "org.seleniumhq.selenium:htmlunit-driver:2.43.1"

    testRuntime 'net.sourceforge.htmlunit:htmlunit:2.43.0'
    testRuntime "org.seleniumhq.selenium:selenium-htmlunit-driver:2.52.0"
    testRuntime "org.seleniumhq.selenium:selenium-chrome-driver:$seleniumVersion"
    testRuntime "org.seleniumhq.selenium:selenium-firefox-driver:$seleniumVersion"
}