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

net.serenitybdd.screenplay.BaseConsequence Maven / Gradle / Ivy

There is a newer version: 4.2.9
Show newest version
package net.serenitybdd.screenplay;

import net.thucydides.core.steps.StepEventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static java.util.Arrays.asList;
import static net.serenitybdd.screenplay.Actor.ErrorHandlingMode.IGNORE_EXCEPTIONS;

public abstract class BaseConsequence implements Consequence {

    private Class complaintType;
    private String complaintDetails;
    protected Optional explanation = Optional.empty();
    protected Optional subjectText = Optional.empty();
    private List setupActions = new ArrayList<>();

    protected Error errorFrom(Throwable actualError) {
        if (actualError instanceof AssertionError) {
            return null;
        } else if (actualError instanceof Error) {
            return (Error) actualError;
        } else {
            return null;
        }
//        return new Error(actualError);
    }

    protected void throwComplaintTypeErrorIfSpecified(Throwable actualError) {
        if (complaintType != null) {
            throw Complaint.from(complaintType, complaintDetails, actualError);
        }
    }

    protected boolean thisStepShouldBeIgnored() {
        return (StepEventBus.getParallelEventBus().currentTestIsSuspended() || StepEventBus.getParallelEventBus().aStepInTheCurrentTestHasFailed());
    }

    @Override
    public BaseConsequence orComplainWith(Class complaintType) {
        return orComplainWith(complaintType, null);
    }

    @Override
    public BaseConsequence orComplainWith(Class complaintType, String complaintDetails) {
        this.complaintType = complaintType;
        this.complaintDetails = complaintDetails;
        return this;
    }

    @Override
    public Consequence whenAttemptingTo(Performable performable) {
        setupActions.add(performable);
        return this;
    }
    @Override
    public Consequence because(String explanation) {
        this.explanation = Optional.ofNullable(explanation);
        return this;
    }

    protected String inputValues() {
        return setupActions.stream()
                .filter(action -> action instanceof RecordsInputs)
                .map(action -> (RecordsInputs) action)
                .map(RecordsInputs::getInputValues)
                .collect(Collectors.joining(","));
    }

    protected String addRecordedInputValuesTo(String message) {
        if (inputValues().isEmpty()) {
            return message;
        }
        return message + " [" + inputValues() + "]";
    }

    public Consequence after(Performable... actions) {
        this.setupActions.addAll(asList(actions));
        return this;
    }

    protected void performSetupActionsAs(Actor actor) {
        actor.attemptsTo(IGNORE_EXCEPTIONS, setupActions.toArray(new Performable[]{}));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy