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

net.masterthought.cucumber.ReportInformation Maven / Gradle / Ivy

Go to download

Provides pretty html reports for Cucumber. It works by generating html from the cucumber json file.

There is a newer version: 5.8.2
Show newest version
package net.masterthought.cucumber;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.masterthought.cucumber.json.Artifact;
import net.masterthought.cucumber.json.Element;
import net.masterthought.cucumber.json.Feature;
import net.masterthought.cucumber.json.Step;
import net.masterthought.cucumber.util.Status;
import net.masterthought.cucumber.util.StatusCounter;
import net.masterthought.cucumber.util.Util;

import com.googlecode.totallylazy.Sequence;

public class ReportInformation {

    private final Map> projectFeatureMap;
    private List features;
    private int numberOfScenarios;
    private int numberOfSteps;
    private final StatusCounter totalSteps = new StatusCounter();
    private final StatusCounter totalBackgroundSteps = new StatusCounter();

    private Long totalDuration = 0l;
    List tagMap = new ArrayList<>();
    private int totalTagScenarios = 0;
    private int totalTagSteps = 0;
    private final StatusCounter totalTags = new StatusCounter();
    
    private long totalTagDuration = 0l;
    private int totalPassingTagScenarios = 0;
    private int totalFailingTagScenarios = 0;
    private Background backgroundInfo = new Background();

    public ReportInformation(Map> projectFeatureMap) {
        this.projectFeatureMap = projectFeatureMap;
        this.features = listAllFeatures();
        processFeatures();
    }

    private List listAllFeatures() {
        List allFeatures = new ArrayList();
        for (Map.Entry> pairs : projectFeatureMap.entrySet()) {
            List featureList = pairs.getValue();
            allFeatures.addAll(featureList);
        }
        return allFeatures;
    }

    public List getFeatures() {
        return this.features;
    }

    public List getTags() {
        return this.tagMap;
    }

    public Map> getProjectFeatureMap() {
        return this.projectFeatureMap;
    }

    public int getTotalScenarios() {
        return numberOfScenarios;
    }

    public int getTotalFeatures() {
        return features.size();
    }

    public int getTotalSteps() {
        return numberOfSteps;
    }

    public int getTotalStepsPassed() {
        return totalSteps.getValueFor(Status.PASSED);
    }

    public int getTotalStepsFailed() {
        return totalSteps.getValueFor(Status.FAILED);
    }

    public int getTotalStepsSkipped() {
        return totalSteps.getValueFor(Status.SKIPPED);
    }

    public int getTotalStepsPending() {
        return totalSteps.getValueFor(Status.PENDING);
    }

    public int getTotalStepsMissing() {
        return totalSteps.getValueFor(Status.MISSING);
    }

    public int getTotalStepsUndefined() {
        return totalSteps.getValueFor(Status.UNDEFINED);
    }

    public String getTotalDurationAsString() {
        return Util.formatDuration(totalDuration);
    }

    public Long getTotalDuration() {
        return totalDuration;
    }

    public String timeStamp() {
        return new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(new Date());
    }

    public String getReportStatusColour(Feature feature) {
        return feature.getStatus() == Status.PASSED ? Status.PASSED.color : Status.FAILED.color;
    }

    public String getTagReportStatusColour(TagObject tag) {
        return tag.getStatus() == Status.PASSED ? Status.PASSED.color : Status.FAILED.color;
    }

    public int getTotalTags() {
        return tagMap.size();
    }

    public int getTotalTagScenarios() {
        return totalTagScenarios;
    }

    public int getTotalTagScenariosPassed() {
        return totalPassingTagScenarios;
    }

    public int getTotalTagScenariosFailed() {
        return totalFailingTagScenarios;
    }

    public int getTotalTagSteps() {
        return totalTagSteps;
    }

    public int getTotalTagPasses() {
        return totalTags.getValueFor(Status.PASSED);
    }

    public int getTotalTagFails() {
        return totalTags.getValueFor(Status.FAILED);
    }

    public int getTotalTagSkipped() {
        return totalTags.getValueFor(Status.SKIPPED);
    }

    public int getTotalTagPending() {
        return totalTags.getValueFor(Status.PENDING);
    }

    public int getTotalTagUndefined() {
        return totalTags.getValueFor(Status.UNDEFINED);
    }

    public int getTotalTagMissing() {
        return totalTags.getValueFor(Status.MISSING);
    }

    public String getTotalTagDuration() {
        return Util.formatDuration(totalTagDuration);
    }

    public long getLongTotalTagDuration() {
        return totalTagDuration;
    }

    public int getTotalScenariosPassed() {
        return this.totalBackgroundSteps.getValueFor(Status.PASSED);
    }

    public int getTotalScenariosFailed() {
        return this.totalBackgroundSteps.getValueFor(Status.FAILED);
    }

    private void processTags() {
        for (TagObject tag : tagMap) {
            totalTagScenarios = calculateTotalTagScenarios(tag);
            totalPassingTagScenarios = calculateTotalTagScenariosForStatus(totalPassingTagScenarios, tag, Status.PASSED);
            totalFailingTagScenarios = calculateTotalTagScenariosForStatus(totalFailingTagScenarios, tag, Status.FAILED);
            for (Status status : Status.values()) {
                this.totalTags.incrementFor(status, tag.getNumberOfStatus(status));
            }

            for (ScenarioTag scenarioTag : tag.getScenarios()) {
                if (scenarioTag.hasSteps()) {
                    Sequence steps = scenarioTag.getScenario().getSteps();
                    List stepList = new ArrayList();
                    for (Step step : steps) {
                        stepList.add(step);
                        totalTagDuration = totalTagDuration + step.getDuration();
                    }
                    totalTagSteps += stepList.size();
                }
            }
        }
    }

    private int calculateTotalTagScenariosForStatus(int totalScenarios, TagObject tag, Status status) {
        List scenarioTagList = new ArrayList();
        for (ScenarioTag scenarioTag : tag.getScenarios()) {
            if (!scenarioTag.getScenario().isBackground()) {
                if (scenarioTag.getScenario().getStatus().equals(status)) {
                    scenarioTagList.add(scenarioTag);
                }
            }
        }
        return totalScenarios + scenarioTagList.size();
    }

    private int calculateTotalTagScenarios(TagObject tag) {
        List scenarioTagList = new ArrayList();
        for (ScenarioTag scenarioTag : tag.getScenarios()) {
            if (!scenarioTag.getScenario().isBackground()) {
                scenarioTagList.add(scenarioTag);
            }
        }
        return totalTagScenarios + scenarioTagList.size();
    }

    private void processFeatures() {
        for (Feature feature : features) {
            List scenarioList = new ArrayList();
            Sequence scenarios = feature.getElements();
            if (Util.itemExists(scenarios)) {
                numberOfScenarios = getNumberOfScenarios(scenarios);
                //process tags
                if (feature.hasTags()) {
                    for (Element e : feature.getElements()) {
                        if (!e.isBackground()) {
                            scenarioList.add(new ScenarioTag(e, feature.getFileName()));
                        }
                    }
                    tagMap = createOrAppendToTagMapByFeature(tagMap, feature.getTagList(), scenarioList);

                }

                for (Element scenario : scenarios) {

                    if (!scenario.isBackground()) {
                        totalBackgroundSteps.incrementFor(scenario.getStatus());
                    } else {
                        setBackgroundInfo(scenario);
                    }

                    if (feature.hasScenarios()) {
                        if (scenario.hasTags()) {
                            scenarioList = addScenarioUnlessExists(scenarioList, new ScenarioTag(scenario, feature.getFileName()));
                            tagMap = createOrAppendToTagMap(tagMap, scenario.getTagList(), scenarioList);
                        }

                    }
                    adjustStepsForScenario(scenario);
                }
            }
        }
        processTags();
    }

    private void setBackgroundInfo(Element e) {
        backgroundInfo.addTotalScenarios(1);
        if (e.getStatus() == Status.PASSED) {
            backgroundInfo.addTotalScenariosPassed(1);
        } else {
            backgroundInfo.addTotalScenariosFailed(1);
        }
        backgroundInfo.addTotalSteps(e.getSteps().size());
        for (Step step : e.getSteps().toList()) {
            backgroundInfo.incrTotalDurationBy(step.getDuration());
            backgroundInfo.incrStepCounterForStatus(step.getStatus());
        }

    }

    private void adjustStepsForScenario(Element element) {
        String scenarioName = element.getRawName();
        if (element.hasSteps()) {
            Sequence steps = element.getSteps();
            numberOfSteps = numberOfSteps + steps.size();
            for (Step step : steps) {
                String stepName = step.getRawName();

                //apply artifacts
                ConfigurationOptions configuration = ConfigurationOptions.instance();
                if (configuration.artifactsEnabled()) {
                    Map map = configuration.artifactConfig();
                    String mapKey = scenarioName + stepName;
                    if (map.containsKey(mapKey)) {
                        Artifact artifact = map.get(mapKey);
                        String keyword = artifact.getKeyword();
                        String contentType = artifact.getContentType();
                        step.setName(stepName.replaceFirst(keyword, getArtifactFile(mapKey, keyword, artifact.getArtifactFile(), contentType)));
                    }
                }

                this.totalSteps.incrementFor(step.getStatus());
                totalDuration = totalDuration + step.getDuration();
            }
        }
    }

    private int getNumberOfScenarios(Sequence scenarios) {
        List scenarioList = new ArrayList<>();
        for (Element scenario : scenarios) {
            if (!scenario.isBackground()) {
                scenarioList.add(scenario);
            }
        }
        return numberOfScenarios + scenarioList.size();
    }

    private String getArtifactFile(String mapKey, String keyword, String artifactFile, String contentType) {
        mapKey = mapKey.replaceAll(" ", "_");
        String link = "";
        if (contentType.equals("xml")) {
            link = "
" + keyword + ""; } else { link = "
" + keyword + ""; } return link; } private List addScenarioUnlessExists(List scenarioList, ScenarioTag scenarioTag) { boolean exists = false; for (ScenarioTag scenario : scenarioList) { if (scenario.getParentFeatureUri().equalsIgnoreCase(scenarioTag.getParentFeatureUri()) && scenario.getScenario().getName().equalsIgnoreCase(scenarioTag.getScenario().getName())) { exists = true; break; } } if (!exists) { scenarioList.add(scenarioTag); } return scenarioList; } // private List addScenarioUnlessExists(List scenarioList, ScenarioTag scenarioTag) { // // Sequence listOfScenarios = Sequences.sequence(scenarioList).realise(); // Sequence results = listOfScenarios.filter(ScenarioTag.predicates.scenarioExists(scenarioTag.getParentFeatureUri(),scenarioTag.getScenario().getName())); // List scenarioTags = results.toList(); // for(ScenarioTag scenario : scenarioTags){ // scenarioList.add(scenario); // } // // scenarioList.add(scenarioTag); // // List scenariosForList = new ArrayList(); // for (ScenarioTag scenario : scenarioList) { // // if (scenario.getParentFeatureUri().equalsIgnoreCase(scenarioTag.getParentFeatureUri()) // && scenario.getScenario().getName().equalsIgnoreCase(scenarioTag.getScenario().getName())) { // if(scenarioTag.getScenario().getKeyword().equalsIgnoreCase("Scenario Outline")){ // scenariosForList.add(scenarioTag); // } // } else { // scenariosForList.add(scenarioTag); // } // } // //// if(!scenariosForList.isEmpty()){ // scenarioList.addAll(scenariosForList); //// } // // return scenarioList; // } private List createOrAppendToTagMap(List tagMap, Sequence tagList, List scenarioList) { for (String tag : tagList) { boolean exists = false; TagObject tagObj = null; for (TagObject tagObject : tagMap) { if (tagObject.getTagName().equalsIgnoreCase(tag)) { exists = true; tagObj = tagObject; break; } } if (exists) { List existingTagList = tagObj.getScenarios(); for (ScenarioTag scenarioTag : scenarioList) { if (scenarioTag.getScenario().getTagList().contains(tag)) { existingTagList = addScenarioUnlessExists(existingTagList, scenarioTag); } } tagMap.remove(tagObj); tagObj.setScenarios(existingTagList); tagMap.add(tagObj); } else { List existingTagList = new ArrayList(); for (ScenarioTag scenarioTag : scenarioList) { if (scenarioTag.getScenario().getTagList().contains(tag)) { existingTagList = addScenarioUnlessExists(existingTagList, scenarioTag); } } tagObj = new TagObject(tag, existingTagList); tagMap.add(tagObj); } } return tagMap; } public List createOrAppendToTagMapByFeature(List tagMap, Sequence tagList, List scenarioList) { for (String tag : tagList) { boolean exists = false; TagObject tagObj = null; for (TagObject tagObject : tagMap) { if (tagObject.getTagName().equalsIgnoreCase(tag)) { exists = true; tagObj = tagObject; break; } } if (exists) { List existingTagList = tagObj.getScenarios(); List all = new ArrayList(); all.addAll(existingTagList); all.addAll(scenarioList); tagMap.remove(tagObj); tagObj.setScenarios(all); tagMap.add(tagObj); } else { tagObj = new TagObject(tag, scenarioList); tagMap.add(tagObj); } } return tagMap; } public Background getBackgroundInfo() { return backgroundInfo; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy