question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to get Scenario step text at runtime

See original GitHub issue

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

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
Phonesiscommented, Feb 18, 2020

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

0reactions
mpkorstanjecommented, Jul 17, 2020

It is not productive to discuss multiple separate issues on a closed ticket. Please open a new issue and take the time to describe the problem you are trying solve and in which ways you think Cucumber is deficient in that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get all the cucumber scenario steps in before hook?
Try something like this: @Before public void setUp(Scenario scenario) throws Exception{ tags = (ArrayList<String>) scenario.
Read more >
Cucumber - Scenario Outline - Tutorialspoint
Go to package explorer on the left hand side of Eclipse. · Expand the project CucumberTest. · Locate pom.xml file. · Right-click and...
Read more >
Karate | Test Automation Made Simple.
Easily define all environments in a single file and share variables across all scenarios. Full script-ability via JS or Java interop. One nice...
Read more >
Cucumber Hooks - Baeldung
Included are the scenario name, number of steps, names of steps, and status (pass or fail). This can be useful if we want...
Read more >
Gherkin Reference - Cucumber Documentation
Feature: Guess the word # The first example has two steps Scenario: Maker ... you cannot have a Given , When , Then...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found