Search code examples
intellij-ideagroovyselenium-chromedrivergeb

Geb test ignoring GebConfig.groovy file launched in IntelliJ


Running in IntelliJ IDEA. GebConfig.groovy is in /src/test/resources.

I am using the Chrome driver.

When I type System.setProperty("webdriver.chrome.driver", "my/path") inside my spec file, and I right click and select run, the test works, meaning it opens Chrome and loads the page.

When I don't do that in the spec file, but just leave it in the GebConfig.groovy file, I get the error message "the page to the driver executable must be set".

There's an air-gap, so I can't copy-paste; I'll type as much as I can here: GebConfig.groovy:

import org.openqa.selenium.chrome.ChromeDriver

...

environments {
    chrome {
        System.setProperty("webdriver.chrome.driver", "my/path")
        driver = {new ChromeDriver()}
    }
}

The spec file is really simple, like the example on GitHub

import LoginPage
import geb.spock.GebReportSpec

class LoginSpec extends GebReportSpec
{

    // Works when I put this here, but I should not have to do this!
    System.setProperty("webdriver.chrome.driver", "my/path")

     def "user can log in" () {
        when: "log in as me"
            def loginPage = to LoginPage
            loginPage.login("me")
        then: 
          ....
     }
 }

Solution

  • To fix your problem if you want to keep the path in the geb config, setting the path outside of the environment section like so should work:

    import org.openqa.selenium.chrome.ChromeDriver
    
    System.setProperty("webdriver.chrome.driver", "my/path")
    
    //You can also set the driver up here as a default and running with an environment set will override it
    driver = {new ChromeDriver()}
    
    
    environments {
        chrome {
            driver = {new ChromeDriver()}
        }
    }
    

    Personally I would avoid adding the driver path to the geb config and create a run configuration in intelliJ for running locally.

    Right click the spec file > Click "Create 'nameOfMySpec'".

    Now add your driver path to the VM parameters:

    -Dgeb.env=chrome -Dwebdriver.chrome.driver=my/path
    

    It's also worth considering a shell script that could then also be called via Jenkins etc:

    mvn test -Dgeb.env=chrome -Dwebdriver.chrome.driver=my/path