allure2 listener to output the steps in console

Viewed 3333

I am using Allure2 with TestNG. I want to write my own listener which prints @Steps in the console output.

I saw the interface "StepLifecycleListener" in allure but I am not able to implement this listener in TestNg. Any pointers ?

@Override
public void beforeStepStart(final StepResult result) {
    System.out.format("Starting step: %s", result.getName());

}


@Override
public void afterStepStop(final StepResult result) {
    System.out.format("Completed step: %s", result.getName());

}
2 Answers

You could easily achieve that with AspectJ as well.

public class StepAspect {
    private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

    @SuppressWarnings("EmptyMethod")
    @Pointcut("@annotation(io.qameta.allure.Step)")
    public void withStepAnnotation() {
        //pointcut body, should be empty
    }

    @SuppressWarnings("EmptyMethod")
    @Pointcut("execution(* *(..))")
    public void anyMethod() {
        //pointcut body, should be empty
    }

    @Before("anyMethod() && withStepAnnotation()")
    public void stepStart(final JoinPoint joinPoint) {
        if (logger.isInfoEnabled()) {
            final MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
            final Step step = methodSignature.getMethod().getAnnotation(Step.class);
            logger.info("Step: {}: {}", joinPoint.getSignature()
                    .toShortString(), AspectUtils.getName(step.value(), joinPoint));
        }
    }
}
Related