
net.serenitybdd.screenplay.QuestionConsequence Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of serenity-screenplay Show documentation
Show all versions of serenity-screenplay Show documentation
Support for the User Journey pattern in Serenity
package net.serenitybdd.screenplay;
import net.serenitybdd.core.eventbus.Broadcaster;
import net.serenitybdd.screenplay.events.ActorAsksQuestion;
import net.serenitybdd.screenplay.formatting.StripRedundantTerms;
import net.thucydides.core.steps.StepEventBus;
import org.hamcrest.Matcher;
import static org.hamcrest.MatcherAssert.assertThat;
public class QuestionConsequence extends BaseConsequence {
protected final Question question;
protected final Matcher expected;
protected final String subject;
private final static SilentPerformable DO_NOTHING = new SilentPerformable();
public QuestionConsequence(Question actual, Matcher expected) {
this.question = actual;
this.expected = expected;
this.subject = QuestionSubject.fromClass(actual.getClass()).andQuestion(actual).subject();
}
@Override
public void evaluateFor(Actor actor) {
if (thisStepShouldBeIgnored() && !StepEventBus.getEventBus().softAssertsActive()) { return; }
Broadcaster.getEventBus().post(new ActorAsksQuestion(question));
try {
optionalPrecondition.or(DO_NOTHING).performAs(actor);
assertThat(question.answeredBy(actor), expected);
} catch (Throwable actualError) {
throwComplaintTypeErrorIfSpecified(errorFrom(actualError));
throwDiagosticErrorIfProvided(errorFrom(actualError));
throw actualError;
}
}
private void throwDiagosticErrorIfProvided(Error actualError) {
if (question instanceof QuestionDiagnostics) {
throw Complaint.from(((QuestionDiagnostics) question).onError(), actualError);
}
}
@Override
public String toString() {
String template = explanation.or("Then %s should be %s");
String expectedExpression = StripRedundantTerms.from(expected.toString());
return addRecordedInputValuesTo(String.format(template, subject, expectedExpression));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy