Extent Report is a popular reporting library used in Selenium to generate interactive HTML reports. To take a screenshot in Extent Report, you can follow these steps:
1.) Save the Screenshot
TakesScreenshot screenshot = driver;
// Capture the screenshot as a file object
File srcFile = screenshot.getScreenshotAs(OutputType.FILE);
// Save the screenshot to a desired location
FileUtils.copyFile(srcFile,
new File("C:/Users/arili/git/Assignments/Selenium/target/exteAttachScreenshot.png"));
- We can save screenshots when running the test to the desired folder.
- From here, this screenshot can be asked to attach onto our report.
2.) Attach the Extent
testCase.addScreenCaptureFromPath("C:/Users/arili/git/Assignments/Selenium/target/exteAttachScreenshot.png");
- We can add a captured screenshot from the path using the ExtentTest method from the extent report utility.
3.) Example:
For this, we will be using the programsbuzz site.
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa. Selenium.OutputType;
import org.openqa. Selenium.TakesScreenshot;
import org.openqa. Selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeTest;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import io.github.bonigarcia.wdm.WebDriverManager;
public class Ext {
ChromeDriver driver;
ExtentReports extent;
ExtentHtmlReporter extentRep;
ExtentTest testCase;
@org.testng.annotations.Test
public void verifyUrl() throws Exception {
testCase = extent.createTest("Print URL");
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.get("http://programsbuzz.com/user/login");
driver.manage().window().maximize();
System.out.println("Current Page URL Is: " + driver.getCurrentUrl());
String currentUrl = driver.getCurrentUrl();
if (currentUrl.contains("login")) {
testCase.log(Status.PASS, "Actual Title!");
} else {
testCase.log(Status.FAIL, "Not Actual Title!");
}
TakesScreenshot screenshot = driver;
// Capture the screenshot as a file object
File srcFile = screenshot.getScreenshotAs(OutputType.FILE);
// Save the screenshot to a desired location
FileUtils.copyFile(srcFile,
new File("C:/Users/arili/git/Assignments/Selenium/target/exteAttachScreenshot.png"));
}
@BeforeTest
public void extentLaunch() {
extent = new ExtentReports();
extentRep = new ExtentHtmlReporter("target/extentExampleAttachScreenshot.html");
extent.attachReporter(extentRep);
}
@AfterMethod
public void finOper() throws IOException {
testCase.addScreenCaptureFromPath("C:/Users/arili/git/Assignments/Selenium/target/exteAttachScreenshot.png");
extent.flush();
driver.quit();
extent.flush();
}
}
- Here we are capturing the screenshot inside the test annotation.
- In the after method we attach the saved screenshot to the report we are generating and flush the ExtentReports.
Alternatively, we can also follow the method of utils and attach a screenshot.
4.) Utility
Let us create a screenshot utility class.
package week4.day2;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa. Selenium.OutputType;
import org.openqa. Selenium.TakesScreenshot;
import org.openqa. Selenium.WebDriver;
public class Utility {
public String getScreenshot(WebDriver driver) {
TakesScreenshot ts = (TakesScreenshot) driver;
File src = ts.getScreenshotAs(OutputType.FILE);
String path = System.getProperty("user.dir") + "/Screenshot/" + System.currentTimeMillis() + ".png";
File destination = new File(path);
try {
FileUtils.copyFile(src, destination);
} catch (IOException e) {
System.out.println("Capture Failed " + e.getMessage());
}
return path;
}
}
- Here we are creating a method getScreenshot() to screenshot and save it to a path.
- We will utilize this class inside our afterMethod annotation in our main test class.
5.) Initiate
package week4.day2;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa. Selenium.OutputType;
import org.openqa. Selenium.TakesScreenshot;
import org.openqa. Selenium.chrome.ChromeDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeTest;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.MediaEntityBuilder;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import io.github.bonigarcia.wdm.WebDriverManager;
public class Ext extends Utility {
ChromeDriver driver;
ExtentReports extent;
ExtentHtmlReporter extentRep;
ExtentTest testCase;
Utility u = new Utility();
@org.testng.annotations.Test
public void verifyUrl() throws Exception {
testCase = extent.createTest("Print URL");
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.get("http://programsbuzz.com/user/login");
driver.manage().window().maximize();
System.out.println("Current Page URL Is: " + driver.getCurrentUrl());
String currentUrl = driver.getCurrentUrl();
if (currentUrl.contains("login")) {
testCase.log(Status.PASS, "Actual Title!");
} else {
testCase.log(Status.FAIL, "Not Actual Title!");
}
TakesScreenshot screenshot = driver;
// Capture the screenshot as a file object
File srcFile = screenshot.getScreenshotAs(OutputType.FILE);
// Save the screenshot to a desired location
FileUtils.copyFile(srcFile,
new File("C:/Users/arili/git/Assignments/Selenium/target/exteAttachScreenshot.png"));
}
@BeforeTest
public void extentLaunch() {
extent = new ExtentReports();
extentRep = new ExtentHtmlReporter("target/extentExampleAttachScreenshot.html");
extent.attachReporter(extentRep);
}
@AfterMethod
public void finOper(ITestResult result) throws IOException {
if (result.getStatus() == ITestResult.FAILURE) {
String temp = u.getScreenshot(driver);
testCase.fail(result.getThrowable().getMessage(),
MediaEntityBuilder.createScreenCaptureFromPath(temp).build());
}
extent.flush();
driver.quit();
extent.flush();
}
}
- In the after method, we have used ITestResult to get the test result, and within a simple if statement, we have mentioned that if the test were to fail, get the screenshot from the Utility.
- Then we use the createScreenCaptureFromPath and attach it to the report.
- If we want a screenshot attached for a passing scenario, mention ITestResult.SUCCESS within if.

- All of the methods yield the same result.