TakeScreenshotAspect

package com.mridul.software.automation.aspects.webdriver;  
 import com.google.common.base.Preconditions;  
 import com.mridul.software.automation.spring.context.AppPropertiesCtx;  
 import lombok.extern.slf4j.Slf4j;  
 import org.apache.commons.io.FileUtils;  
 import org.aspectj.lang.annotation.AfterReturning;  
 import org.aspectj.lang.annotation.Aspect;  
 import org.aspectj.lang.annotation.Pointcut;  
 import org.springframework.beans.factory.annotation.Autowired;  
 import java.io.File;  
 import java.util.LinkedHashSet;  
 import java.util.Set;  
 @Aspect  
 @Slf4j  
 public class TakeScreenshotAspect {  
   @Autowired  
   private AppPropertiesCtx config;  
   private final static ThreadLocal<Set<String>> FILENAME_THREAD_LOCAL = ThreadLocal.withInitial(LinkedHashSet::new);  
   @Pointcut("execution(** org.openqa.selenium.TakesScreenshot.getScreenshotAs(..))")  
   public void interceptTakeScreenshot() {  
   }  
   @AfterReturning(value = "interceptTakeScreenshot()", returning = "retVal")  
   public void invokeAfter(Object retVal) {  
     try {  
       String fileName = ((File) retVal).getName();  
       log.debug("File name on returning {}", fileName);  
       Preconditions.checkNotNull(config, "config not autowired in aspect!");  
       String prefixPath = config.getReport().getScreenshot().getTest().getMethodName();  
       String fullPath = prefixPath + fileName;  
       log.debug("File path on returning {}", fullPath);  
       FILENAME_THREAD_LOCAL.get().add(fullPath);  
       FileUtils.copyFile((File) retVal, new File(fullPath));  
     } catch (final Throwable e) {  
       log.debug("Invoked due to exception while taking screenshot", e);  
       throw new RuntimeException(e);  
     }  
   }  
   public Set<String> getScreenshotNames() {  
     Set<String> screenshotNames = FILENAME_THREAD_LOCAL.get();  
     screenshotNames.parallelStream().forEach(e -> log.debug("screenshotName : {}", e));  
     return screenshotNames;  
   }  
 }  

Comments

Popular posts from this blog

FileSystemUtils

Report

AbstractTestNgBaseTest