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

net.serenitybdd.cucumber.events.StepFinishedWithResultEvent Maven / Gradle / Ivy

There is a newer version: 4.2.8
Show newest version
package net.serenitybdd.cucumber.events;

import io.cucumber.core.plugin.SerenityReporterParallel;
import io.cucumber.plugin.event.*;
import net.thucydides.model.domain.TestOutcome;
import net.thucydides.model.domain.stacktrace.RootCauseAnalyzer;
import net.thucydides.model.screenshots.ScreenshotAndHtmlSource;
import net.thucydides.model.steps.ExecutedStepDescription;
import net.thucydides.model.steps.StepFailure;
import net.thucydides.core.steps.events.StepEventBusEventBase;

import java.time.ZonedDateTime;
import java.util.*;

import static org.apache.commons.lang3.StringUtils.isEmpty;

public class StepFinishedWithResultEvent extends StepEventBusEventBase {

    private static final String OPEN_PARAM_CHAR = "\uff5f";
    private static final String CLOSE_PARAM_CHAR = "\uff60";


	private final Result result;
	private final io.cucumber.messages.types.Step currentStep;
	private final TestStep currentTestStep;
    private final List screenshotList;
    boolean isInDataDrivenTest;

	public StepFinishedWithResultEvent(Result result,
                                       io.cucumber.messages.types.Step currentStep,
                                       TestStep currentTestStep,
                                       List screenshotList,
                                       ZonedDateTime time,
                                       boolean isInDataDrivenTest) {
		this.result = result;
		this.currentStep = currentStep;
		this.currentTestStep =  currentTestStep;
		this.screenshotList =  screenshotList;
        this.timestamp = time;
        this.isInDataDrivenTest = isInDataDrivenTest;
	}

	@Override
	public void play() {
 		if (getStepEventBus().currentTestIsSuspended()) {
            getStepEventBus().stepIgnored();
        } else if (Status.PASSED.equals(result.getStatus())) {
            getStepEventBus().stepFinished(screenshotList, getTimestamp());
        } else if (Status.FAILED.equals(result.getStatus())) {
            failed(SerenityReporterParallel.stepTitleFrom(currentStep, currentTestStep), result.getError(), screenshotList);
        } else if (Status.SKIPPED.equals(result.getStatus())) {
            skipped(SerenityReporterParallel.stepTitleFrom(currentStep, currentTestStep), result.getError());
        } else if (Status.PENDING.equals(result.getStatus())) {
            getStepEventBus().stepPending();
        } else if (Status.UNDEFINED.equals(result.getStatus())) {
            getStepEventBus().stepPending();
        }
	}


    private void failed(String stepTitle, Throwable cause, List screenshots) {
        if (!errorOrFailureRecordedForStep(stepTitle, cause)) {
            if (!isEmpty(stepTitle)) {
                getStepEventBus().updateCurrentStepTitle(stepTitle);
            }
            Throwable rootCause = new RootCauseAnalyzer(cause).getRootCause().toException();
            if (isAssumptionFailure(rootCause)) {
                getStepEventBus().assumptionViolated(rootCause.getMessage());
            } else {
                StepFailure stepFailure = new StepFailure(ExecutedStepDescription.withTitle(normalized(currentStepTitle())), rootCause);
                getStepEventBus().stepFailed(stepFailure, screenshots, isInDataDrivenTest);
            }
        }
    }

    private void skipped(String stepTitle, Throwable cause) {
        if (!errorOrFailureRecordedForStep(stepTitle, cause)) {
            if (!isEmpty(stepTitle)) {
                getStepEventBus().updateCurrentStepTitle(stepTitle);
            }
            if (cause == null) {
                getStepEventBus().stepIgnored();
            } else {
                Throwable rootCause = new RootCauseAnalyzer(cause).getRootCause().toException();
                if (isAssumptionFailure(rootCause)) {
                    getStepEventBus().assumptionViolated(rootCause.getMessage());
                } else {
                    getStepEventBus().stepIgnored();
                }
            }
        }
    }

	private boolean errorOrFailureRecordedForStep(String stepTitle, Throwable cause) {
        if (!latestTestOutcome().isPresent()) {
            return false;
        }
        if (!latestTestOutcome().get().testStepWithDescription(stepTitle).isPresent()) {
            return false;
        }
        Optional matchingTestStep = latestTestOutcome().get().testStepWithDescription(stepTitle);
        if (matchingTestStep.isPresent() && matchingTestStep.get().getNestedException() != null) {
            return (matchingTestStep.get().getNestedException().getOriginalCause() == cause);
        }

        return false;
    }

	private Optional latestTestOutcome() {

        if (!getStepEventBus().isBaseStepListenerRegistered()) {
            return Optional.empty();
        }

        List recordedOutcomes = getStepEventBus().getBaseStepListener().getTestOutcomes();
        return (recordedOutcomes.isEmpty()) ? Optional.empty()
                : Optional.of(recordedOutcomes.get(recordedOutcomes.size() - 1));
    }

	private boolean isAssumptionFailure(Throwable rootCause) {
        try {
            // Load the AssumptionViolatedException class using its name
            Class clazz = Class.forName("org.junit.AssumptionViolatedException");

            // Check if the rootCause is an instance of the AssumptionViolatedException class
            return clazz.isAssignableFrom(rootCause.getClass());
        } catch (ClassNotFoundException e) {
            // AssumptionViolatedException class is not found on the classpath
            return false;
        }
        //return (AssumptionViolatedException.class.isAssignableFrom(rootCause.getClass()));
    }


    private String normalized(String value) {
        return value.replaceAll(OPEN_PARAM_CHAR, "{").replaceAll(CLOSE_PARAM_CHAR, "}");
    }

	private String currentStepTitle() {
        return getStepEventBus().getCurrentStep().isPresent()
                ? getStepEventBus().getCurrentStep().get().getDescription() : "";
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy