1

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;

@Before()
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.

2 Answers 2

2

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:

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

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

Typical example for No 2 is taking screenshot on failure:

@After
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
    }
  }
}
3
  • 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. Commented Apr 1, 2020 at 0:01
  • Sorry, don't know how to do that.
    – Mate Mrše
    Commented Apr 1, 2020 at 6:46
  • 1
    @Mate_Mrse, I will go to Cucumber Devs and if they provide me with a decent answer post it here. Commented Apr 2, 2020 at 3:54
0

If you want to make use of scenario interface in every step definition , then set the scenario into a context and then get it.

public class Context {
//use lombok 
@Getter @Setter
private Scenario scenario;
} 

public class Hook
{

@Before
public void setUp(Scenario scenario)
{
  context.setScenrario(scenario)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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