Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
February 23, 2017

Capture Screenshot in Extent Reports – Java – Version3





Capture Screenshot in Extent Reports will discuss about capturing the screenshot of a particular failure step in the HTML report.  In our previous blog we have seen how to log the messages in the report. Same way here we will capture the screenshot for a particular failure step. The screenshot will give the information about on which screen the script  got failed during the execution. Then we can easily identify the issue by looking into the report.

For this we need to use the log() method of ExtentTest class. To log the screenshot in the report we need to pass the path of the screenshot to the log method as a parameter. So, before logging the screenshot to the report we need to capture the screenshot of that particular failure method and place it in a folder from there we need to get the path of the screenshot file and pass it to the log method.

Below is the sample program to capture screenshot in the report:

import java.io.IOException;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.markuputils.ExtentColor;
import com.aventstack.extentreports.markuputils.MarkupHelper;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;

public class CapturingScreenshot
{
    ExtentHtmlReporter htmlReporter;
    ExtentReports extent;
    ExtentTest test;
    WebDriver driver;
    
    @BeforeTest
    public void config()
    {
        htmlReporter = new ExtentHtmlReporter(System.getProperty("user.dir") +"/test-output/MyOwnReport.html");
        extent = new ExtentReports();
        extent.attachReporter(htmlReporter);
    }
    
    @Test
    public void captureScreenshot()
    {
        test = extent.createTest("captureScreenshot");
        System.setProperty("webdriver.gecko.driver", "/KRISHNA VOLUME/drivers/geckodriver");
        driver = new FirefoxDriver();
        driver.get("https://automationtesting.in");
        String title = driver.getTitle();
        Assert.assertEquals("Home - Automation Test", title);
        }
    
    @AfterMethod
    public void getResult(ITestResult result) throws IOException
    {
        if (result.getStatus() == ITestResult.FAILURE)
        {
            String screenShotPath = GetScreenShot.capture(driver, "screenShotName");
            test.log(Status.FAIL, MarkupHelper.createLabel(result.getName()+" Test case FAILED due to below issues:", ExtentColor.RED));
            test.fail(result.getThrowable());
            test.fail("Snapshot below: " + test.addScreenCaptureFromPath(screenShotPath));
        }
        else if(result.getStatus() == ITestResult.SUCCESS)
        {
            test.log(Status.PASS, MarkupHelper.createLabel(result.getName()+" Test Case PASSED", ExtentColor.GREEN));
        }
        else
        {
            test.log(Status.SKIP, MarkupHelper.createLabel(result.getName()+" Test Case SKIPPED", ExtentColor.ORANGE));
            test.skip(result.getThrowable());
        }
        extent.flush();
    }
    
    @AfterTest
    public void endReport()
    {
        driver.quit();
    }
}

In the above program we have written  test.log method to log the screenshot into the extent reports. This information will logged in the report with time stamp.




Below is the program to capture the screenshot and it will place the screenshot in a particular folder and will return the path of the screenshot. Will use that path to capture the screenshot in the HTML report for a failure step.

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

public class GetScreenShot {
    
    public static String capture(WebDriver driver,String screenShotName) throws IOException
    {
        TakesScreenshot ts = (TakesScreenshot)driver;
        File source = ts.getScreenshotAs(OutputType.FILE);
        String dest = System.getProperty("user.dir") +"\ErrorScreenshots\"+screenShotName+".png";
        File destination = new File(dest);
        FileUtils.copyFile(source, destination);        
                    
        return dest;
    }
}

Above program will return the path of the screenshot.

Below is the output of the above program:

Capture Screenshot in the Report

 

Please watch the YouTube video of this blog for better understanding.



Share this post: on Twitter on Facebook

Extent Reports Log Generation – Java – Version3 Capture FullPage Screenshot in Extent Reports – Java

Related Posts

GENERATE EXTENT REPORT WITH MULTIPLE CLASSES JAVA

Extent Reports-Java-Version3

Extent Report With Multiple Classes

Capture FullPage Screenshot in Extent Reports – Java

Extent Reports-Java-Version3

Capture FullPage Screenshot in Extent Reports – Java

EXTENT REPORTS LOG GENERATION JAVA

Extent Reports-Java-Version3

Extent Reports Log Generation – Java – Version3

GENERATING EXTENT REPORTS JAVA-V3

Extent Reports-Java-Version3

Generating Extent Reports – Java – Version3

EXTENT REPORTS INTRODUCTION JAVA

Extent Reports-Java-Version3

Extent Reports Introduction – Java – Version3

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2025