Adding screenshot to the TestNG Report

how to take screenshot for failed test cases in testng
testng report customization
add screenshot in extent report c#
screenshot in testng
how to add screenshot in extent report
screenshot exception selenium
allure screenshot on failure testng
testng modify emailable-report

I'm trying to add Screenshots of the failed test case to the testNG report and I don't have any idea about it

This is my pom class for getting screenshot

package library;

import java.awt.AWTException;
import java.io.File;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

public class TakeScreenShot {

    WebDriver driver;

    public TakeScreenShot(WebDriver driver) {

        this.driver=driver;

    }

     public void CaptureScreenshot(String screenshotName) throws AWTException
        {


                    try {
                                TakesScreenshot ts=(TakesScreenshot)driver;
                                File source=ts.getScreenshotAs(OutputType.FILE);
                                FileUtils.copyFile(source, new File("C:\\Users\\Documents\\Eclipse\\WorkSpace\\ScreenShot\\"+screenshotName));

                                System.out.println("Screenshot taken");


                    } catch (Exception e) {

                                System.out.println("Exception "+e.getMessage());

                    }            
        }
}

And this is my TestNG class

@AfterMethod
  public void CloseBrowser(ITestResult result) throws AWTException {

     String name=result.getName()+"."+result.getMethod().getCurrentInvocationCount()+".png";

     if(ITestResult.FAILURE==result.getStatus())
         {
            ScreenshotPageObjectModel screenshotPom= new ScreenshotPageObjectModel(driver);

             screenshotPom.CaptureScreenshot(name);

         }

        driver.close();
  }

Thanks for helping and please mention where should I do changes for adding the screenshot in the report

You need to use ITestListeners interface of TestNg, it basically provide 7 methods, those are :

onTestStart(ITestResult result)  
onTestFailure(ITestResult result)    
onTestSuccess(ITestResult result)  
onTestSkipped(ITestResult result)  
onTestFailedButWithinSuccessPercentage(ITestResult result)  
onStart(ITestContext context)  
onFinish(ITestContext context)  

Your requirement here is "take screen shot of failed test cases/methods".You should use onTestFailure(ITestResult result) methods.

Demonstration :

public class TestListener implements ITestListener {
        WebDriver driver=null;  
        String filePath = "D:\\SCREENSHOTS";  

        @Override
        public void onTestFailure(ITestResult result) {
            System.out.println("***** Error "+result.getName()+" test has failed *****");
            String methodName=result.getName().toString().trim();
            takeScreenShot(methodName);
        }

        public void takeScreenShot(String methodName) {
            //get the driver
            driver=TestBase.getDriver();
             File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
             //The below method will save the screen shot in d drive with test method name 
                try {
                    FileUtils.copyFile(scrFile, new File(filePath+methodName+".png"));
                    System.out.println("***Placed screen shot in "+filePath+" ***");
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
        public void onFinish(ITestContext context) {}

        public void onTestStart(ITestResult result) {   }

        public void onTestSuccess(ITestResult result) {   }

        public void onTestSkipped(ITestResult result) {   }

        public void onTestFailedButWithinSuccessPercentage(ITestResult result) {   }

        public void onStart(ITestContext context) {   }
    }  

You need to add this tag to your XML file:

<listeners>
       <listener class-name="com.pack.listeners.TestListener"/>
</listeners>

Adding screenshots to TestNG report, Yes, you can include the link to your screenshot in testng report. testclasses with @Listeners({yourListener.class}) or by adding the listener to your testng.xml . 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.

To get screen shot use this method:


 public static void takeSnapShot(WebDriver driver, String fileWithPath) throws Exception {
            TakesScreenshot scrShot = (TakesScreenshot)driver;
            File SrcFile = (File)scrShot.getScreenshotAs(OutputType.FILE);
            File DestFile = new File(fileWithPath);
            FileUtils.copyFile(SrcFile, DestFile);
        }

    You can call the screen shot under TestNG using below code:
     @AfterMethod(
                alwaysRun = true
        )
        protected void afterMethod(ITestResult result, Method method) throws Exception {
            // boolean testStatus = false;
            String fileName = "FAIL  - Error Message Generated on Details Reports";
            byte testStatus;
            if (result.getStatus() == 2) {
                fileName = System.getProperty("user.dir") + "\\Reports\\failure_screenshots\\" + ".png";
                takeSnapShot(this.driver, fileName);
                ExtentTestManager.getTest().log(LogStatus.FAIL, "Error Screenshot" + ExtentTestManager.getTest().addScreenCapture("failure_screenshots\\" + ".png"));
                ExtentTestManager.getTest().log(LogStatus.FAIL, "Test Failed");
                testStatus = 2;
            } else {
                ExtentTestManager.getTest().log(LogStatus.PASS, "Test passed");
                testStatus = 1;
            }

Ref-> https://www.softwaretestingmaterial.com/screenshots-extent-reports/ http://automationtesting.in/capture-screenshot-in-extent-reports-java/

Hope this helps!!!

PS- Use the most stable extent report version - 2.41.1

How to add screenshot in TestNg Report, Added screenshot column to add screen shots on failure. Before jumping ahead, let us first try to check how the default emailable report looks  You need to use ITestListeners interface of TestNg, it basically provide 7 methods, those are : onTestStart(ITestResult result) onTestFailure(ITestResult result) onTestSuccess(ITestResult result) onTestSkipped(ITestResult result) onTestFailedButWithinSuccessPercentage(ITestResult result) onStart(ITestContext context) onFinish(ITestContext context)

I suggest to use ReportNG instead of the TestNG premitive report.

How can I include a failure screenshot to the testNG report, I am using maven , testNg and webdriver to write my selenium test case. i added this method to add screenshot after method if it fails 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. Please follow below steps to perform the task. 3.Store the Screenshot in a seperate folder with different name or timestamp.

Customize TestNG emailable report with screenshots, TestNG library provides a very handy reporting feature. After execution Add code to attach the screenshot to the PDF report. TestNG Report:  On Fri, May 11, 2012 at 3:05 PM, Web User wrote: Hello I am using maven , testNg and webdriver to write my selenium test case. i added this method to add screenshot after method if it fails

Adding Screenshot to TestNg report, log to do so. To see the report in TestNG report you need to click on Reporter output. So question is how can you attach this screen shot into  Pre-requisites to Insert Screenshots in Extent Reports: Java should be installed (Link to Install and setup Java ) TestNG should be installed. Extent Report Jars (Version 2.41.2) – Download. extent-config.xml – It allows to configure HTML Report.

Customize, PDF & Email TestNG Reports in Selenium WebDriver, I am using TestNG framework and I have a test case where I am taking screenshot at each step and pushing to report. But when report is  With the help of TestNG 'ITestListener', we can take screenshots for failed test cases by overriding method named ‘onTestFailure()’ and add login for taking screenshots. We have a complete example to take screenshots for failure. And now we need to add these screenshots to TestNG Report. We also discussed example on TestNG custom report