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;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

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)

@RunWith(Cucumber.class)
public class CucumberRunner {
    public static void generateReport() {

    }
}


pom.xml :

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.selenium.cucumber</groupId>
    <artifactId>CucumberReport</artifactId>
    <version>0.0.1</version>
    <dependencies>
        <!--Dependency For Cucumber Java -->
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>1.2.4</version>
        </dependency>
        <!-- Dependency For Cucumber JUnit Runner -->
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.2.4</version>
        </dependency>
        <!-- Dependency For Cucumber Custom Report -->
        <dependency>
            <groupId>net.masterthought</groupId>
            <artifactId>cucumber-reporting</artifactId>
            <version>2.2.0</version>
        </dependency>

    </dependencies>
    <!-- Build Part -->
    <build>
        <plugins>
            <!-- 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 -->

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <includes>
                        <include>com/selenium/cucumber/runner/CucumberRunner.java</include>
                    </includes>
                    <printSummary>true</printSummary>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
            <!-- This is for Cucumber Custom Report plug in we specify the configuration
                details under configuration tag. -->

            <plugin>
                <groupId>net.masterthought</groupId>
                <artifactId>maven-cucumber-reporting</artifactId>
                <version>2.0.0</version>
                <executions>
                    <execution>
                        <id>execution</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <projectName>CucumberReport</projectName>
                            <outputDirectory>${project.build.directory}/cucumber-html-reports</outputDirectory>
                            <cucumberOutput>${project.build.directory}/cucumber.json</cucumberOutput>
                            <parallelTesting>false</parallelTesting>
                            <buildNumber>1</buildNumber>
                            <checkBuildResult>false</checkBuildResult>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <!-- This is for Maven Sure Fire Report plug in.It is used to create documentation
        and html report for this project -->

    <reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-report-plugin</artifactId>
                <version>2.19.1</version>
                <reportSets>
                    <reportSet>
                        <reports>
                            <report>report-only</report>
                        </reports>
                    </reportSet>
                </reportSets>
            </plugin>
        </plugins>
    </reporting>
</project> 

 





1 comment:

  1. Nice Article ! Indian Cyber Army credibility in Ethical hacking training & Cybercrime investigation training is acknowledged across nation as we offer hands on practical knowledge and full assistance with basic as well as advanced level ethical hacking & cybercrime investigation courses. The training is conducted by subject specialist corporate professionals with wide experience in managing real-time ethical hacking/ cyber security projects. Indian Cyber Army implements a blend of academic learning and practical sessions to give the candidate optimum exposure.

    ReplyDelete