Hello @kanikahans,
You need to define Extent Report Object at class Level or wherever you like. And then you can use it on Failure.
- For the Report location: Create New Folder with name Report in your project directory at root location
- For the Screenshot location: Create New Folder with name Screenshots in your project directory at root location
Here is the Code:
//Report Initialization
ExtentHtmlReporter htmlreport = new ExtentHtmlReporter(".\\Report\\Extent Report with Screenshot.html");
ExtentReports reports = new ExtentReports();
reports.attachReporter(htmlreport);
ExtentTest testlog;
//Capture and save screenshot
File screen = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
BufferedImage img = ImageIO.read(screen);
File filetest = Paths.get(".").toAbsolutePath().normalize().toFile();
ImageIO.write(img, "png", new File(filetest + "\\Screenshots\\" + "Test.png"));
//Log Screenshot in Report
testlog.info("Details of " + "Test screenshot", MediaEntityBuilder
.createScreenCaptureFromPath(System.getProperty("user.dir") + "\\Screenshots\\" + "Test.png").build());
//Flush Report-Mandatory, Else report will not generate.
reports.flush();
Hope it helps you!!
Thank you!