2

In cucumber framework, is there a way I can get the currently executing feature file's name or even better it's folder path in the step definition file?

My project is implemented in java and I'm using intelliJ idea. I've already tried using before hook which helps me fetch the scenario instance. But, I can't find a way to get the feature file info.

3 Answers 3

1

The only solution I could come up with is by mentioning the feature file's name in the feature file Title and then in the @Before hook get this title using scenario.getId().split(";")[0]

And then parse this Title to fetch the feature file name and store it in a variable which I can later on use in the @After hook to pass it to the Custom Formatter to parse my feature file and save it's data in the database.

Sign up to request clarification or add additional context in comments.

Comments

0

Not supposed to? The reason you would want it is for traceability and explainability. Very helpful for debugging, too. Especially when you have more than 20 gherkin files (with up to 200 steps) and more than 20 step definition files.
I put one of these at the top of every Java step definition file:

@Before 
public void printScenarioName(Scenario scenario) {
    this.scenario = scenario;
    this.featureName = CukeUtils.getFeatureName(scenario);
    String result = "@Before:\n*************Setting Feature: " + this.featureName + 
            "\n*************Setting Scenario: " + this.scenario.getName();
    log.info(result);
}

where in CukeUtils I have defined:

public static String getFeatureName(Scenario scenario) {
        String featureName = "";
        System.out.println("scenario.getId(): " + scenario.getId());  
        // Usually the scenario Id is doctored version of the lines following 
        // the Feature: and the Scenario: keywords.
        // Eg.: scenario.getId(): a-long-(20-minute)-non-invasive-smoke-test-that-
        //comfirms-that-i-can-login-to-area51-via-the-nasa-portal;as-a-superuser-i-
        //must-be-able-to-login-to-area51-via-the-nasa-portal-so-that-i-can-access-
        //all-the-secret-files
        String rawFeatureName = scenario.getId().split(";")[0]
                         .replace("-i-", "-I-").replace("-"," ");
        featureName = featureName + rawFeatureName.substring(0, 1).toUpperCase() + 
                rawFeatureName.substring(1).replace("nasa", "NASA");
        return featureName;
    }

Comments

-1

Long story short: you are not supposed to. Ask yourself what you are really trying to achieve, why you need the path in the first place. Is it because of some external file? Do you really need an external file or can the content be sufficiently represented in you feature files? If you really need an external file why not have it as a resource? And so on.

3 Comments

Thanks Peter for your reply. To give you a gist of what I'm trying to achieve is a way to insert the test cases and test data of the currently executing feature file into a database. I have created a custom formatter to parse a given feature file and store its data into the database. What I want is a way to provide this formatter the path of the feature file which is currently being executed. I have the @After hook defined in the step definition file which will make a call to my custom formatter class but I'm unable to pass any reference of the feature file in it. :(
If you are using a custom formatter, you are already getting the info on the feature file in the void feature(Feature feature) method. I mean if we are talkin about the same kind of custom formatter: automationrhapsody.com/create-cucumber-jvm-custom-formatter
You're correct. The custom formatter helps me parse the feature file. But I need to pass the path of the feature file which I want to parse. In my case, it's the one which is currently being executed in my IDE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.