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

1
  • 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. Commented Dec 30, 2016 at 11:16

8 Answers 8

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!

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

2 Comments

Of course, this assumes that you didn't use acronyms in your feature.
I can't believe that the original text of the Feature is not available to Steps Definition snippets.
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()); 

Comments

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 

Comments

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]); } 

1 Comment

It is not possible in cucumber 6.
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); 

1 Comment

What is Reporter? I couldn't find this in Cucumber API.
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 !

Comments

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; } 

Comments

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); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.