How to Embed / Attach screenshots when each step is executed in Cucumber-JVM

30 September, 2015
Recently I had to take several screenshots after each Step in executed and embed them in the results. As there is no out of the box way of doing this, I used the combination of listeners provided by Cucumber-jvm Formatter and Reporter Interface.

Steps to do this:
1. Create a class for example: CustomFormatter which implements Formatter, Reporter
2. Create a Before hook accepting a Scenario object that you store in an instance variable.
3. The method which gets triggered after each step is executed is result() of Reporter interface. Here you can take the screenshot but you wont get the step name here.
The step name is given by the step() of Formatter interface. However that is not after each step, it gives the steps at the start before the scenario is run. Hence we need to store the step names at the start and iterate over it in the result() by incrementing the step names by 1 each time. In the result(), do Scenario.embed(bytes, mime) several times.

No comments:

Post a Comment