I am not able to attach screenshots to extent reports. What is the solution?

60 Views Asked by At
@AfterMethod
    public void getResult(ITestResult result) throws Exception{
     if(result.getStatus() == ITestResult.FAILURE)
    {
       extentlogger.log(Status.FAIL, MarkupHelper.createLabel(result.getThrowable() + 
       " - Test Case Failed", ExtentColor.RED));
   
       try {
       // get path of captured screenshot using custom failedTCTakeScreenshot method
       String screenshotPath = takeSnapShot(result);
       extentlogger.fail("Test Case Failed Snapshot is below " + 
     extentlogger.addScreenCaptureFromPath(screenshotPath));
      } catch (InterruptedException e) {
       e.printStackTrace();
       }
     }
    }

The above code is the code I'm using after method in TestNG. Am I doing anything wrong?enter image description here

1

There are 1 best solutions below

0
On

You don't have any method named as takeSnapShot or its not declared as public:

Create a method in base class with below content or just directly add below code in the aftermethod hook by replacing takeSnapShot step:

    //Convert web driver object to TakeScreenshot

    TakesScreenshot scrShot =((TakesScreenshot)webdriver);

    //Call getScreenshotAs method to create image file

            File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);

        //Move image file to new destination

            File DestFile=new File(fileWithPath);

            //Copy file at destination

            FileUtils.copyFile(SrcFile, DestFile);