Extent Reports – Screenshots of Failed Test Cases in Extent Reports

Insert Screenshots of a Failed Test Cases in Extent Reports:

In this post, we see how to add Screenshots in Extent Reports – Selenium WebDriver.

Check this post for Latest Version of Extent Reports.

Pre-requisites to Insert Screenshots in Extent Reports:

  1. Java should be installed (Link to Install and setup Java )
  2. TestNG should be installed
  3. Extent Report Jars (Version 2.41.2) – Download
  4. extent-config.xml – It allows to configure HTML Report

Steps To Generate Extent Reports:

  1. Firstly, create a TestNG project in eclipse
  2. Now download extent library files from the following link: http://extentreports.com/
  3. Add the downloaded library files in your project
  4. Add Selenium WebDriver Jars
  5. Create a java class say ‘ExtentReportsClass’ and add following code to it

NOTE: In this post, I am not explaining everything. In the earlier post (i.e., Generate ExtentReports), I have mentioned detailed explanation. In this I just explain how to capture screenshot and pass it to the extent reports.

Below mentioned method is to capture the screenshot and returns the path of the screenshot.

Same code (getScreenshot method) I have placed in the below TestNG Class. You could also place this code in your utilities function class.

If you are a beginner and wants to know how to capture screenshot then check this post on how to capture screenshot in Selenium and this post to capture full page screenshot using aShot

Program to insert captured screenshots in extent reports:

extent-config.xml: 

By using this external XML file (extent-config.xml), we could change the details such as Report Theme (either standard or dark), Report Title, Document Title etc.,

We use extent object and use loadConfig() method to load this xml file.

Refresh the project after execution of above ExtentReportsClass.java file. You could find an HTML file named “STMExtentReport.html” in your test-output folder. Copy the location of the STMExtentReport.html file and open it by using any browser. You could see a beautiful high rich HTML reports as shown below.

Extent Report – Test Summary Report Which Contains Screenshot of a Failed Test Case:

Screenshot Failed Test Case Extent Reports

 

This way we could add screenshots of a failed test cases in extent reports.

You could also find introduction of extentreports by clicking on this link

Rajkumar SM is a founder of SoftwareTestingMaterial. He is a certified Software Test Engineer by profession and blogger & youtuber by choice. He has an extensive experience in the field of Software Testing. He writes here about Software Testing which includes both Manual and Automation Testing. He loves to be with his wife and cute little kid 'Freedom'.

4 thoughts on “Extent Reports – Screenshots of Failed Test Cases in Extent Reports”

  1. Hi,
    I added the same code but Image is not attached to the report. Image displayed as a line.
    By Looking in to my Code can you please help me where is the error.

    I am using Latest version Extentreports-3.1.1.jar

    public static WebDriver driver;
    public static WebDriverWait wait;
    public static Properties sprops = new Properties();
    public static Properties aprops = new Properties();
    public String ScrPath = System.getProperty(“user.dir”);
    public File SharedRepofile = new File(ScrPath+”\\Repositories\\SharedRepo.properties”);
    public FileInputStream SharedRepoInputFile;
    public FileInputStream AppRepoInputFile;
    public String ErrScPath = System.getProperty(“user.dir”)+”\\ErrorScreenshots\\”;

    public String CaptureScreenShot(String screenshotname) throws Exception{
    DateFormat DF = new SimpleDateFormat(“dd-MM-yyyy_HHmmss”);
    Date D = new Date();
    String time = DF.format(D);
    TakesScreenshot ts = (TakesScreenshot)driver;
    File Source = ts.getScreenshotAs(OutputType.FILE);
    String dest = ErrScPath + screenshotname+”_”+time+”.png”;
    File destination = new File(dest);
    FileUtils.copyFile(Source, destination);
    return dest;
    }

    @BeforeTest()
    public void beforeTest(String Browser, String DataFile, String AppRepofile ) throws Exception {
    if (Browser.equalsIgnoreCase(“Chrome”)) {
    ChromeOptions options = new ChromeOptions();
    options.addArguments(“test-type”);
    options.addArguments(“disable-popup-blocking”);
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    System.setProperty(“webdriver.chrome.driver”, “D:\\lib\\chromedriver.exe”);
    driver = new ChromeDriver();
    }
    else if(Browser.equalsIgnoreCase(“IE”)){
    System.setProperty(“webdriver.ie.driver”, “D:\\lib\\IEDriverServer.exe”);
    driver = new InternetExplorerDriver();
    }
    else if(Browser.equalsIgnoreCase(“Firefox”)) {
    System.setProperty(“webdriver.gecko.driver”, “D:\\lib\\geckodriver.exe”);
    driver = new FirefoxDriver();
    }

    Capabilities cap = ((RemoteWebDriver)driver).getCapabilities();
    String OS = System.getProperty(“os.name”);
    String BrowserName = cap.getBrowserName().toLowerCase();
    String Version = cap.getVersion();

    HtmlReporter = new ExtentHtmlReporter(System.getProperty(“user.dir”)+”\\CustomReport\\MyOwnReport.html”);
    Extent = new ExtentReports();
    Extent.attachReporter(HtmlReporter);

    Extent.setSystemInfo(“OS”, OS);
    try {
    Extent.setSystemInfo(“Host Name”, Inet4Address.getLocalHost().toString());
    } catch (UnknownHostException e) {
    e.printStackTrace();
    }
    Extent.setSystemInfo(“Web Browser”, BrowserName);
    Extent.setSystemInfo(“Browser Version : “, Version);

    HtmlReporter.config().setChartVisibilityOnOpen(true);
    HtmlReporter.config().setDocumentTitle(“Logix Health Automation Test Report”);
    HtmlReporter.config().setReportName(“Logix Express”);
    HtmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
    HtmlReporter.config().setTheme(Theme.STANDARD);

    setExcelFile(ScrPath+DataFile);

    driver.get(GetDatafromCell(“Global”, 2, “URL”));
    driver.manage().window().setSize(new Dimension(1280, 1024));
    Thread.sleep(3000);

    SharedRepoInputFile = new FileInputStream(SharedRepofile);
    sprops.load(SharedRepoInputFile);
    AppRepoInputFile = new FileInputStream(ScrPath+AppRepofile);
    aprops.load(SharedRepoInputFile);

    login(GetDatafromCell(“Global”, 2, “UID”), GetDatafromCell(“Global”, 2, “PWD”));

    Thread.sleep(5000);
    }

    @AfterSuite
    public void afterSuite() {
    File f = new File(System.getProperty(“user.dir”)+”\\CustomReport\\MyOwnReport.html”);
    if (f.exists()) {
    String oldtDir = System.getProperty(“user.dir”) + “\\CustomReport\\Old\\”;
    File fold = new File(oldtDir);
    //f= new File(“MyOwnReport.html”).renameTo(new SimpleDateFormat(“dd-MM-yyyy HH:mm:ss”).format(new Date()+”MyOwnReport.html”);
    String rn = new SimpleDateFormat(“dd-MM-yyyy HH:mm:ss”)+”MyOwnReport.html”;
    File nf = new File(rn);
    f.renameTo(nf);
    try {
    FileUtils.moveFileToDirectory(f, fold, true);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    Extent.flush();
    }

    @BeforeMethod
    public void beforeMethod(Method method) {
    Test = Extent.createTest((this.getClass().getSimpleName()));
    }

    @Test
    public void forTest() throws Exception {
    driver.findElement(By.linkText(“Dashboard”)).click();
    Thread.sleep(6000);
    }

    @Test
    public void forTest2() throws Exception {
    driver.findElement(By.linkText(“Work Queue”)).click();
    driver.findElement(By.id(“toggleOutstanding”)).click();
    Thread.sleep(3000);
    }
    @AfterMethod
    public void getResult(ITestResult result) throws Exception{
    if (result.getStatus() == ITestResult.FAILURE) {
    // With Java Screen Capture
    String imagepath = CaptureScreenShot(result.getName());
    Test.log(Status.FAIL, MarkupHelper.createLabel(result.getName() + ” Test case FAILED due to below issues:”, ExtentColor.RED));
    //Test.log(Status.FAIL, MarkupHelper.createLabel(result.getThrowable()+ ” Test case FAILED :”, ExtentColor.RED));
    //Test.fail(result.getThrowable());
    //Test.log(Status.FAIL, (Markup) Test.addScreenCaptureFromPath(imagepath));
    //Test.log(Status.FAIL, Test.addScreenCaptureFromPath(imagepath));
    Test.fail(“Snapshot below : ” + Test.addScreenCaptureFromPath(imagepath));
    }
    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());
    }

    }

    Reply

Leave a Comment

Share via
Copy link
Powered by Social Snap