how to get current Cucumber feature file name at runtime using Java

Umesh Kumar picture Umesh Kumar · Dec 30, 2016 · Viewed 12.4k times · Source

I want get current feature file name at runtime using Java. I have scenario info in hook but unable to get feature file

@Before
    public void before(final Scenario scenario) {
               this.scenario = scenario;
      }

Do we have any similar thing to get current Feature file name ?? i am using cucumber version 1.2.4

Answer

Carlo Matulessy picture Carlo Matulessy · Mar 22, 2017

UPDATE:

This is my implementation for feature names starting with an uppercase letter like in the example:

private String getFeatureFileNameFromScenarioId(Scenario scenario) {
    String featureName = "Feature ";
    String rawFeatureName = scenario.getId().split(";")[0].replace("-"," ");
    featureName = featureName + rawFeatureName.substring(0, 1).toUpperCase() + rawFeatureName.substring(1);

    return featureName;
}

ORIGINAL:

I don't know if this is useful for you, but I would suggest to use scenario.getId()

This will give you the feature file name and scenario name, for example:

Feature: Login to the app

Scenario: Login to the app with password
Given I am on the login screen
When I enter my passcode
Then I press the ok button

with scenario.getId() you would get the following:

login-to-the-app;login-to-the-app-with-password

Hope this helps you!