I'm creating a TestNG Maven framework. I have a problem with taking a screenshot on test failure and uploading it to Extent Report Below is Extent Report listener.
String targetLocation = null;
String testClassName = result.getInstanceName();
String errorDate = new SimpleDateFormat("(MM.dd.YYYY HH-mm-ss)").format(new Date());
String testMethodName = result.getName();
String screenShotName = testMethodName + errorDate + ".png";
String fileSeperator = System.getProperty("file.separator");
String reportsPath = System.getProperty("user.dir") + fileSeperator + "TestReport" + fileSeperator
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
targetLocation = reportsPath + fileSeperator + testClassName + fileSeperator + screenShotName;
try {
File targetFile = new File(targetLocation);
FileHandler.copy(src, targetFile);
} catch (FileNotFoundException e) {
Log.info("File not found exception occurred while taking screenshot " + e.getMessage());
} catch (Exception e) {
Log.info("An exception occurred while taking screenshot " + e.getCause());
}
// attach screenshots to report
try {
ExtentTestManager.getTest().fail("Screenshot", MediaEntityBuilder.createScreenCaptureFromPath(targetLocation).build());
} catch (IOException e) {
Log.info("An exception occured while taking screenshot " + e.getCause());
}
ExtentTestManager.getTest().log(Status.FAIL, "Test Failed");