Site Loader
Auckland, New Zealand
In this article we will discuss how to generate Extent Report with Specflow and Selenium C#, this article is splitted into three different videos and this post will keep on updating everytime new video is released.

Extent Reports

ExtentReports is a logger-style API written for Java and .NET environments which allows creating HTML reports from tests.

Extentreport pseudocode ‚Äď BDD Style

Here is the report we are going to generate in this mini series

Here is the complete video of the above discussion   Thanks for reading the post and watching the video!!! Please leave your comments and let me know if there is anything I should update in this post. Thanks, Karthik KK

Post Author: Karthik kk

36 Replies to “Generating Extent Report with Specflow and Selenium C#”

  1. Karthik, thanks for the solution. Your work are always awesome and very helpful. I was tryin to implement extent report but i am getting null point exception on reflection. I am using specflow 2.3.1. Let me know if you have workaround for this.

    Error = System.NullReferenceException : Object reference not set to an instance of an object. Bascially, pInfo = null

    PropertyInfo pInfo = typeof(ScenarioContext).GetProperty(“TestStatus”, BindingFlags.Instance | BindingFlags.NonPublic);
    MethodInfo getter = pInfo.GetGetMethod(nonPublic: true);object TestResult = getter.Invoke(ScenarioContext.Current, null);

    1. Update as below

      PropertyInfo pInfo = typeof(ScenarioContext).GetProperty(“ScenarioExecutionStatus”, BindingFlags.Instance | BindingFlags.Public);
      MethodInfo getter = pInfo.GetGetMethod(nonPublic: true);
      object TestResult = getter.Invoke(ScenarioContext.Current, null);

      1. comment those three lines, and instead of TestResult use the following:
        ScenarioContext.Current.ScenarioExecutionStatus.ToString()
        The error you are facing is probably because you are using specflow 2.1.0

    2. Hi,
      I am using ExtentReports v4.0.3. This below solution worked for me:

      [AfterStep]
      public void AfterEveryStep(ScenarioContext scenarioContext)
      {
      var stepInfo = scenarioContext.StepContext.StepInfo;
      var stepType = stepInfo.StepInstance.StepDefinitionType.ToString();

      ExtentTest stepNode = null;
      switch (stepType)
      {
      case “Given”:
      stepNode = _scenarioNode.CreateNode(stepInfo.Text);
      break;
      case “When”:
      stepNode = _scenarioNode.CreateNode(stepInfo.Text);
      break;
      case “Then”:
      stepNode = _scenarioNode.CreateNode(stepInfo.Text);
      break;
      }

      if (scenarioContext.ScenarioExecutionStatus != ScenarioExecutionStatus.OK)
      {
      Screenshot ss = ((ITakesScreenshot)_driver).GetScreenshot();
      string screenshot = ss.AsBase64EncodedString;

      List failTypes = new List
      {
      ScenarioExecutionStatus.BindingError,
      ScenarioExecutionStatus.TestError,
      ScenarioExecutionStatus.UndefinedStep
      };

      if (scenarioContext.ScenarioExecutionStatus == ScenarioExecutionStatus.StepDefinitionPending)
      {
      stepNode.Skip(“This step has been skipped and not executed.”, MediaEntityBuilder.CreateScreenCaptureFromBase64String(screenshot).Build());
      } else if (failTypes.Contains(scenarioContext.ScenarioExecutionStatus))
      {
      stepNode.Fail(scenarioContext.TestError.Message, MediaEntityBuilder.CreateScreenCaptureFromBase64String(screenshot).Build());
      }
      }
      }

      1. Wow looks like an cool solution, I will try updating the post to latest version of Extent report then !

        Thanks,
        Karthik KK

      2. Thanks very much Gokce for another perspective, been stuck ever since the new version of specflow.
        however in your code, can you please clarify where “_scenarioNode” is declared?

    1. Update as Below

      PropertyInfo pInfo = typeof(ScenarioContext).GetProperty(“ScenarioExecutionStatus”, BindingFlags.Instance | BindingFlags.Public);
      MethodInfo getter = pInfo.GetGetMethod(nonPublic: true);
      object TestResult = getter.Invoke(ScenarioContext.Current, null);

  2. I see this error after running by implementing ExtentReport.

    ManageAccessoriesCatalogSetUp : System.IO.FileNotFoundException : Could not load file or assembly ‘MongoDB.Bson, Version=2.3.0.157, Culture=neutral, PublicKeyToken=null’ or one of its dependencies. The system cannot find the file specified.
    at AventStack.ExtentReports.ExtentReports.CreateTest(String name, String description)
    at ECom.UITests.Reusables.TestBehaviour.BeforeFeatuer() in C:\TFS\SEGOT-eCom-Core\Integration\Src\ECom.Core.UITests\ECom.UITests.Reusables\TestBehaviour.cs: line 51
    at TechTalk.SpecFlow.Bindings.BindingInvoker.InvokeBinding(IBinding binding, IContextManager contextManager, Object[] arguments, ITestTracer testTracer, ref TimeSpan duration)
    at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.FireEvents(HookType bindingEvent)
    at ECom.AMC.UITests.ManageAccessoriesCatalogFeature.FeatureSetup()

  3. I want to know that.. how i can marked “SKIP” to perticular step. As i was able to add Pass and Fail. But i want to apply SKIP for some steps.. so that that will be reflected in my report as well.

  4. Hi Karthik,

    Thank you so much for the detailed session. I just have one clarification on the execution part. It would be really great if you could help me with that. How do we run the specific feature or scenarios based on the user input. For example, if i have 100 scenarios and i wanted to run only 40 scenarios, how do we achieve that in specflow.

      1. Hello Karthik,

        I have manage to generate Screenshots and Reports Separately, How do I need to attach error screenshots in reports , Have you got any video ?

  5. Is there any way by which for any feature marked as “ignore” will be shown in Skip ?
    Currently it is showing as “Pass” in Extent report

    1. I guess with free version we dont have much control over that, but should be available in Pro version.

      Thanks,
      Karthik KK

  6. Now the problem is all cases marked as “Fail” are showing in report.
    And any case marked as “Error” is not shown in report.

    Is there any way we can show both “Fail and Error” test cases ?

  7. Hello karthik,

    Pls suggest me how to rerun failed test cases using Specflow and c#. I am using Nunit to run test cases.
    I have tried intalling retry plugins but none of them are working.
    Request you to pls reply.

  8. Hello Karthik,

    I am not able to rerun the failed test cases.
    I have tried to use all plug in present in nuget but none of them worked for me.
    I am using Specflow with c# and running the test cases using Nunit.

  9. I am unable to attach extent reports generated from Azure pipeline back to test run instance, it was working fine in MSTest, but having issues with Nunit

  10. Hi, thanks for sharing this material, it has helped me a lot. However, I am currently in a problem, although I was able to implement the report I have the following feature:
    Scenario: Repport Test
         Given Step Pass
         And Step Fail
         And Step Skip
    But the report only yields results for step pass and step fail
    then my question is, how can I achieve that if a step of the feature fails the other steps that were not executed in the report are shown as pending execution?
    Greetings from Chile, Sorry but my English is very basic.

  11. warning CS0618: ‘FeatureContext.Current’ is obsolete: ‘Please get the FeatureContext via Context Injection – http://www.specflow.org/documentation/Context-Injection/
    Pages\Hooks1.cs(53,53,53,76): warning CS0618: ‘ScenarioContext.Current’ is obsolete: ‘Please get the ScenarioContext via Context Injection – http://www.specflow.org/documentation/Context-Injection/
    Pages\Hooks1.cs(62,17,62,40): warning CS0618: ‘ScenarioContext.Current’ is obsolete: ‘Please get the ScenarioContext via Context Injection – http://www.specflow.org/documentation/Context-Injection/
    1 warning CS0618: ‘ScenarioContext.Current’ is obsolete: ‘Please get the ScenarioContext via

    Context Injection – http://www.specflow.org/documentation/Context-Injection/
    Hooks1.cs(76,96,76,119): warning CS0618: ‘ScenarioContext.Current’ is obsolete: ‘Please get the ScenarioContext via Context Injection – http://www.specflow.org/documentation/Context-Injection/
    \Hooks1.cs(78,95,78,118): warning CS0618: ‘ScenarioContext.Current’ is obsolete: ‘Please get the ScenarioContext via Context Injection – http://www.specflow.org/documentation/Context-Injection/
    Pages\Hooks1.cs(80,95,80,118): warning CS0618: ‘ScenarioContext.Current’ is obsolete: ‘Please get the ScenarioContext via Context Injection – http://www.specflow.org/documentation/Context-Injection/
    Pages\Hooks1.cs(82,94,82,117): warning CS0618: ‘ScenarioContext.Current’ is obsolete: ‘Please get the ScenarioContext via Context Injection – http://www.specflow.org/documentation/Context-Injection/
    1>C:\Users\StepDefinition\StepDefinition1.cs(17,38,17,45): warning CS0169: The field ‘StepDefinition1.context’ is never used

    getting the error .please help

  12. Hi Karthik,

    Thanks for the solution. it was really helpful. Need your help on a problem i am facing. how to display the messages or exception in extent reports using extenttest from the solution you provided?

  13. Hi Karthik Kk,
    Love your work, must say you have been an important part on my journey as an automation test analyst. Can you please redo this extent report video using the new version of extent report (v4.0) which will take care of the issue we are all facing with context injection and “pInfo” returning null. you will be doing the testing world an immeasurable favour as this issue as i’ve checked is affecting a whole lot of testers. looking forward to a great solution from you as you’ve always does. thanks very much in advance.

  14. Am using extent reporting with specflow. Has anyone ever come across this error :
    “I navigate to the site URL is not a valid BDD test. This happens when using AnalysisStrategy.BDD without BDD-style tests or if the test was not marked with the appropriate BDD type.”

    Throws : AventStack.ExtentReports.Core.InvalidAnalysisStrategyException:

  15. Hi Kartik, This write up was super helpful, would you know how can we attach the extent report results with screenshots to the Azure devops pipeline? I need to run my specflow scenarios through Azure pipelines and needed to attach the report with screenhots there

Leave a Reply

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