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

com.github.mkolisnyk.cucumber.reporting.CucumberResultsOverview 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.IOException;
import java.io.InputStream;
import java.net.URL;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.github.mkolisnyk.cucumber.reporting.types.result.CucumberFeatureResult;
import com.github.mkolisnyk.cucumber.reporting.types.result.CucumberScenarioResult;

public class CucumberResultsOverview extends CucumberResultsCommon {
    private String outputDirectory;
    private String outputName;

    /**
     * @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;
    }

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

    protected String getFeatureData(CucumberFeatureResult[] results) {
        int passed = 0;
        int failed = 0;
        int undefined = 0;

        for (CucumberFeatureResult result : results) {
            if (result.getStatus().trim().equalsIgnoreCase("passed")) {
                passed++;
            }
            if (result.getStatus().trim().equalsIgnoreCase("failed")) {
                failed++;
            }
            if (result.getStatus().trim().equalsIgnoreCase("undefined")
                    || result.getStatus().trim().equalsIgnoreCase("skipped")) {
                undefined++;
            }
        }
        return String.format("['Passed', %d], ['Failed', %d], ['Undefined', %d]", passed, failed, undefined);
    }

    protected String getScenarioData(CucumberFeatureResult[] results) {
        int passed = 0;
        int failed = 0;
        int undefined = 0;

        for (CucumberFeatureResult result : results) {
            for (CucumberScenarioResult element : result.getElements()) {
                if (element.getStatus().trim().equalsIgnoreCase("passed")) {
                    passed++;
                }
                if (element.getStatus().trim().equalsIgnoreCase("failed")) {
                    failed++;
                }
                if (element.getStatus().trim().equalsIgnoreCase("undefined")
                        || element.getStatus().trim().equalsIgnoreCase("skipped")) {
                    undefined++;
                }
            }
        }

        return String.format("['Passed', %d], ['Failed', %d], ['Undefined', %d]", passed, failed, undefined);
    }

    protected String generateFeatureOverview(CucumberFeatureResult[] results) throws IOException {
        String content = this.getReportBase();
        content = content.replaceAll("__TITLE__", "Features Overview");
        String reportContent = "";

        reportContent += "

Features Status

" + ""; for (CucumberFeatureResult result : results) { reportContent += String.format( "", result.getStatus(), result.getName(), result.getStatus(), result.getPassed(), result.getFailed(), result.getUndefined() + result.getSkipped(), result.getDuration()); } reportContent += "
Feature NameStatusPassedFailedUndefinedDuration
%s%s%d%d%d%.2fs
"; reportContent += "

Scenario Status

" + "" + "" + "" + "" + "" + "" + "" + ""; for (CucumberFeatureResult result : results) { for (CucumberScenarioResult element : result.getElements()) { reportContent += String.format( "" + "" + "" + "", element.getStatus(), result.getName(), element.getName(), element.getStatus(), element.getPassed(), element.getFailed(), element.getUndefined() + element.getSkipped(), element.getRerunAttempts(), element.getDuration()); } } reportContent += "
Feature NameScenarioStatusPassedFailedUndefinedRetriesDuration
%s%s%s%d%d%d%d%.2fs
"; content = content.replaceAll("__REPORT__", reportContent); content = content.replaceAll("__FEATURE_DATA__", getFeatureData(results)); content = content.replaceAll("__SCENARIO_DATA__", getScenarioData(results)); return content; } public void executeOverviewReport(String reportSuffix) throws Exception { CucumberFeatureResult[] features = readFileContent(true); File outFile = new File( this.getOutputDirectory() + File.separator + this.getOutputName() + "-" + reportSuffix + ".html"); FileUtils.writeStringToFile(outFile, generateFeatureOverview(features)); final WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_11); webClient.getOptions().setThrowExceptionOnScriptError(false); final HtmlPage page = webClient.getPage(new URL("file://" + outFile.getAbsolutePath()).toExternalForm()); String content = page.asXml(); FileUtils.writeStringToFile(outFile, content); webClient.close(); } public void executeFeaturesOverviewReport() throws Exception { executeOverviewReport("feature-overview"); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy