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  How to Select a Dropdown in Selenium WebDriver using Java

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.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Reporter;
import org.testng.annotations.BeforeTest;


public class SelDriver {

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

@BeforeTest
public void openBrowser() throws IOException{

//Reporter .log is a Logger given by TestNG framework.
Reporter.log(“Screenshot Capture in TestNG Results Started”); 
System.setProperty(“webdriver.chrome.driver”, “F:\Java_Applications\Zip Files\chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
Reporter.log(“Chrome Browser is Maximized”);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);

}

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 java.io.IOException;
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{

driver.get(“http://www.easyselenium.com”);

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