I'm looking for information/guidelines on how to use io.cucumber.core.api.Scenario interface? I read through the API reference documentation and Official Cucumber-Java documentation and some scattered usage examples, but still couldn't figure out what is the correct way to use it, and what happens when I call methods of this interface.

Why would I like to find this info? I am maintaining a sizable automation framework, and moving it to use dependency injection for step classes. But the previous developer used scenario.write as a logging tool and I was not sure how to refactor that, or what implementation to bind to Scenario interface.

Upd. Here is an example of what I have in classes that that use Scenario.write as logger.

// Let's assume that code below resides in some Java class that defines Gherkin steps
private Scenario scenario;

public void before(Scenario scenario) {
    this.scenario = scenario;

@Then("^Received XML file should match \"([^\"]*)\"$")
public void receivedXmlFileShouldMath(String expectedFilePath) {

    // ignoring how we read expected file and where received file content is stored

    scenario.write("Ignoring date field in xml message");
    compareXml(expectedXml, actualXml)

If it is possible I would like to move Scenario injection to the class constructor, or use some other technique to still get the message in the report.


In my experience, that interface is used in one of two ways:

  1. getting info on the test case (getting the name, id, status of the test case, etc)
  2. writing to the report file (screenshots and text)

Some example usages for No. 1:

public void setUp(Scenario scenario) {
    System.out.println(scenario.getName() + " is starting.")

public void afterTest(Scenario scenario) {
    if (scenario.isFailed()) {
        // do stuff

Typical example for No 2 is taking screenshot on failure:

public void embedScreenshot(Scenario scenario) {
  if (scenario.isFailed()) {
    try {
      byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
      scenario.embed(screenshot, "image/png");
      scenario.write("This text is written to the report...")
    } catch (Exception e) {
      // do stuff
| improve this answer | |
  • thank you for your response. I've seen the examples like ones you showed. Would you mind checking the update I added to my question. – GKalnytskyi Apr 1 at 0:01
  • Sorry, don't know how to do that. – Mate Mrše Apr 1 at 6:46
  • 1
    @Mate_Mrse, I will go to Cucumber Devs and if they provide me with a decent answer post it here. – GKalnytskyi Apr 2 at 3:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.