Reputation: 1
Please check and let me know anything need to modify
Below code written inside base page:
public static String getScreenshot1() throws IOException {
String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
TakesScreenshot ts = (TakesScreenshot) driver;
File source = ts.getScreenshotAs(OutputType.FILE);
String destination = System.getProperty("user.dir") + "/Screenshots/" + dateName + ".png";
File finalDestination = new File(destination);
FileHandler.copy(source, finalDestination);
return destination;
}
test.log(Status.PASS, "Browser must navigate to Login page",
MediaEntityBuilder.createScreenCaptureFromPath(getScreenshot1()).build());
Upvotes: 0
Views: 1412
Reputation: 41
To have the screenshots available across all the system, we can convert the screenshot as base64 string and then attach it in ExtentReport.
To convert a screenshot path to base64 image:
InputStream in = new FileInputStream(getScreenshot1());
byte[] imageBytes = IOUtils.toByteArray(in);
String base64 = Base64.getEncoder().encodeToString(imageBytes);
test.log(Status.PASS,"Attached Screenshot ", MediaEntityBuilder.createScreenCaptureFromBase64String("data:image/png;base64,"+base64).build());
You can refer the extent report documentation here.
Upvotes: 0