0

I'm new to gradle and learning as I go. I've tests that I can kick off using gradle command in my terminal. The issue I'm running into is that I have to keep two copies of the same test, one for QA and one for Staging environment. They are similar tests the reason for this is that I have no idea how to set up gradle in a way that I can just tell it to run testA on QA or Staging My Current gradle.build file looks like this

dependencies {
    testCompile 'org.testng:testng:6.14.3'
    compile 'org.seleniumhq.selenium:selenium-java:3.141.59'
    compile 'io.github.bonigarcia:webdrivermanager:3.3.0'
    compile 'org.slf4j:slf4j-nop:1.7.25'
}
test {
    useTestNG()
}

My current gradle command looks like this for QA

gradle test --tests testlandingpageQA 

or for Staging

gradle test --tests testlandingpageDev  

I need a solution where end user can pick which environment to run this test on.

user3329818
  • 31
  • 1
  • 6
  • you may have to get your url from an environment variable or a property file (and have several property files: test-it.qa.properties, test-it.staging.properties). But at the end, you have to run gradle with a custom command line argument. see https://stackoverflow.com/questions/11696521/how-to-pass-arguments-from-command-line-to-gradle – Max Aug 19 '19 at 17:04
  • I understand what you are saying but have zero idea how to implement it. I looked at the link and tbh I have no idea whats goiing on Im lost. – user3329818 Aug 19 '19 at 18:28
  • For a simple case, you should have gradle set without the url as an command line argument. Then, in your test class, in a method annotated with @Before, you get this url from an environment variable or a Java property (System.getEnv("xxx") or System.getProperty("xxx")). At the end, set manually this value then run gradle without the url as a command line argument. Bonus: set this env/property variable as a command line argument for gradle (see previous link). – Max Aug 20 '19 at 08:02
  • Thanks that makes sense I will try that. – user3329818 Aug 20 '19 at 18:44

1 Answers1

0

I got this working in build.gradle file I had to define system property

test {
systemProperty 'env', System.properties['env'] ?: 'staging'

useTestNG()
}

In java I had to do

    String environment = System.getProperty("env");
        driver.get("https://" + environment + ".somewebsite.com");

Finally in the console/terminal I can run something like

gradle -Denv=Dev test --tests testlandingpage

Hopefully it helps others who are trying to figure this out

user3329818
  • 31
  • 1
  • 6