How to generate extent report with specflow and selenium c#

How to generate an HTML report using extent report in selenium with Sepcflow and c#.The main part of automation is to generate a report to analyze the automation results. In this Specflow Tutorial will see how to generate extent report in BDD automation

Extent report library provides good support to generate a beautiful and color full report for almost all the platforms or Frameworks Like TestNG, Nunit and Specflow and BDD. So In this tutorial, we will see, How to generate extent reports with Specflow and selenium c#.

We will implement hooks to generate an HTML report is Specflow. because hooks play an important role to put any condition between the test cases. Read in detail about the HOOKS in SPECFLOW.

Steps to generate extent report with Specflow and selenium c#

Generate an HTML  report in selenium with spec flow and c# is really simple. below are the steps to generate an HTML report to analyze automation results.

  • Install extent report plugin from the Nuget tool of visual studio.
  • Create a object of ExtentHtmlReporter().
  • Set a theme for the report.
  • Create an object of ExtentReports() and the attached reporter.
  • Add Tittle of feature file.
  • Add scenario tittle in feature file.
  • Add step definition in the Scenario.
  • Keep condition According to the error.

 

generate extent report with specflow and selenium c#

Let’s start writing code for extent reports. Here I going to explain the Important code line by line. At the end of the post, you can see the complete implementation of the extent report with hooks.

Create an object of ExtentHtmlReporter Which will take the path as input where you want to save the report.

ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(path);

Configure the Theme of extent report

htmlReporter.Configuration().Theme = AventStack.ExtentReports.Reporter.Configuration.Theme.Standard;

Create an object of extent report and attached reporter

ExtentReportsextent = new ExtentReports();

Add Feature file title n the report

ExtentTestfeatureName = extent.CreateTest<Feature>(FeatureContext.Current.FeatureInfo.Title);

Add Scenario title in feature file or under that feature

ExtentTestscenario = featureName.CreateNode<Scenario>(ScenarioContext.Current.ScenarioInfo.Title);

Now we need to add test definitions Like Given, When And then ETC.

Find the definition type

var stepType = ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString();

Find the error if the test is failed

ScenarioContext.Current.TestError

This will return an error message if there is some error otherwise it will return null so we can use this in the if-else condition accordingly can add the step definition in the report.

Add step definition in the report

scenario.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.Text);

Generate extent report with Specflow and selenium c#

using SpecFlow;
using System;
using TechTalk.SpecFlow;
using AventStack.ExtentReports;
using AventStack.ExtentReports.Gherkin.Model;
using AventStack.ExtentReports.Reporter;
using SpecFlow.Pages;


namespaceDemoPro.Hooks
{
    [Binding]
publicsealedclassHooks
    {
privatestatic ExtentTest featureName;
privatestatic ExtentTest scenario;
privatestatic ExtentReports extent;
publicstaticstring ReportPath;  

        [BeforeTestRun]
publicstaticvoid BeforeTestRun()
        { 
            DriverContext.Initialize();
            Page.Initialize();
string path1 = AppDomain.CurrentDomain.BaseDirectory.Replace("\\bin\\Debug", "");
string path = path1+"Report\\index.html";
            ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(path);
            htmlReporter.Configuration().Theme = AventStack.ExtentReports.Reporter.Configuration.Theme.Standard;
            extent = new ExtentReports();
            extent.AttachReporter(htmlReporter);
        }


        [BeforeFeature]
publicstaticvoid BeforeFeature()
        {
//Create dynamic feature name

            featureName = extent.CreateTest<Feature>(FeatureContext.Current.FeatureInfo.Title);
            Console.WriteLine("BeforeFeature");

        }

        [BeforeScenario]
publicvoid BeforeScenario()
        {
            Console.WriteLine("BeforeScenario");
            scenario = featureName.CreateNode<Scenario>(ScenarioContext.Current.ScenarioInfo.Title);
        }

        [AfterStep]
publicvoid InsertReportingSteps()
        {
var stepType = ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString();
if (ScenarioContext.Current.TestError == null)
            {
if (stepType == "Given")
                    scenario.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.Text);
elseif (stepType == "When")
                    scenario.CreateNode<When>(ScenarioStepContext.Current.StepInfo.Text);
elseif (stepType == "Then")
                    scenario.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.Text);
elseif (stepType == "And")
                    scenario.CreateNode<And>(ScenarioStepContext.Current.StepInfo.Text);
            }elseif(ScenarioContext.Current.TestError != null)
            {                            
if (stepType == "Given") {
                 scenario.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);

                }
elseif (stepType == "When")
                {
                    scenario.CreateNode<When>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);

                }
elseif (stepType == "Then") { 
                    scenario.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);

                }
elseif (stepType == "And")
                {
                    scenario.CreateNode<And>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);

                }               
            }
        }

        [AfterScenario]
publicvoid AfterScenario()
        {
            Console.WriteLine("AfterScenario");
//implement logic that has to run after executing each scenario
        }



        [AfterTestRun]
publicstaticvoid AfterTestRun()
        {
            DriverContext.Driver.Close();
            DriverContext.Driver.Dispose();
//kill the browser
//Flush report once test completes
            extent.Flush();
//kill the browser
        }


    }
}

How to Add Screenshots in extent report using Specflow C#.

An HTML report will help us to understand the report or it is a summary report of our test suite execution. If the suite or automation frameworks contains 1000 of case then the result analysis is really difficult. Extent report have a perfect solution to this problem.

We can add a screenshot of the failed Testcase in the HTML report that will help to understand the exact reason for failing the test case. So here is the complete tutorial to add a screenshot in extent report using Specflow C#.

Free Specflow Tutorial