How to capture step name being executed in @BeforeStep annotation

1,617 views
Skip to first unread message

Anukul Singhal

unread,
Jun 15, 2018, 9:39:47 AM6/15/18
to Cukes
Hi,

I have a use case which requires me to capture the step name being executed, just like how you can do it with Scenario class.  Here is the Hooks class content that I have:

 public class Hooks {

    TestContext testContext;

    public Hooks(TestContext context) {
        testContext = context;
    }

    @Before
    public void beforeScenario(Scenario scenario) {
        System.out.println("Executing before scenario for : " + scenario.getName());
    }

    @After
    public void afterScenario(Scenario scenario) {
        System.out.println("Executing after scenario for : " + scenario.getName());
    }

    @BeforeStep
    public void beforeStep() {
        System.out.println("In Before step...");
        //TODO: Need to capture the Step which is going to execute
        //Need some kind of implementation like step.getName()
    }

    @AfterStep
    public void afterStep() {

    }

}

I tried to pass in PickleStep object as an argument to beforeStep() method but seems like it only likes to have args of type cucumber.api.Scenario class (or no args).  Is there anyway I can capture the current step name being executed in @BeforeStep annotation?

I am using cucumber-java - 3.0.2 (io.cucumber)

Thanks,
Anukul

MP Korstanje

unread,
Jun 17, 2018, 6:02:39 AM6/17/18
to Cukes
Hey Anukul,

I suspect you are trying to use this information to create a report of some sort. If so you should look into implementing a formatter instead. Have a look at the existing formatters to see how:


-MP

Anukul Singhal

unread,
Jun 18, 2018, 4:36:39 PM6/18/18
to Cukes
Hey MP,

Thanks a ton for this reference, was able to create a custom formatter (captured test step name), and plugged in to the cucumber options.

Thanks,
Anukul

praveen halhalli

unread,
Mar 19, 2020, 9:37:16 AM3/19/20
to Cukes
Hi Anukul, MP,

Could you please help me here for fetching step name, the above git hub link is not working.
Reply all
Reply to author
Forward
0 new messages