How to add log messages to cucumber Jenkins report

As you may know by default pretty cucumber reports don’t save log messages in the cucumber.json file by default in order to display more information in the generated HTML report. This post will cover this function via declaring a java Appender in order to store log messages in the cucumber.json file. Before starting I would like to mention that there are two ways to add log messages under the cucumber report and this is the second example, first example has been covered under the following post.

1.Adding java appender

In this example, we will use Cucumber 6 and spring boot as test starters. First, we will create an appender class that will receive a log event and will attach it to the cucumber scenario in order to save it to the result cucumber.json file.

/**
 * CucumberScenarioAppender appends logs to the current cucumber scenario output
 */
@Component
public class CucumberScenarioAppender extends AppenderBase<ILoggingEvent> {

    private Scenario getScenario() {
        return StaticSpringContext.getBean(CucumberScenarioContext.class).getCurrentScenario();
    }

    @Override
    protected void append(ILoggingEvent iLoggingEvent) {
        Scenario cukeScenario = getScenario();
        if (cukeScenario != null)
            cukeScenario.log(iLoggingEvent.getFormattedMessage());
    }
}

2. Create cucumber scenario holder service

In order to get the running cucumber scenario to let’s first save the started scenario to a holder service class that will allow us to get the scenario in our appender.

/**
 * CucumberScenarioContext is a holder for the current Cucumber Scenario object
 * <p>
 * current scenario should be assigned in before hooks
 */
@Component
@ScenarioScope
public class CucumberScenarioContext {

    private Scenario currentScenario;

    public Scenario getCurrentScenario() {
        return currentScenario;
    }

    public void setCurrentScenario(Scenario scenario) {
        currentScenario = scenario;
    }
}

3. Create static context application aware

Following class will allow us to get from the spring context class based on name in a static context. This will be used in a moment of getting a Scenario from our holder class.

@Component
public class StaticSpringContext implements ApplicationContextAware {
    private static ApplicationContext context;

    public static <T> T getBean(Class<T> beanClass) {
        return context.getBean(beanClass);
    }

    @Override
    public void setApplicationContext(ApplicationContext context) {
        StaticSpringContext.context = context;
    }
}

4. Add hook that will save scenario in holder service

One of the most important steps is to add a hook that will save the cucumber scenario in the scenario holder class, in our case this will be done by following section of the code :

public class GeneralHook {

    @Autowired
    private CucumberScenarioContext scenarioContext;

    @Before(order = 0)
    public void loggerSetup(Scenario scenario) {
        scenarioContext.setCurrentScenario(scenario);
    }
}

5.Add appender link in logback configuration

Last step in order to link Java Appender with log events we need to add an appender in logback configuration.

<appender name="Cucumber" class="com.automation.solutions.appender.CucumberScenarioAppender"/>

And add it to root level too.

 <root level="INFO">
        <appender-ref ref="RootSiftAppender"/>
        <appender-ref ref="Cucumber"/>
        <appender-ref ref="TIME_BASED_FILE"/>
        <appender-ref ref="STDOUT"/>
    </root>

A working example explained above example can be found here.

The end result will be similar :

cucumber report with logs