How to Add Screenshots to TestNG Report

How to add screenshot to TestNG Report example post provides complete real time experience coding and Testing is an activity to verify expected results.While Testing application using Selenium Testing it is very important to capture screenshot of application in order to verify any error is occurred in application while selenium script execution.

Add Screenshots to TestNG Report

Manual testers takes screenshot using print screen (PrtSc) keyboard button or using any third party tools in order to log a defect in a Test Management tools for full proof.However coming to Selenium Automation Testing or Automation Testing taking screenshot and using already taken screenshot in test reports is different.

In this post you’ll learn exactly how to take the screenshot and save the screenshot with different name or with timestamp and add those screenshots to TestNG Report OR Results in HTML reports.

ALSO READ  TestNG - Execute multiple classes in testng examples

Trending Posts

Write Test Cases PASS/FAIL in Excel using Selenium
Complete XPATH Tutorial examples
Selenium Data Driven Framework
Selenium HTML Reports Generation
Read Data From Properties files

Please follow below steps to perform the task.

1.Create the method for screenshot
2.Call the method in another classes to use in @Test methods
3.Store the Screenshot in a separate folder with different name or timestamp.

Class 1:

package com.sel.Selenium.Practise;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.testng.Reporter;
import org.testng.annotations.BeforeTest;

public class SelDriver {

public static WebDriver driver;
String baseurl;
public static Properties property;

public void openBrowser() throws IOException{

//Reporter .log is a Logger given by TestNG framework.
Reporter.log(“Screenshot Capture in TestNG Results Started”); 
System.setProperty(“”, “F:\Java_Applications\Zip Files\chromedriver.exe”);
driver = new ChromeDriver();
Reporter.log(“Chrome Browser is Maximized”);


public void screenCapture() throws IOException{

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
File screenshotName = new File (“E:\Screenshots\”+System.currentTimeMillis()+”_”+”.png”);
FileUtils.copyFile(scrFile, screenshotName);
Reporter.log(“<br><img src='”+screenshotName+”‘ height=’300’ width=’300’/><br>”);



Class 2:

package com.sel.Selenium.Practise;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.Test;

public class GoogleSite extends SelDriver {

@Test//Test Case one
public void seleniumElements() throws InterruptedException, IOException{


System.out.println(“Open Selenium site”);
Reporter.log(“Selenium Site is Open”);

screenCapture();//Calling same method created in first class
Reporter.log(“Screenshot Capture is done”);  
String Title=driver.getTitle();
Assert.assertEquals(Title, “Learn Selenium Automation Testing”);
Reporter.log(“Verifying the Page with Page Title”);
Reporter.log(“Screenshot Capture in TestNG Results ended.”);


Now run the second class as TestNG Test ,you’ll observe screenshot will added in specific folder with time-stamp ,refresh the project and open index.html file to check screenshot is added ot not in TestNG Results under Reporter Output link of TestNG Report.

Please try above code and let me know in case you’re facing any issues.In case code is working fine,Please provide your feedback on below comment box.Thank you for reading and Have a Great Selenium WebDriver Scripting.

Rajesh K