Java Cucumber: Take @CucumberOptions from external source like a property file

Viewed 16932

Is it possible to take cucumber option values from a java .properties file?

In this SO post, it shows that it is being passed from CLI.

Here's my sample class:

@RunWith(Cucumber.class)
@CucumberOptions(
        features = {"resources/features/"},
        glue = {"classpath:com/"},
        tags = {"@foo, @bar"}
)
public class UITestRunner {

}

Instead of hardcoding the tags here, I'd like to take it from a property file. Any help is appreciated!

6 Answers

I solved this by extending the Cucumber runner. You can find examples here:

For cucumber-jvm 4.0.0: https://github.com/martinschneider/yasew/blob/master/src/main/java/io/github/martinschneider/yasew/junit/YasewRunner.java

For cucumber-jvm 2.4.0: https://github.com/martinschneider/yasew/blob/db8cd74281139c14603e9ae05548530a7aebbade/src/main/java/io/github/martinschneider/yasew/junit/YasewRunner.java

The key part, as discussed in some of the replies and comments, is to set the cucumber.options system property:

String cucumberOptions =
        "--tags @"
            + getProperty(PLATFORM_KEY, DEFAULT_PLATFORM)
            + " --glue io.github.martinschneider.yasew.steps --glue "
            + getProperty(STEPS_PACKAGE_KEY)
            + " --plugin pretty --plugin html:report --plugin json:"
            + getProperty(CUCUMBER_REPORT_DIRECTORY_KEY, 
DEFAULT_CUCUMBER_REPORT_DIRECTORY)
            + "/cucumber.json"
            + " "
            + getProperty(FEATURES_DIRECTORY_KEY);
    LOG.info("Setting cucumber options ({}) to {}", CUCUMBER_OPTIONS_KEY, cucumberOptions);
    System.setProperty(CUCUMBER_OPTIONS_KEY, cucumberOptions);

I'm using a setup with Spring and JUnit and I'm not sure if there's a better place to put this code.

Overwriting the runner is not very elegant but it works like a charm!

An example for an override feature source line in cucumber.properties file in project tree is:

cucumber.options=-g StepDefs src\\test\\resources\\Testfeature.feature

The Cucumber for Java Book is cool. I got it after reading this post. I experimented some time to see what path the CucumberOptions property accepts... so here above is the quick solve. ;)

StepDefs is the folder where my step definitions are located in the project tree.

I prefer this way to have everything in one place. Maybe for porting the testsuite to another system it is more common to set a System variable in the target system so the possible customer has always one directory where to place feature-files.

I was searching for a solution how to pass(over write) feature file path glue (steps) path in command line in Dcucumber options. It was quite challenging and I was unable to find the exact solution in many of the forums. Finally found a working solution

Just posting here it could help anybody.

gradle -Dcucumber.options="-g XX.XXX.XXX.steps --tags @xxxxxx featurefilepath/features/" test

You must follow this order having -g as a first option. Thaanks

I am doing like this:-

cucmberOption.properties

#cucumber.options=--plugin html:output/cucumber-html-report 
#src/test/resources
cucumber.options.feature =src/test/resources
cucumber.options.report.html=--plugin html:output/cucumber-html-report
  1. Java Class: CreateCucumberOptions.java

    Method to load properties file:-

    private static void loadPropertiesFile(){
        InputStream input = null;
        try{
            String filename = "cucumberOptions.properties";
            input = CreateCucumberOptions.class.getClassLoader().getResourceAsStream(filename);
            if(input==null){
                LOGGER.error("Sorry, unable to find " + filename);
                return;
            }
            prop.load(input);
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            if(input!=null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
  2. method to get and set CucumberOptions

        private String createAndGetCucumberOption(){       
         StringBuilder sb = new StringBuilder();
         String featureFilesPath = 
         prop.getProperty("cucumber.options.feature");
         LOGGER.info(" featureFilesPath: " +featureFilesPath);
         String htmlOutputReport = 
          prop.getProperty("cucumber.options.report.html");
         LOGGER.info(" htmlOutputReport: " +htmlOutputReport);
         sb.append(htmlOutputReport);
         sb.append(" ");
         sb.append(featureFilesPath);
         return sb.toString();
        }
    
    
     private void setOptions(){
       String value = createAndGetCucumberOption();
       LOGGER.info(" Value: " +value);
       System.setProperty(KEY, value);
       }
    

And main method to run this:-

    public static void main(String[] args) {
        CreateCucumberOptions cucumberOptions = new CreateCucumberOptions();
        JUnitCore junitRunner = new JUnitCore();
        loadPropertiesFile();
        cucumberOptions.setOptions();
        junitRunner.run(cucumberTest.runners.RunGwMLCompareTests.class);
     }

And RunGwMLCompareTests.class is my Cucumber class

@RunWith(Cucumber.class)
@CucumberOptions(
        monochrome = true,
        tags = {"@passed"},
        glue =  "cucumberTest.steps")
public class RunGwMLCompareTests {

    public RunGwMLCompareTests(){

    }
}

So basically nopw you get set output report and feature folders through properties files and others options like glue definations java class. And to run the test cases just run your main class.

Regards, Vikram Pathania

Related