Careeroza
Attaching Screenshots to Reports
Base64 Screenshots in Extent
Attach the screenshot as a Base64 string so it is stored inside the HTML file. A file path breaks as soon as the report is emailed or moved.
@Override
public void onTestFailure(ITestResult result) {
WebDriver driver = DriverFactory.getDriver();
String base64 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);
ExtentManager.test().fail(result.getThrowable(),
MediaEntityBuilder.createScreenCaptureFromBase64String(base64, result.getName()).build());
}Attachments in Allure
@Override
public void onTestFailure(ITestResult result) {
WebDriver driver = DriverFactory.getDriver();
byte[] png = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
Allure.addAttachment("Screenshot on failure", "image/png", new ByteArrayInputStream(png), "png");
Allure.addAttachment("Page URL", driver.getCurrentUrl());
Allure.addAttachment("Page source", "text/html", driver.getPageSource(), "html");
}Tip: Attach the current URL and the page source as well. Many failures ("element not found") are explained by seeing that the app showed an error page.
Practice
Exercise: Write a helper ScreenshotUtils.fileName(String testName, LocalDateTime time) that returns names like addToCart_20260314_103005.png.
Show answer
public static String fileName(String testName, LocalDateTime time) {
return testName + "_" + time.format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")) + ".png";
}