All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.mkolisnyk.cucumber.reporting.CucumberDetailedResults Maven / Gradle / Ivy

There is a newer version: 1.3.5
Show newest version
package com.github.mkolisnyk.cucumber.reporting;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.xhtmlrenderer.pdf.ITextRenderer;

import com.github.mkolisnyk.cucumber.reporting.types.result.CucumberBeforeAfterResult;
import com.github.mkolisnyk.cucumber.reporting.types.result.CucumberFeatureResult;
import com.github.mkolisnyk.cucumber.reporting.types.result.CucumberScenarioResult;
import com.github.mkolisnyk.cucumber.reporting.types.result.CucumberStepResult;

/**
 * @author Myk Kolisnyk
 */
public class CucumberDetailedResults extends CucumberResultsCommon {
    private String outputDirectory;
    private String outputName;
    private String screenShotLocation;
    private String screenShotWidth;

    /**
     * @return the outputDirectory
     */
    public final String getOutputDirectory() {
        return outputDirectory;
    }

    /**
     * @param outputDirectoryValue the outputDirectory to set
     */
    public final void setOutputDirectory(String outputDirectoryValue) {
        this.outputDirectory = outputDirectoryValue;
    }

    /**
     * @return the outputName
     */
    public final String getOutputName() {
        return outputName;
    }

    /**
     * @param outputNameValue the outputName to set
     */
    public final void setOutputName(String outputNameValue) {
        this.outputName = outputNameValue;
    }

    /**
     * @return the screenShotLocation
     */
    public final String getScreenShotLocation() {
        return screenShotLocation;
    }

    /**
     * @param screenShotLocationValue the screenShotLocation to set
     */
    public final void setScreenShotLocation(String screenShotLocationValue) {
        this.screenShotLocation = screenShotLocationValue;
    }

    /**
     * @return the screenShotWidth
     */
    public final String getScreenShotWidth() {
        return screenShotWidth;
    }

    /**
     * @param screenShotWidthValue the screenShotWidth to set
     */
    public final void setScreenShotWidth(String screenShotWidthValue) {
        this.screenShotWidth = screenShotWidthValue;
    }

    private String getReportBase() throws IOException {
        InputStream is = this.getClass().getResourceAsStream("/results-report-tmpl.html");
        String result = IOUtils.toString(is);
        return result;
    }

    private String escapeHtml(String input) {
        return StringEscapeUtils.escapeHtml(input);
    }

    private String generateOverview(CucumberFeatureResult[] results) {
        final int secondsInMinute = 60;
        final int secondsInHour = 3600;
        int featuresPassed = 0;
        int featuresFailed = 0;
        int featuresUndefined = 0;
        int scenariosPassed = 0;
        int scenariosFailed = 0;
        int scenariosUndefined = 0;
        int stepsPassed = 0;
        int stepsFailed = 0;
        int stepsUndefined = 0;
        final float highestPercent = 100.f;
        float overallDuration = 0.f;
        for (CucumberFeatureResult result : results) {
            result.valuate();
            overallDuration += result.getDuration();
            if (result.getStatus().equals("passed")) {
                featuresPassed++;
            } else if (result.getStatus().equals("failed")) {
                featuresFailed++;
            } else {
                featuresUndefined++;
            }
            scenariosPassed += result.getPassed();
            scenariosFailed += result.getFailed();
            scenariosUndefined += result.getUndefined() + result.getSkipped();

            for (CucumberScenarioResult scenario : result.getElements()) {
                stepsPassed += scenario.getPassed();
                stepsFailed += scenario.getFailed();
                stepsUndefined += scenario.getUndefined() + scenario.getSkipped();
            }
        }
        return String.format(""
                + ""
                + ""
                    + ""
                + ""
                    + ""
                + ""
                    + "
PassedFailedUndefined%%Passed
Features%d%d%d%.2f
Scenarios%d%d%d%.2f
Steps%d%d%d%.2f
" + "
Overall Duration: %dh %02dm %02ds
", featuresPassed, featuresFailed, featuresUndefined, highestPercent * (float) featuresPassed / (float) (featuresPassed + featuresFailed + featuresUndefined), scenariosPassed, scenariosFailed, scenariosUndefined, highestPercent * (float) scenariosPassed / (float) (scenariosPassed + scenariosFailed + scenariosUndefined), stepsPassed, stepsFailed, stepsUndefined, highestPercent * (float) stepsPassed / (float) (stepsPassed + stepsFailed + stepsUndefined), (int) overallDuration / secondsInHour, ((int) overallDuration % secondsInHour) / secondsInMinute, ((int) overallDuration % secondsInHour) % secondsInMinute); } private String generateNameFromId(String scId) { String result = scId.replaceAll("[; !@#$%^&*()+=]", "_"); return result; } private String generateTableOfContents(CucumberFeatureResult[] results) { String reportContent = ""; reportContent += "

Table of Contents

    "; for (CucumberFeatureResult result : results) { reportContent += String.format( "
  1. %s
      ", result.getStatus(), result.getId(), escapeHtml(result.getName())); for (CucumberScenarioResult scenario : result.getElements()) { if (scenario.getKeyword().contains("Scenario")) { reportContent += String.format( "
    1. %s
    2. ", scenario.getStatus(), scenario.getId(), escapeHtml(scenario.getName())); } } reportContent += "
  2. "; } reportContent += "
"; return reportContent; } private String generateStepRows(CucumberStepResult step) { String reportContent = ""; if (step.getRows() != null) { reportContent += String.format( "", step.getResult().getStatus()); for (int i = 0; i < step.getRows().length; i++) { reportContent += ""; for (int j = 0; j < step.getRows()[i].length; j++) { reportContent += String.format("", escapeHtml(step.getRows()[i][j])); } reportContent += ""; } reportContent += "
%s
"; } return reportContent; } private String generateDocString(CucumberStepResult step) { String reportContent = ""; if (StringUtils.isNotBlank(step.getDocString())) { reportContent += String.format( "", step.getResult().getStatus()); reportContent += String.format("
%s
", escapeHtml(step.getDocString()).replaceAll(System.lineSeparator(), "

")); reportContent += ""; } return reportContent; } private String generateScreenShot(CucumberScenarioResult scenario, CucumberStepResult step) { String reportContent = ""; if (step.getResult().getStatus().trim().equalsIgnoreCase("failed")) { reportContent += String.format( "
%s%s
", step.getResult().getStatus(), "
", escapeHtml(step.getResult().getErrorMessage()).replaceAll(System.lineSeparator(), "

" + System.lineSeparator()) ); String filePath = this.getScreenShotLocation() + this.generateNameFromId(scenario.getId()) + ".png"; File shot = new File(this.outputDirectory + filePath); if (shot.exists()) { String widthString = ""; if (StringUtils.isNotBlank(this.getScreenShotWidth())) { widthString = String.format("width=\"%s\"", this.getScreenShotWidth()); } reportContent += String.format( "", step.getResult().getStatus(), filePath, widthString ); } } return reportContent; } private String generateBeforeAfterRow(CucumberBeforeAfterResult results, String name) { if (results != null) { String error = escapeHtml(results.getResult().getErrorMessage()); if (StringUtils.isBlank(error)) { error = ""; } return String.format( "%s%s" + "%s", results.getResult().getStatus(), name, results.getResult().getDurationTimeString("HH:mm:ss:S"), results.getResult().getStatus(), "
" + error.replaceAll(System.lineSeparator(), "

") + "
" ); } return ""; } private String generateStepsReport(CucumberFeatureResult[] results) throws IOException { String content = this.getReportBase(); content = content.replaceAll("__TITLE__", "Detailed Results Report"); content = content.replaceAll("__OVERVIEW__", generateOverview(results)); String reportContent = ""; reportContent += generateTableOfContents(results); reportContent += "

Detailed Results Report

"; for (CucumberFeatureResult result : results) { reportContent += String.format( "" + "" + "" + "" + "" + "" + ""; } reportContent += "
Feature: %s

%s
Passed: %dFailed: %dUndefined: %dDuration: %.2fs
", result.getStatus(), result.getId(), escapeHtml(result.getName()), result.getStatus(), escapeHtml(result.getDescription()).replaceAll(System.lineSeparator(), "

" + System.lineSeparator()), result.getStatus(), result.getPassed(), result.getFailed(), result.getUndefined() + result.getSkipped(), result.getDuration(), result.getStatus()); for (CucumberScenarioResult scenario : result.getElements()) { reportContent += String.format( "" + "" + "" + "" + "" + "%s" + "" + "" + this.generateBeforeAfterRow(scenario.getAfter(), "After") + ""; } reportContent += "
%s: %s

%s
Passed: %dFailed: %dUndefined: %dDuration: %.2fs
", scenario.getStatus(), scenario.getKeyword(), scenario.getId(), escapeHtml(scenario.getName()), scenario.getStatus(), escapeHtml(scenario.getDescription()).replaceAll(System.lineSeparator(), "

" + System.lineSeparator()), scenario.getStatus(), scenario.getPassed(), scenario.getFailed(), scenario.getUndefined() + scenario.getSkipped(), scenario.getDuration(), this.generateBeforeAfterRow(scenario.getBefore(), "Before"), scenario.getStatus()); for (CucumberStepResult step : scenario.getSteps()) { reportContent += String.format( "", step.getResult().getStatus(), step.getKeyword(), escapeHtml(step.getName()), step.getResult().getDurationTimeString("HH:mm:ss:S") ); reportContent += this.generateStepRows(step); reportContent += this.generateDocString(step); reportContent += this.generateScreenShot(scenario, step); } reportContent += "
%s %s%s
" + "Back to Table of Contents
"; reportContent = reportContent.replaceAll("£", "£"); reportContent = reportContent.replaceAll("[$]", "$"); content = content.replaceAll("__REPORT__", reportContent); return content; } public void executeDetailedResultsReport(boolean toPdf, boolean aggregate) throws Exception { CucumberFeatureResult[] features = readFileContent(aggregate); String formatName = ""; if (aggregate) { formatName = "%s%s%s-agg-test-results.html"; } else { formatName = "%s%s%s-test-results.html"; } File outFile = new File( String.format(formatName, this.getOutputDirectory(), File.separator, this.getOutputName())); String content = generateStepsReport(features); FileUtils.writeStringToFile(outFile, content, "UTF-8"); if (toPdf) { String url = outFile.toURI().toURL().toString(); String outputFile = this.getOutputDirectory() + File.separator + this.getOutputName() + "-test-results.pdf"; OutputStream os = new FileOutputStream(outputFile); ITextRenderer renderer = new ITextRenderer(); renderer.setDocument(url); renderer.layout(); renderer.createPDF(os); os.close(); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy