Customized JBehave reports

By default JBehave lays out HTML report data tables horizontally, with headers on the first row and values on the second row below. In some cases though it may be more visually appealing to organise the data vertically instead, with headers and rows in two separate columns.

Not to worry – JBehave can be heavily customized (heavily, but not easily… documentation is scarce). HTML Tables in particular are rendered by a freemarker macro “renderTable” located in ftl/jbehave-html-output.ftl. Simply edit this file and modify the below macro to swap headers and columns.

Original macro:

<#macro renderTable table>
<#assign rows=table.getRows()>
<#assign headers=table.getHeaders()>
<table>
<thead><tr>
<#list headers as header>
<th>${header?html}</th>
</#list>
</tr></thead>
<tbody>
<#list rows as row>
<tr>
<#list headers as header>
<#assign cell=row.get(header)>
<td>${cell?html}</td>
</#list>
</tr>
</#list>
</tbody>
</table>
</#macro>

becomes:


<#macro renderTable table>
<#assign rows=table.getRows()>
<#assign headers=table.getHeaders()>
<table>
<tbody>
<#list headers as header>
<tr>
<#assign cell=rows[0].get(header)>
<td>${header?html}</td>
<td>${cell?html}</td>
</tr>
</#list>
</tbody>
</table>
</#macro>

Copy the edited ftl file in your project resources as custom-html-output.ftl

Next instruct JBehave to look for this custom freemarker file at generation time.


import org.jbehave.core.configuration.Keywords;
import org.jbehave.core.reporters.*;

import java.io.File;

public class CustomHtmlOutput extends HtmlTemplateOutput {

        public CustomHtmlOutput (File file, Keywords keywords){
            super(file, keywords, new FreemarkerProcessor(CustomHtmlOutput.class),"custom-html-output.ftl");
        }

        public static final Format FORMAT = new Format("HTML"){
            @Override
            public StoryReporter createStoryReporter(FilePrintStreamFactory factory, StoryReporterBuilder storyReporterBuilder){
                factory.useConfiguration(storyReporterBuilder.fileConfiguration("html"));
                return new CustomHtmlOutput(factory.getOutputFile(),storyReporterBuilder.keywords())  ;
            }
        };
}


and modify the embedder configuration to use the above class.


            Configuration configuration = injectedEmbedder().configuration();
            configuration.useStoryReporterBuilder(configuration.storyReporterBuilder().withFormats(CustomHtmlOutput.FORMAT));
            injectedEmbedder().runStoriesAsPaths(storyPaths()) ;
        }
Advertisements

2 thoughts on “Customized JBehave reports

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s