How to Add Screenshots to TestNG Report?

26 / Dec / 2016 by Ubaid Ahmed 9 comments

Taking screenshots during testing is often considered a good practice. Adding a screenshot to the test reports provides complete clarity and visibility of the application such as if the application is working smoothly or something in the application needs to be fixed. It also makes the report meaningful and presentable.

While carrying out manual testing one can take a screenshot by using “PrtScr” command or by using any screenshot capturing application. However, incase of automation testing, the process of taking screenshot and including it in the report is quite different.

Read this blog to understand how to take a screenshot during automation testing and adding it it to the report.

A brief about TestNG

TestNG is a powerful and easy-to-use testing framework. It is designed in such a manner that it covers various categories of tests such as Unit, Integration, System etc. The official definition of TestNG is :

TestNG is a testing framework inspired from JUnit and NUnit, but introducing some new functionalities that make it more powerful and easier to use.

A task to take screenshot

  1. Access http://google.com
  2. Search for cars
  3. Verify if there is any link containing the word “car”
  4. Take screenshot

Follow the below code to take screenshots 

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//The below method will save the screen shot in d drive with name "screenshot.png"
timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
screenShotName = new File("D:\\MyTest\\Screenshots\\"+timeStamp+".png");
FileUtils.copyFile(scrFile, screenShotName);

Adding screenshots to the report

String filePath = screenShotName.toString();
String path = "<img src="\"file://"" alt="\"\"/" />";
Reporter.log(path);

Complete Working Code

public class TestNGDefaultReport {
static WebDriver driver;
@BeforeSuite
public void setup(){
System.setProperty("webdriver.chrome.driver","D:\\MyTest\\chromedriver.exe");

System.setProperty("org.uncommons.reportng.escape-output", "false");
driver = new ChromeDriver();
}
 
@BeforeMethod
public void beforeEachMethod(){
driver.get("http://google.com");
}
 
//Test case 1
@Test
public void cars() throws Exception {
System.out.println("I am Test method and I am searching for cars");
driver.findElement(By.name("q")).sendKeys("Cars");
 
driver.findElement(By.name("btnG")).click();
 
//Wait for the results to appear
Thread.sleep(2000);
takeScreenshot();
if(driver.findElement(By.partialLinkText("car")).isDisplayed()){
Assert.assertTrue(true); 
}
else{
Assert.assertTrue(false);
}
}
 
@AfterSuite
public void endOfSuite(){
System.out.println("I am the end of suite");
driver.quit();
}
 
public static void takeScreenshot() throws Exception {
String timeStamp;
File screenShotName;
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//The below method will save the screen shot in d drive with name "screenshot.png"
timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime()); 
screenShotName = new File("D:\\MyTest\\Screenshots\\"+timeStamp+".png");
FileUtils.copyFile(scrFile, screenShotName);
 
String filePath = screenShotName.toString();
String path = "<img src="\"file://"" alt="\"\"/" />";
Reporter.log(path);
 
}
}

TestNG Reports

TestNG provides certain predefined listeners. These listeners are by default added to any test execution. Hence, different HTML and XML reports are generated for any test execution. The report is generated by default under the folder named test-output. You can view the report by opening index.html.

Conclusion

You make you reports more meaningful and presentable by adding screenshots to your reports.

FOUND THIS USEFUL? SHARE IT

comments (9)

  1. Sreekala

    Hi,
    The image in testng report is not visible when I send via email as its saved locally.How to make the emailable report with screenshot visible.
    Thanks,
    Sreekala

    Reply
    1. amol

      you need to store screenshots in shared drive which is accessble by all thus if any ine click on hyperlink it would not throw any error

      Reply
  2. siva paritala

    Hi Ubaid Ahmed,
    I uesed the following code to add screenshot in testng report but it doesn’t add imgae can you help me pls….

    @AfterMethod
    public static void Report(ITestResult result)
    {
    if((result.getStatus()== ITestResult.FAILURE) && screenShot_value.equalsIgnoreCase(“true”))
    {
    captureScreenshot(Config.driver, result.getName());
    System.out.println(“——-“+result.getName()+”——–“);
    String filePath=”C:/Users/Public/Pictures/Sample Pictures/Hydrangeas.jpg”;
    System.out.println(“========”+filePath+”========”);
    Reporter.log(filePath);
    }
    }

    public static String captureScreenshot(WebDriver driver, String screenshotName)
    {
    try
    {
    TakesScreenshot ts=(TakesScreenshot)driver;
    File source=ts.getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(source, new File(screenshots_save_path+”\\screenshots\\”+screenshotName+”.png”));
    }
    catch (Exception e)
    {
    System.out.println(“Exception while taking Screenshot”+e.getMessage());
    }
    return screenshotName;
    }

    Reply
    1. Ubaid Ahmed

      Are you getting any error? If yes, please share it. If not, please let me know what is happening.

      Also make sure that you have provided the correct path in FileUtils.copyFile(source, new File(screenshots_save_path+”\\screenshots\\”+screenshotName+”.png”));

      Reply
  3. Yogesh Khachane

    Hi Ubaid Ahmed,
    I tried to add Screenshot to TestNG Report but i can’t do that Please help me and send any small example code to my email if you have time. I am waiting your valuable Reply.
    Thanks,
    Yogesh

    Reply
    1. Ubaid Ahmed

      Hi Yogesh Khachane,

      Thank you for going through the blog. Can you please share the error that you are getting? The code in the blog is a working one and I am able to add the screenshots using it. Try using the same code as it is and later on make modification as per your need.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *