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
December 9, 2016

Capture Screenshot in Extent Reports – CSharp





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:

using NUnit.Framework;
using NUnit.Framework.Interfaces;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using RelevantCodes.ExtentReports;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExtentReportsDemo
{
    [TestFixture]
    public class CapturingScreenshot
    {
        ExtentReports extent;
        ExtentTest test;
        IWebDriver driver;

        [OneTimeSetUp]
        public void Init()
        {
            string path = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
            string actualPath = path.Substring(0, path.LastIndexOf("bin"));
            string projectPath = new Uri(actualPath).LocalPath;
            string reportPath = projectPath + "Reports\\ExtentScreenshot.html";
            extent = new ExtentReports(reportPath, true);
        }

        [Test]
        public void CaptureScreenshot()
        {
            test = extent.StartTest("CaptureScreenshot");
            driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("https://automationtesting.in");
            string title = driver.Title;
            Assert.AreEqual("Home - Automation Test", title);
            test.Log(LogStatus.Pass, "Test Passed");
        }

        [TearDown]
        public void GetResult()
        {
            var status = TestContext.CurrentContext.Result.Outcome.Status;
            var stackTrace = "<pre>" + TestContext.CurrentContext.Result.StackTrace + "</pre>";
            var errorMessage = TestContext.CurrentContext.Result.Message;

            if (status == TestStatus.Failed)
            {
                string screenShotPath = GetScreenShot.Capture(driver, "ScreenShotName");
                test.Log(LogStatus.Fail, stackTrace + errorMessage);
                test.Log(LogStatus.Fail, "Snapshot below: " + test.AddScreenCapture(screenShotPath));
            }
            extent.EndTest(test);
        }

        [OneTimeTearDown]
        public void Endreport()
        {
            driver.Close();
            extent.Flush();
            extent.Close();
        }
    }
}

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.

using OpenQA.Selenium;
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExtentReportsDemo
{
    public class GetScreenShot
    {
        public static string Capture(IWebDriver driver, string screenShotName)
        {
            ITakesScreenshot ts = (ITakesScreenshot)driver;
            Screenshot screenshot = ts.GetScreenshot();
            string pth = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
            string finalpth = pth.Substring(0, pth.LastIndexOf("bin")) + "ErrorScreenshots\\" + screenShotName + ".png";
            string localpath = new Uri(finalpth).LocalPath;
            screenshot.SaveAsFile(localpath, ImageFormat.Png);
            return localpath;
        }
    }
}

Above program will return the path of the screenshot.

Below is the output of the above program:

Capture Screenshot in Extent Reports

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



Share this post: on Twitter on Facebook

Extent Reports Log Generation – CSharp Vigo Reports Introduction

Related Posts

LOG GENERATION IN EXTENT REPORTS CSharp

Extents Reports - C#

Extent Reports Log Generation – CSharp

GENERATING EXTENT REPORTS CSharp

Extents Reports - C#

Generating Extent Reports – CSharp

EXTENT REPORTS INTRODUCTION CSharp

Extents Reports - C#

Extent Reports Introduction – CSharp

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