Skip to content

Instantly share code, notes, and snippets.

@artsok
Created October 15, 2019 09:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artsok/80a041774eeb218fdc3fb3c7b085eaee to your computer and use it in GitHub Desktop.
Save artsok/80a041774eeb218fdc3fb3c7b085eaee to your computer and use it in GitHub Desktop.
AllureLogger for Atlas-framework
import io.qameta.allure.Allure;
import io.qameta.allure.AllureLifecycle;
import io.qameta.allure.model.Status;
import io.qameta.allure.model.StepResult;
import io.qameta.allure.util.ResultsUtils;
import io.qameta.atlas.core.api.Listener;
import io.qameta.atlas.core.api.Target;
import io.qameta.atlas.core.context.TargetContext;
import io.qameta.atlas.core.internal.Configuration;
import io.qameta.atlas.core.util.MethodInfo;
import org.hamcrest.Matcher;
import java.lang.reflect.Method;
import java.util.*;
public class AllureLogger implements Listener {
private final AllureLifecycle lifecycle = Allure.getLifecycle();
private final Map<String, MethodFormatter> loggableMethods;
public AllureLogger() {
loggableMethods = new HashMap<>();
loggableMethods.put("open", (name, args) -> String.format("Opening page \'%s\'", args[0]));
loggableMethods.put("click", (name, args) -> String.format("Clicking at web element \'%s\'", name));
loggableMethods.put("submit", (name, args) -> String.format("Submitting to web element \'%s\'", name));
loggableMethods.put("clear", (name, args) -> String.format("Clearing web element' input \'%s\'", name));
loggableMethods.put("sendKeys", (name, args) -> {
String arguments = Arrays.toString(((CharSequence[]) args[0]));
return String.format("Typing to the web element \'%s\' value [%s]", name, arguments);
});
loggableMethods.put("waitUntil", (name, args) -> {
Matcher matcher = (Matcher) (args[0] instanceof Matcher ? args[0] : args[1]);
return String.format("Waiting until the web element \'%s\' has state [%s]", name, matcher);
});
loggableMethods.put("should", (description, args) -> {
Matcher matcher = (Matcher) (args[0] instanceof Matcher ? args[0] : args[1]);
return String.format("The web element should be \'%s\' in the state [%s]", description, matcher);
});
}
@Override
public void beforeMethodCall(final MethodInfo methodInfo,
final Configuration configuration) {
getMethodFormatter(methodInfo.getMethod()).ifPresent(formatter -> {
final String name = configuration.getContext(TargetContext.class)
.map(TargetContext::getValue)
.map(Target::name)
.orElse(methodInfo.getMethod().getName());
final Object[] args = methodInfo.getArgs();
lifecycle.startStep(Objects.toString(methodInfo.hashCode()),
new StepResult().setName(formatter.format(name, args)).setStatus(Status.PASSED));
});
}
@Override
public void afterMethodCall(final MethodInfo methodInfo,
final Configuration configuration) {
getMethodFormatter(methodInfo.getMethod())
.ifPresent(title -> lifecycle.stopStep(Objects.toString(methodInfo.hashCode())));
}
@Override
public void onMethodReturn(final MethodInfo methodInfo,
final Configuration configuration,
final Object returned) {
}
@Override
public void onMethodFailure(final MethodInfo methodInfo,
final Configuration configuration,
final Throwable throwable) {
getMethodFormatter(methodInfo.getMethod()).ifPresent(title ->
lifecycle.updateStep(stepResult -> {
stepResult.setStatus(ResultsUtils.getStatus(throwable).orElse(Status.BROKEN));
stepResult.setStatusDetails(ResultsUtils.getStatusDetails(throwable).orElse(null));
})
);
}
private Optional<MethodFormatter> getMethodFormatter(Method method) {
return Optional.ofNullable(loggableMethods.get(method.getName()));
}
@FunctionalInterface
private interface MethodFormatter {
String format(String name, Object[] args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment