Saturday, May 14, 2016

Custom Cucumber Reporting Using Maven

On previous post we learn how to create custom cucumber report using normal java project.

If we use this report for normal java project we create a custom annotation to generate this report, but if we use maven for this report then it is so easy to create the report and we need very less code.

So how we create this custom cucumber report using maven

Step 1:

 Open eclipse and go to this path File ->New -> Project... ->Maven -> Maven Project.

Click on next after select the Maven Project

Step 2:

 Check the  "Create a simple project (skip archetype selection)" and click on next.
Type the Group Id and Artifact Id under the Artifact section and click finish button.
Group Id means if any organization have multiple project under one group like here we group all cucumber projects under one group.

Artifact Id is the project name.

Step 3:

Our project structure now created in our eclipse work space and it looks like

Step 4 :

We create feature file and put it under src/test/resources.Cucumber runner as well as step file put into src/test/java after creating package. So it looks like

 Step 5 :

Create pom.xml file with all the details and run it using the below command. Here we need to remember that custom report only generated if we use verify or later sequence command from maven default life cycle. So we use install here.

clean install site

Step 6:

 After running this pom.xml report generated under target folder.

Complete Code :

Feature File:

Feature: Calculator
  As a user
  I want to use a calculator
  So that I don't need to calculate myself

  Scenario: Add two numbers
    Given I have a calculator
    When I add 2 and 3
    Then the result should be 4

Step File :

package com.selenium.cucumber.steps;
import org.junit.Assert;

public class StepFile {
    int actualSum;

    @Given("^I have a calculator$")
    public void iHaveACalculator() throws Throwable {


    @When("^I add (\\d+) and (\\d+)$")
    public void iAddAnd(int value1, int value2) throws Throwable {
        actualSum = value1 + value2;

    @Then("^the result should be (\\d+)$")
    public void theResultShouldBe(int expectedSum) throws Throwable {
        Assert.assertEquals(expectedSum, actualSum);



Runner :
package com.selenium.cucumber.runner;

import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.SnippetType;
import cucumber.api.junit.Cucumber;

@CucumberOptions(plugin = { "html:target/cucumber-html-report", "json:target/cucumber.json" }, features = {
        "src/test/resources/featureFile.feature" }, glue = {
                "com/selenium/cucumber/steps" }, strict = true, dryRun = false, monochrome = true, snippets = SnippetType.CAMELCASE)

public class CucumberRunner {
    public static void generateReport() {


pom.xml :

<project xmlns="" xmlns:xsi=""
        <!--Dependency For Cucumber Java -->
        <!-- Dependency For Cucumber JUnit Runner -->
        <!-- Dependency For Cucumber Custom Report -->

    <!-- Build Part -->
            <!-- Maven Sure Fire Plug In. It is used to select runner class from any
                folder location as well as not stop to execution whenever any test fail -->

            <!-- This is for Cucumber Custom Report plug in we specify the configuration
                details under configuration tag. -->

    <!-- This is for Maven Sure Fire Report plug in.It is used to create documentation
        and html report for this project -->