How can I pass JVM system properties on to my tests?
Asked Answered
H

5

6

I have the following task

task testGeb(type:Test) {
   jvmArgs '-Dgeb.driver=firefox'
   include "geb/**/*.class"
   testReportDir = new File(testReportDir, "gebtests")
}

The system property doesn't appear to make it to the Geb tests, as Geb does not spawn Firefox to run the tests. When I set the same system property in Eclipse and run the tests, everything works fine.

Hufnagel answered 12/4, 2011 at 20:43 Comment(0)
P
17

Try using system properties:

test {
   systemProperties['geb.driver'] = 'firefox'
   include "geb/**/*.class"
   testReportDir = new File(testReportDir, "gebtests")
}
Pam answered 13/4, 2011 at 11:57 Comment(2)
This might actually be an issue with Geb. I confirmed that the prop is being passed properly.Hufnagel
There was a bug in early snapshot versions of 0.6 that would have prevented this from working, but it has been resolved for the 0.6 final.Telescopic
H
7

You can also directly set the system property in the task:

task testGeb(type:Test) {
    System.setProperty('geb.driver', 'firefox')}

(the solution above will also work for task type different from Test)

or if you would like to be able to pass different properties from the command line, you can include a more flexible solution in the task definition:

task testGeb(type:Test) {
    jvmArgs project.gradle.startParameter.systemPropertiesArgs.entrySet().collect{"-D${it.key}=${it.value}"}
}

and then you can run: ./gradlew testGeb -D[anyArg]=[anyValue], in your case: ./gradlew testGeb -Dgeb.driver=firefox

Heteroclite answered 17/11, 2015 at 14:52 Comment(0)
I
1
Below code works fine for me using Gradle and my cucumber scenarios are passing perfectly. Add below code in your build.gradle file:

//noinspection GroovyAssignabilityCheck

test{

    systemProperties['webdriver.chrome.driver'] = '/usr/bin/google_chrome/chromedriver'

}

Note: I used Ubuntu OS and the chrome_driver path I specified in /usr/bin/google_chrome/ and your path varies according to your path.

Including answered 15/6, 2016 at 9:26 Comment(0)
H
1

Add systemProperties System.getProperties() in your task

test {
  ignoreFailures = false
  include "geb/**/*.class"
  testReportDir = new File(testReportDir, "gebtests")
  // set a system property for the test JVM(s)
  systemProperties System.getProperties()
}

So that it will be configurable while running the test. For example

gradle -Dgeb.driver=firefox test
gradle -Dgeb.driver=chrome test 
Halland answered 16/3, 2017 at 11:58 Comment(0)
M
0

I would recommend doing the following

gradle myTask -DmyParameter=123

with the following code

task myTask {
    doLast {
        println System.properties['myParameter']
    }
 }

The output should be

gradle myTask -DmyParameter=123 :myTask 123

BUILD SUCCESSFUL

Total time: 2.115 secs

Microsporangium answered 31/7, 2012 at 9:18 Comment(1)
This only sets the property for the Gradle JVM, not the JVM that runs the tests.Panthea

© 2022 - 2024 — McMap. All rights reserved.