Skip to content

artofcode.info

The beauty of coding!

Menu
  • Home
  • Front-End
    • Angular
    • Javascript
    • ReactJS
  • Back-End
    • Java
    • Php
  • Test Automation
  • News
Menu
cucumber report with logs

How to add log messages to the cucumber Jenkins report

Posted on November 16, 2021December 7, 2022 by guru

Adding log messages to the automated report it’s an important feature, that helps in analyzing and identifying the failing issues. In this post, we are going to help you to add log messages to the 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 in 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 a cucumber scenario holder service

In order to get the running cucumber scenario 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

The 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 a hook that will save the scenario in the 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 the section of the code :

public class GeneralHook {

    @Autowired
    private CucumberScenarioContext scenarioContext;

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

5. Add an 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 the 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

Hope that adding log messages to the cucumber Jenkins report will help you in your personal and professional projects.


Recent Posts

  • An overview of Accelerate: Building and Scaling High-Performance Technology Organizations
  • Cucumber JUnit XML report with logs
  • How to add log messages to the cucumber Jenkins report
  • Factory Method Pattern in the creation of Selenium WebDriver
  • How to attach log message to cucumber report via MDC
  • How to send different values in cucumber features based on profile
  • How to log execution time for cucumber steps
  • Strategy Pattern in Test Automation with Selenium WebDriver

Categories

  • Back-End
  • Java
  • News
  • Test Automation
  • Uncategorized
© 2023 artofcode.info | Powered by Minimalist Blog WordPress Theme