Ray Nicholus
Ray Nicholus

Reputation: 19890

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

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.

Upvotes: 6

Views: 16773

Answers (5)

arulraj.net
arulraj.net

Reputation: 4817

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 

Upvotes: 1

Madasu
Madasu

Reputation: 11

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.

Upvotes: 1

OlgaMaciaszek
OlgaMaciaszek

Reputation: 3912

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

Upvotes: 7

techarch
techarch

Reputation: 1141

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

Upvotes: 0

Nikita Skvortsov
Nikita Skvortsov

Reputation: 4923

Try using system properties:

test {
   systemProperties['geb.driver'] = 'firefox'
   include "geb/**/*.class"
   testReportDir = new File(testReportDir, "gebtests")
}

Upvotes: 17

Related Questions