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

17,899

Solution 1

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!

Solution 2

Kotlin 1.5, cucumber-java 6.10.0:

@Before
fun beforeScenario(scenario: Scenario) {
    println(scenario.uri)
}

In my case prints:

file:///C:/Users/K.H/git/JvmClient/src/jvmTest/resources/features/C197544.feature
Share:
17,899
Umesh Kumar
Author by

Umesh Kumar

10 years of experience in extensive Automation(Web, Mobile, API) Testing

Updated on June 08, 2022

Comments

  • Umesh Kumar
    Umesh Kumar almost 2 years

    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

    • Grasshopper
      Grasshopper over 7 years
      There is a PR for this capability - github.com/cucumber/cucumber-jvm/pull/984, but it is not going to be merged with a release. As a workaround add the feature file name as a tag to the feature file with some kind of identifier. Then you can use scenario.getSourceTagNames() to get all the tags. Using the identifier determine the tag with the feature file name.
  • Tihamer
    Tihamer almost 4 years
    Of course, this assumes that you didn't use acronyms in your feature.
  • Tihamer
    Tihamer almost 4 years
    I can't believe that the original text of the Feature is not available to Steps Definition snippets.
  • UnknownBeast
    UnknownBeast almost 4 years
    It is not possible in cucumber 6.
  • paul
    paul over 3 years
    What is Reporter? I couldn't find this in Cucumber API.