How to generate extent report with specflow and selenium c#

The main part of automation is generate report. To generate HTML report extent report provide good support a beautiful and color full report for almost all the platform or Framework Like TestNG, Nunit and specflow and BDD. So In this tutorial we will see, How to generate extent report with specflow and selenium c#.

Steps to generate extent report with specflow and selenium c#

Step 1) Install extent report plugin from Nuget tool of visual studio.

Step 2) Create a object of ExtentHtmlReporter().

Step 3) Set a theme for the report.

Step 4) Create an object of ExtentReports() and attached reporter.

Step 5) Add Tittle of feature file.

Step 6) Add scenario tittle in feature file.

Step 7) Add step definition in the Scenario.

Step 8) Keep condition According to the error.

generate extent report with specflow and selenium c#

Start writing code for extent report here I going to explain Important code line by Line. At the end of the post you can see complete implementation of extent report with hooks.

Create a object of ExtentHtmlReporter Which will take 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 extends 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 test is failed

ScenarioContext.Current.TestError

This will return error message if there is some error other wise it will return null so we can using 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
        }


    }
}

 

Add a Comment

Your email address will not be published. Required fields are marked *

17 − seventeen =