0

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: 
          ....
     }
 }
Guy
  • 666
  • 1
  • 10
  • 34

1 Answers1

2

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
Rushby
  • 869
  • 9
  • 18
  • I tried your suggestion. Same error occurred. I'm using an old version of IDEA. I'll upgrade and try again. One question: if I do get this to work, does this mean I have to repeat this for every spec file? I'd prefer to avoid the duplication. There could be 20 spec files eventually. (The shell script option would cover that, but only if I use mvn.) – Guy Oct 11 '18 at 14:14
  • You can create a "Default" in the Run/Debug Configuration, this will mean that when you run a test within IntelliJ by right click > run it will use the default configuration. That would allow you to have the above settings in one place only. – Rushby Oct 11 '18 at 14:43
  • Updated my answer with a fix that should work if you wish to continue with setting the path in the config. – Rushby Oct 11 '18 at 14:54
  • 1
    This is to clarify that moving the `System.setProperty` line outside of the `environments` block worked for me. Much obliged. – Guy Oct 11 '18 at 15:54