
io.guixer.logs.YamlLogLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of guixer-logs Show documentation
Show all versions of guixer-logs Show documentation
Log analysis utilities, for Guixer.
The newest version!
package io.guixer.logs;
import static io.guixer.logs.StepTypes.toStepType;
import static net.avcompris.commons3.databeans.DataBeans.instantiate;
import static org.apache.commons.lang3.StringUtils.substringAfterLast;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import javax.annotation.Nullable;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.NotImplementedException;
import org.joda.time.DateTime;
import com.avcompris.util.SnakeYAMLUtils;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import io.guixer.logs.ElaborateLog.Intent;
import io.guixer.logs.ElaborateLog.Locator;
import io.guixer.logs.ElaborateLog.LogError;
import io.guixer.logs.ElaborateLog.Step;
import io.guixer.logs.ElaborateLogUtils.MutableAttribute;
import io.guixer.logs.ElaborateLogUtils.MutableBefore;
import io.guixer.logs.ElaborateLogUtils.MutableDone;
import io.guixer.logs.ElaborateLogUtils.MutableGroup;
import io.guixer.logs.ElaborateLogUtils.MutableIntent;
import io.guixer.logs.ElaborateLogUtils.MutableLocator;
import io.guixer.logs.ElaborateLogUtils.MutableLogError;
import io.guixer.logs.ElaborateLogUtils.MutableScreenshot;
import io.guixer.logs.ElaborateLogUtils.MutableStatus;
import io.guixer.logs.ElaborateLogUtils.MutableStep;
import io.guixer.types.AttributeScope;
import io.guixer.types.LocatorType;
import io.guixer.types.ResultType;
import io.guixer.types.StepType;
import net.avcompris.commons3.yaml.Yaml;
public class YamlLogLoader {
private final ElaborateLog log;
public YamlLogLoader(
final File yamlLogFile
) throws IOException {
if (!yamlLogFile.isFile()) {
throw new FileNotFoundException("YAML file: " + yamlLogFile.getCanonicalPath());
}
final byte[] bytes = FileUtils.readFileToByteArray(yamlLogFile);
final Yaml yaml = // YamlUtils.loadYaml(yamlLogFile);
GuixerYamlUtils.bindSnakeYaml(SnakeYAMLUtils.loadYAML(yamlLogFile));
final String guixerVersion = yaml.get("guixerVersion").asString();
final String dateAsString = yaml.get("date").toString();
// final DateTime dateTime=DateTime.parse(dateAsString);
final long timeMillis = yaml.get("timeMillis").asLong();
final String testMethodName = yaml.get("testMethodName").asString();
final String scenario = yaml.get("scenario").asString();
final String testClassName;
final String testClassSimpleName;
if (yaml.has("testClassName")) {
testClassName = yaml.get("testClassName").asString();
if (yaml.has("testClassSimpleName")) {
testClassSimpleName = yaml.get("testClassSimpleName").asString();
} else if (testClassName.contains(".")) {
testClassSimpleName = substringAfterLast(testClassName, ".");
} else {
testClassSimpleName = testClassName;
}
} else if (yaml.has("testClassSimpleName")) {
testClassSimpleName = yaml.get("testClassSimpleName").asString();
testClassName = testClassSimpleName;
} else {
throw new RuntimeException("testClassName and/or testClassSimpleName should be set.");
}
@Nullable
final LogError error;
if (yaml.has("error")) {
final Yaml errorYaml = yaml.get("error");
final Yaml screenshotYaml = errorYaml.get("screenshot");
error = instantiate(MutableLogError.class) //
.setTimeMillis(errorYaml.get("timeMillis").asLong()) //
.setTrace(errorYaml.get("trace").asString()) //
.setScreenshot(instantiate(MutableScreenshot.class) //
.setTimeMillis(screenshotYaml.get("timeMillis").asLong()) //
.setFileName(screenshotYaml.get("fileName").asString()));
} else {
error = null;
}
final MutableElaborateLog log = instantiate(MutableElaborateLog.class) //
.setSuccessCount(0) //
.setFailureCount(0) //
.setInError(error != null) //
.setError(error) //
.setRawBytes(bytes) //
.setGuixerVersion(guixerVersion) //
.setDateAsString(dateAsString) //
.setTimeMillis(timeMillis) //
.setTestClassName(testClassName) //
.setTestClassSimpleName(testClassSimpleName) //
.setTestMethodName(testMethodName) //
.setScenario(scenario);
this.log = log;
if (yaml.has("attributes")) {
for (final Yaml attributeYaml : yaml.get("attributes").items()) {
log.addToAttributes(instantiate(MutableAttribute.class) //
.setScope(toAttributeScope(attributeYaml.get("scope").asString())) //
.setName(attributeYaml.get("name").asString()) //
.setValue(attributeYaml.has("value") //
? attributeYaml.get("value").asString() //
: null));
}
}
if (yaml.has("before")) {
final MutableBefore before = instantiate(MutableBefore.class);
log.setBefore(before);
for (final Yaml stepYaml : yaml.get("before").items()) {
before.addToSteps(loadStep(stepYaml));
}
}
if (yaml.has("done")) {
final Yaml doneYaml = yaml.get("done");
log.setDone(instantiate(MutableDone.class) //
.setTimeMillis(doneYaml.get("timeMillis").asLong()) //
.setMessage(doneYaml.get("message").asString()));
}
log.setIntents(getIntents(yaml));
@Nullable
final Integer refSuccessCount = yaml.has("successCount") //
// ? Integer.parseInt(yaml.get("successCount").asString()) //
? yaml.get("successCount").asInt() //
: null;
@Nullable
final Integer refFailureCount = yaml.has("failureCount") //
// ? Integer.parseInt(yaml.get("failureCount").asString()) //
? yaml.get("failureCount").asInt() //
: null;
computeSuccessFailures(log, log.getIntents());
if (refFailureCount != null && log.getFailureCount() != refFailureCount) {
throw new IllegalStateException(
"failureCount: Expected: " + refFailureCount + ", but was: " + log.getFailureCount());
}
if (refSuccessCount != null && log.getSuccessCount() != refSuccessCount) {
throw new IllegalStateException(
"successCount: Expected: " + refSuccessCount + ", but was: " + log.getSuccessCount());
}
}
private static void computeSuccessFailures(
final MutableElaborateLog log,
final Intent[] intents
) {
for (final Intent intent : intents) {
if (intent.getGroup() != null) {
computeSuccessFailures(log, intent.getGroup().getIntents());
} else {
for (final Step step : intent.getSteps()) {
@Nullable
final ResultType resultType = step.getResultType();
if (resultType == ResultType.FAILURE) {
log.setFailureCount(log.getFailureCount() + 1);
} else if (resultType == ResultType.SUCCESS) {
log.setSuccessCount(log.getSuccessCount() + 1);
}
}
}
}
}
public ElaborateLog getLog() {
return log;
}
private static Intent[] getIntents(
final Yaml yaml
) {
final List intents = Lists.newArrayList();
if (yaml.has("intents")) {
for (final Yaml intentYaml : yaml.get("intents").items()) {
final MutableIntent intent;
if (intentYaml.has("group")) {
final String groupName = intentYaml.get("group").asString();
final long beginAtMs = intentYaml.get("beginAtMs").asLong();
@Nullable
final Long endAtMs = intentYaml.has("endAtMs") //
? intentYaml.get("endAtMs").asLong() //
: null;
intent = instantiate(MutableIntent.class) //
.setTimeMillis(beginAtMs) //
.setTitle(groupName) //
.setGroup(instantiate(MutableGroup.class) //
.setName(groupName) //
.setBeginAtMs(beginAtMs) //
.setEndAtMs(endAtMs) //
.setIntents(getIntents(intentYaml)));
} else if (intentYaml.has("status")) {
final String label = intentYaml.get("status").asString();
final long statusAtMs = intentYaml.get("statusAtMs").asLong();
intent = instantiate(MutableIntent.class) //
.setTimeMillis(statusAtMs) //
.setTitle(label) //
.setStatus(instantiate(MutableStatus.class) //
.setLabel(label) //
.setStatusAtMs(statusAtMs));
} else {
final long timeMillis = intentYaml.get("timeMillis").asLong();
final String title = intentYaml.get("title").asString();
intent = instantiate(MutableIntent.class) //
.setTimeMillis(timeMillis) //
.setTitle(title);
if (intentYaml.has("steps")) {
for (final Yaml stepYaml : intentYaml.get("steps").items()) {
intent.addToSteps(loadStep(stepYaml));
}
}
}
intents.add(intent);
}
}
return Iterables.toArray(intents, Intent.class);
}
private static Step loadStep(
final Yaml yaml
) {
final long timeMillis = yaml.get("timeMillis").asLong();
final String typeAsString = yaml.get("type").asString();
final StepType type = toStepType(typeAsString);
final MutableStep step = instantiate(MutableStep.class) //
.setTimeMillis(timeMillis) //
.setType(type);
switch (type) {
case ASSERT_ABSENT:
step.setLocator(loadLocator(yaml.get("locator"))) //
.setResultType(toResultType(yaml.get("result").asString()));
break;
case ASSERT_PRESENT:
step.setLocator(loadLocator(yaml.get("locator"))) //
.setResultType(toResultType(yaml.get("result").asString()));
break;
case ATTRIBUTE:
step.setScope(toAttributeScope(yaml.get("scope").asString())) //
.setName(yaml.get("name").asString());
if (yaml.has("value")) {
step.setValue(yaml.get("value").asString());
}
break;
case CALL:
step.setCallable(yaml.get("callable").asString());
break;
case CLEAR:
step.setLocator(loadLocator(yaml.get("locator")));
break;
case CLICK:
step.setLocator(loadLocator(yaml.get("locator")));
break;
case EXECUTE_SCRIPT:
step.setScript(yaml.get("script").asString());
break;
case EXT:
step.setNamespace(yaml.get("namespace").asString()) //
.setValue(yaml.get("value").asString());
break;
case FAILURE:
step.setMessage(yaml.get("message").asString()) //
.setResultType(toResultType(yaml.get("result").asString()));
break;
case GET:
step.setUrl(yaml.get("url").asString());
break;
case MESSAGE:
step.setMessage(yaml.get("message").asString());
break;
case SEND_KEYS:
step.setLocator(loadLocator(yaml.get("locator"))) //
.setValue(yaml.get("value").asString());
break;
case SET_VARIABLE:
step.setName(yaml.get("name").asString()) //
.setValue(yaml.get("value").asString());
break;
case SET_MASKED_VARIABLE:
step.setName(yaml.get("name").asString()) //
.setValue(yaml.get("value").asString());
break;
case SLEEP:
step.setSeconds(yaml.get("seconds").asInt());
break;
case SUCCESS:
step.setMessage(yaml.get("message").asString()) //
.setResultType(toResultType(yaml.get("result").asString()));
break;
case STATUS:
step.setLabel(yaml.get("label").asString());
break;
case TAKE_SCREENSHOT:
step.setFileName(yaml.get("fileName").asString());
break;
case WAIT_FOR:
step.setLocator(loadLocator(yaml.get("locator")));
break;
case WAIT_FOR_NOT:
step.setLocator(loadLocator(yaml.get("locator")));
break;
default:
throw new NotImplementedException("type: " + type);
}
return step;
}
private static AttributeScope toAttributeScope(
final String scopeAsString
) {
if ("UPLOAD".equals(scopeAsString)) {
return AttributeScope.UPLOAD;
} else if ("RUN".equals(scopeAsString)) {
return AttributeScope.RUN;
} else if ("STEP".equals(scopeAsString)) {
return AttributeScope.STEP;
} else {
throw new NotImplementedException("scope: " + scopeAsString);
}
}
private static ResultType toResultType(
final String resultTypeAsString
) {
if ("FAILURE".equals(resultTypeAsString)) {
return ResultType.FAILURE;
} else if ("SUCCESS".equals(resultTypeAsString)) {
return ResultType.SUCCESS;
} else {
throw new NotImplementedException("resultType: " + resultTypeAsString);
}
}
private static Locator loadLocator(
final Yaml locatorYaml
) {
return instantiate(MutableLocator.class) //
.setType(toLocatorType(locatorYaml.get("type").asString())) //
.setValue(locatorYaml.get("value").asString());
}
private static LocatorType toLocatorType(
final String typeAsString
) {
if ("By.id".equals(typeAsString)) {
return LocatorType.BY_ID;
} else if ("By.cssSelector".equals(typeAsString)) {
return LocatorType.BY_CSS_SELECTOR;
} else if ("By.xpath".equals(typeAsString)) {
return LocatorType.BY_XPATH;
} else {
throw new NotImplementedException("type: " + typeAsString);
}
}
private interface MutableElaborateLog extends ElaborateLog {
MutableElaborateLog setGuixerVersion(
String guixerVersion
);
MutableElaborateLog setDateAsString(
String dateAsString
);
MutableElaborateLog setDate(
@Nullable DateTime date
);
MutableElaborateLog setTestClassName(
String testClassName
);
MutableElaborateLog setTestClassSimpleName(
String testClassSimpleName
);
MutableElaborateLog setTestMethodName(
String testMethodName
);
MutableElaborateLog setTimeMillis(
long timeMillis
);
MutableElaborateLog setScenario(
String scenario
);
MutableElaborateLog setSuccessCount(
int successCount
);
MutableElaborateLog setFailureCount(
int failureCount
);
MutableElaborateLog addToAttributes(
Attribute attribute
);
MutableElaborateLog setBefore(
@Nullable Before before
);
MutableElaborateLog addToIntents(
Intent intent
);
MutableElaborateLog setIntents(
Intent[] intents
);
MutableElaborateLog setInError(
boolean inError
);
MutableElaborateLog setError(
@Nullable LogError error
);
MutableElaborateLog setRawBytes(
byte[] bytes
);
MutableElaborateLog setDone(
Done done
);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy