javajbehave

How to get current story/scenario in after scenario in jbehave


I'm trying to get the current running story/scenario in an afterScenario, using a 'context' object:

    Context context = new Context();
    String currentScenario = context.getCurrentScenario();
    System.out.println("currentScen:" + currentScenario);

but currentScenario is returning 'null'. Why is this and how do I return the current running story/scenario

Many thanks for your help


Solution

  • I think you are asking two different questions. I'll try to address them separately.

    The first question is "Why does creating a context and calling getCurrentScenario() not return the currently running scenario?". I assuming you are referencing the org.jbehave.core.context.Context class. In which case, you have created a new instance of the context which has no details on the current running scenario. So it makes sense that this returns null. Somewhere there may be a StepMonitor with such a context, but you don't have access to it from that code.

    The second is "How can I access the current scenario from within a AfterScenario method?" The normal method for accessing such data from inside of the lifecycle methods (Before/After Scenario/Story/Stories) is using a reporter. This is basically a listener that gets events as the stories are running. You can then keep track if the story/scenario/step in progress and provide it back to you lifecycle methods. Rather than providing an example, I will point you to a related question that already has such an example that covers the same use case.

    Jbehave get story name inside @BeforeStory