I’ve been researching if it’s possible to append screenshots attachments to the specific methods marked by custom @Captured annotation for the Allure reposts using AOP
I created annotation itself and aspect that intercepts marked methods
@Aspect
public class ScreenshotAppenderAspect {
@Pointcut("@annotation(com.annotations.Captured)")
public void captured (){}
//if method returns object
@Around("captured()")
public Object aroundStep(ProceedingJoinPoint point){
Object result;
//getting name of allure step
String stepName = getStepName();
result = point.proceed();
// method with screenshot attachment
screenshot(stepName);
return result;
}
//if it's void method
@Around("captured() && execution(void *(..))")
public Object aroundStep(ProceedingJoinPoint point){
point.proceed();
// method with screenshot attachment
Screenshot(stepName);
}}
However the result in report is added Screenshot step after marked with @Captured “open google” step.
Is there way to put Screenshot step or attachment itself directly
at the end inside of marked method as if I did it manually within method itself. I understand allure itself works with AOP but I have limited understanding of its methods
Your aspect @Around
advice methods never return any results, i.e. nothing is passed on to the caller, which will yield default values like null
, 0
, false
. Besides, I do not see any need to capture the same joinpoint twice. If you write the aspect in the right way, it will also work for void
methods. Besides, should the package name not be com.annotations
instead of com.annotanions
?
Conceptually, your aspect should look like this, plus probably some error handling:
@Aspect
public class ScreenshotAppenderAspect {
@Around("@annotation(com.annotanions.Captured)")
public Object aroundStep(ProceedingJoinPoint point) throws Throwable {
//getting name of allure step
String stepName = getStepName();
Object result = point.proceed();
// method with screenshot attachment
Screenshot(stepName);
return result;
}
}
2