7

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

Umesh Kumar
  • 1,387
  • 2
  • 16
  • 34
  • 1
    There is a PR for this capability - https://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. – Grasshopper Dec 30 '16 at 11:16

8 Answers8

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!

Carlo Matulessy
  • 975
  • 1
  • 13
  • 27
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
K.H.
  • 1,383
  • 13
  • 33
1

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());
mcleap
  • 11
  • 2
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]);
    }
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);
Jigs
  • 1
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 !

Motivated Mind
  • 108
  • 1
  • 9
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;
}
0009laH
  • 1,960
  • 13
  • 27
Piotr P
  • 1
  • 1
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);
iso
  • 1
  • 2