net.serenitybdd.screenplay.questions.UIStateReaderBuilder Maven / Gradle / Ivy
package net.serenitybdd.screenplay.questions;
import net.serenitybdd.screenplay.Actor;
import net.serenitybdd.screenplay.EnumValues;
import net.serenitybdd.screenplay.Question;
import net.serenitybdd.screenplay.targets.Target;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
@Deprecated
public class UIStateReaderBuilder{
private final Target target;
private final Class type;
private String subject;
private final Optional optionalParameter;
private Logger logger = LoggerFactory.getLogger(this.getClass());
public UIStateReaderBuilder(Target target, Class type) {
this.target = target;
this.type = type;
this.subject = target.getName();
this.optionalParameter = Optional.empty();
}
public UIStateReaderBuilder(Target target, Class type, Optional optionalParameter) {
this.target = target;
this.type = type;
this.subject = target.getName();
this.optionalParameter = optionalParameter;
}
public UIStateReaderBuilder describedAs(String subject) {
this.subject = subject;
return this;
}
/**
* Convert the value returned by a question to an arbitrary type
* @return
*/
public Question map(Function transformer) {
return Question.about(subject).answeredBy(
actor -> transformer.apply(asAString().answeredBy(actor))
);
}
/**
* A convenience method to return a question about a target
* e.g. Text.of(VetList.VET_NAME).asAString()
*/
public Question asAString() {
return Question.about(subject).answeredBy(actor -> ((TargetedUIState) viewedBy(actor)).asString());
}
public Question asADate() {
return Question.about(subject).answeredBy(actor -> ((TargetedUIState) viewedBy(actor)).asLocalDate());
}
public Question asADate(String format) {
return Question.about(subject).answeredBy(actor -> ((TargetedUIState) viewedBy(actor)).asLocalDate(format));
}
public Question asABigDecimal() {
return Question.about(subject).answeredBy(actor -> ((TargetedUIState) viewedBy(actor)).asBigDecimal());
}
public Question asABoolean() {
return Question.about(subject).answeredBy(actor -> ((TargetedUIState) viewedBy(actor)).asBoolean());
}
public Question asDouble() {
return Question.about(subject).answeredBy(actor -> ((TargetedUIState) viewedBy(actor)).asDouble());
}
public Question asFloat() {
return Question.about(subject).answeredBy(actor -> ((TargetedUIState) viewedBy(actor)).asFloat());
}
public Question asLong() {
return Question.about(subject).answeredBy(actor -> ((TargetedUIState) viewedBy(actor)).asLong());
}
public Question asInteger() {
return Question.about(subject).answeredBy(actor -> ((TargetedUIState) viewedBy(actor)).asInteger());
}
public Question asEnum(Class enumType) {
return Question.about(subject)
.answeredBy(
actor -> EnumValues.forType(enumType).getValueOf(((TargetedUIState) viewedBy(actor)).asString()));
}
/**
* A convenience method to return a question about a target
* e.g. Text.of(VetList.VET_NAME).asACollection()
*/
public Question> asACollection() {
return Question.about(subject).answeredBy(actor -> ((TargetedUIState) viewedBy(actor)).asList());
}
public Question> asACollectionOf(Class type) {
return Question.about(subject).answeredBy(actor -> ((TargetedUIState) viewedBy(actor)).asListOf(type));
}
public Question> asAList() {
return Question.about(subject).answeredBy(actor -> ((TargetedUIState) viewedBy(actor)).asList());
}
public Question> asAListOf(Class type) {
return Question.about(subject).answeredBy(actor -> ((TargetedUIState) viewedBy(actor)).asListOf(type));
}
public T viewedBy(Actor actor) {
try {
if (optionalParameter.isPresent()) {
return (T) type.getConstructor(Target.class, Actor.class, String.class).newInstance(target, actor, optionalParameter.get());
} else {
return (T) type.getConstructor(Target.class, Actor.class).newInstance(target, actor);
}
} catch (Exception e) {
logger.error("Failed to instantiate UIStateReader of type " + type, e);
throw new IllegalStateException("Failed to instantiate UIStateReader of type " + type, e);
}
}
}