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

8

8

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

Mortify answered 30/12, 2016 at 10:38 Comment(1)
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.Beeves
J
6

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!

Jabe answered 22/3, 2017 at 8:17 Comment(2)
Of course, this assumes that you didn't use acronyms in your feature.Detonate
I can't believe that the original text of the Feature is not available to Steps Definition snippets.Detonate
K
2

There is an easier way to extract the feature name (without .feature postfix) from Scenario if you can add Apache commons-io on your classpath:

String featureName = FilenameUtils.getBaseName(scenario.getUri().toString());

If you need the full feature file name with postfix you should use the getName(...) method instead:

String fullFeatureName = FilenameUtils.getName(scenario.getUri().toString());
Kayceekaye answered 17/3, 2022 at 9:6 Comment(0)
A
1

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
Argolis answered 23/10, 2021 at 19:36 Comment(0)
H
0

I used the below method at Hooks class

    @Before
    public void beforeScenario(Scenario scenario){

// scenarioId = "file:///**/src/test/resources/features/namefeature.feature:99"

        String scenarioId=scenario.getId(); 

        int start=scenarioId.indexOf(File.separator+"features"+File.separator);
        int end=scenarioId.indexOf(".");

        String[] featureName=scenarioId.substring(start,end).split(File.separator+"features"+File.separator);
        System.out.println("featureName ="+featureName[1]);
    }
Haight answered 7/6, 2020 at 5:2 Comment(1)
It is not possible in cucumber 6.Hulky
N
0

You can use Reporter to get the current running instance and then extract our the actual feature name from the feature file like so:

    Object[] paramNames = Reporter.getCurrentTestResult().getParameters();          
    String featureName = paramNames[1].toString().replaceAll("^\"+|\"+$", "");
    System.out.println("Feature file name: " + featureName);
Newland answered 20/8, 2020 at 23:26 Comment(1)
What is Reporter? I couldn't find this in Cucumber API.Degree
B
0

Create a listener as below

import io.cucumber.plugin.ConcurrentEventListener;
import io.cucumber.plugin.event.EventHandler;
import io.cucumber.plugin.event.EventPublisher;
import io.cucumber.plugin.event.TestCaseStarted;

public class Listener implements ConcurrentEventListener {

  @Override
  public void setEventPublisher(EventPublisher eventPublisher) {
    eventPublisher.registerHandlerFor(TestCaseStarted.class, testCaseStartedEventHandler);
  }

  private final EventHandler<TestCaseStarted> testCaseStartedEventHandler = event -> {
    System.out.println("Current file fame : " + event.getTestCase().getUri().toString());
  };
}

And then supply your listener to cucumber as below

"-p", "com.myProject.listener.Listener"

This will give you feature file name !

Baronial answered 23/8, 2020 at 17:39 Comment(0)
C
0

maybe like this, its return only filename:

private String getFeatureFileNameFromScenarioId(Scenario scenario) {
    String[] tab = scenario.getId().split("/");
    int rawFeatureNameLength = tab.length;
    String featureName = tab[rawFeatureNameLength - 1].split(":")[0];
    System.out.println("featureName: " + featureName);

    return featureName;
}
Cankerworm answered 30/9, 2020 at 19:12 Comment(0)
S
0

Create Scenario instance in Hooks class with static keyword. Than you can use the following codes inside step definitions.

String currentFilePath = Hooks.scenario.getUri().toString();
String fileName = currentFilePath.substring(currentFilePath.lastIndexOf("/")+1);
Supersonic answered 24/3, 2023 at 14:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.