Skip to content

Conversation

@glaciousm
Copy link

Summary

This PR implements the feature requested in #1805 - allowing @BeforeStep and @AfterStep hooks to receive step information (keyword, text, line number) alongside the existing Scenario parameter.

New API:

@BeforeStep
public void beforeStep(Scenario scenario, Step step) {
    System.out.println("Running: " + step.getKeyword() + step.getText());
    System.out.println("At line: " + step.getLine());
}

@AfterStep  
public void afterStep(Scenario scenario, Step step) {
    if (scenario.isFailed()) {
        System.out.println("Failed step: " + step.getText());
    }
}

Changes

  • Add new io.cucumber.java.Step interface exposing step details
  • Add internal StepInfo wrapper class implementing the Step interface
  • Extend HookDefinition with execute(state, step) default method
  • Update JavaHookDefinition to support 2-parameter step hooks
  • Pass step info through HookTestStep execution chain

Backward Compatibility

  • Existing hooks with 0 or 1 parameter continue to work unchanged
  • No breaking changes to the public API

Test plan

  • All existing cucumber-core tests pass (665 tests)
  • All existing cucumber-java tests pass (115 tests)
  • Tested with a real Selenium/Cucumber project

Closes #1805

Allow @BeforeStep and @afterstep hooks to receive step information
(keyword, text, line number) alongside the existing Scenario parameter.

New API:
```java
@BeforeStep
public void beforeStep(Scenario scenario, Step step) {
    System.out.println("Running: " + step.getKeyword() + step.getText());
    System.out.println("At line: " + step.getLine());
}
```

Changes:
- Add new io.cucumber.java.Step interface exposing step details
- Add StepInfo wrapper class implementing Step interface
- Extend HookDefinition with execute(state, step) default method
- Update JavaHookDefinition to support 2-parameter step hooks
- Pass step info through HookTestStep execution chain
- Maintain full backward compatibility with existing hooks

Closes cucumber#1805
Copy link
Contributor

@mpkorstanje mpkorstanje left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this looks like a decent approach.

  • The HookTestStep::run method is duplicated. I don't think that this is necessary. In TestStep::executeStep it should be possible to pass the step argument and propagate it down to HookDefinition::execute by adding an extra argument to StepDefinitionMatch::runStep. If it goes unused in other places, that is okay.

  • io.cucumber.plugin.event.StepArgument shouldn't be visible to users but I don't know what to request that yet.

* @param step the step being executed
* @throws Throwable if the hook fails
*/
public void runStep(TestCaseState state, io.cucumber.plugin.event.Step step) throws Throwable {
Copy link
Contributor

@mpkorstanje mpkorstanje Jan 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code doesn't have to be duplicated. The StepDefinitionMatch interface is an package private interface. It is okay t o add parameter too it.

@mpkorstanje
Copy link
Contributor

Don't worry about the Semver check btw.

- Remove duplicated run method from HookTestStep, use parent's implementation
- Pass step argument through TestStep::executeStep and ExecutionMode
- Add runStep(state, step) default method to StepDefinitionMatch interface
- Make HookDefinition.execute(state) a default no-op and deprecate it
- Remove getArgument() from Step interface (StepArgument visibility concern)
- Refactor JavaHookDefinition validation to explicitly check hook types
- Update tests to match new error message format
@glaciousm
Copy link
Author

pushed changes based on the comments, if you could re-review

Copy link
Contributor

@mpkorstanje mpkorstanje left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please review your own code before asking me for a review.

  1. These changes came in incredibly fast.
  2. The changes are as requested in letter, but not in spirit. I find that I'm writing the same comments as I did in my previous review, but for exactly one level higher in the call chain.
  3. There is a rather obvious blunder in the code that regular refactoring tools wouldn't produce.

Normally I'd give you the benefit of the doubt but I do see several LLM based projects on your profile so I am skeptical that you've done the necessary work to create a correct pull request.

@glaciousm
Copy link
Author

glaciousm commented Jan 15, 2026

I will go through all my changes, one by one, apologies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Step information in @BeforeStep and @AfterStep hook

2 participants