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.

Selenium with Python Tutorial | TEC...
Selenium with Python Tutorial | TECHLISTIC.COM

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


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


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;

privatestatic ExtentTest featureName;
privatestatic ExtentTest scenario;
privatestatic ExtentReports extent;
publicstaticstring ReportPath;  

publicstaticvoid BeforeTestRun()
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();

publicstaticvoid BeforeFeature()
//Create dynamic feature name

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


publicvoid BeforeScenario()
            scenario = featureName.CreateNode<Scenario>(ScenarioContext.Current.ScenarioInfo.Title);

publicvoid InsertReportingSteps()
var stepType = ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString();
if (ScenarioContext.Current.TestError == null)
if (stepType == "Given")
elseif (stepType == "When")
elseif (stepType == "Then")
elseif (stepType == "And")
            }elseif(ScenarioContext.Current.TestError != null)
if (stepType == "Given") {

elseif (stepType == "When")

elseif (stepType == "Then") { 

elseif (stepType == "And")


publicvoid AfterScenario()
//implement logic that has to run after executing each scenario

publicstaticvoid AfterTestRun()
//kill the browser
//Flush report once test completes
//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