cucumber-jvm: Unable to get Scenario step text at runtime

I am trying to retrieve the Scenario step text at run time. This used to be possible in the Java implementation via reflection. See answer here:

https://stackoverflow.com/questions/39664422/in-cucumber-is-it-possible-to-programmatically-get-the-current-step-being-execu

    Field f = scenario.getClass().getDeclaredField("testCase");
    f.setAccessible(true);
    TestCase r = (TestCase) f.get(scenario);

    //You need to filter out before/after hooks
    List<PickleStepTestStep> stepDefs = r.getTestSteps()
            .stream()
            .filter(x -> x instanceof PickleStepTestStep)
            .map(x -> (PickleStepTestStep) x)
            .collect(Collectors.toList());

However, with the more recent cucumber (from 4.8.0) this is no longer possible. I get the following error:

java.lang.NoSuchFieldException: testCase

Is there any other means to obtain the step text? I can do it with Cucumber.js via the Scenario.pickle object

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 16 (8 by maintainers)

Most upvoted comments

What are you using the step text for?

Your favourite answer for this 😉 Reporting/logging. I am aware that the plugin route is an option. However, we already have a fully working reporting mechanism working very well using the hooks. The only limitation we have is the fact we can’t retrieve the step text.

I have done something similar in cucumber.js along with SpecFlow (for .NET) so just seems a shame it’s not currently possibly with Java