Monday, April 13, 2020

Cucumber Selenium test automation framework using C# - Part 7 - Adding a test report with logs

Prerequisites


Please complete Part 6 of this tutorial before continuing.

Adding a test report using Extent Reports


1) Install 'ExtentReports' via Nuget Package Manager


Extent reporting is a html reporting tool which can be used with Cucumber. 

2) Creating the report and closing the report can be done in our Hooks.cs class. In the Hooks.cs class, add BeforeTestRun section.



3) Import following packages.

using AventStack.ExtentReports;
using AventStack.ExtentReports.Gherkin.Model;
using AventStack.ExtentReports.Reporter;

Add a extent-config.xml file to add configurations with following content.

<?xml version="1.0" encoding="UTF-8"?>
<extentreports>
  <configuration>
    <!-- report theme -->
    <!-- standard, dark -->
    <theme>standard</theme>

    <!-- document encoding -->
    <!-- defaults to UTF-8 -->
    <encoding>UTF-8</encoding>

    <!-- protocol for script and stylesheets -->
    <!-- defaults to https -->
    <protocol>https</protocol>

    <!-- title of the document -->
    <documentTitle>Test Automation Report</documentTitle>

    <!-- report name - displayed at top-nav -->
    <reportName>Test Automation Report</reportName>

    <!-- timestamp format -->
    <timeStampFormat>MMM dd, yyyy HH:mm:ss</timeStampFormat>

    <!-- custom javascript -->
    <scripts>
      <![CDATA[
                $(document).ready(function() {
                    
                });
            ]]>
    </scripts>

    <!-- custom styles -->
    <styles>
      <![CDATA[
                
            ]]>
    </styles>
  </configuration>
</extentreports>

4) Add the implementation as given below.



5) Add the closing of report to the AfterTestRun method.


6) Add feature information to the report


7) Add scenario information to the report


8) Add step information to the report

var stepType = ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString();
            PropertyInfo pInfo = typeof(ScenarioContext).GetProperty("ScenarioExecutionStatus", BindingFlags.Instance | BindingFlags.Public);
            MethodInfo getter = pInfo.GetGetMethod(nonPublic: true);
            object TestResult = getter.Invoke(ScenarioContext.Current, null);
            if (ScenarioContext.Current.TestError == null)
            {
                if (stepType == "Given")
                {
                    step = scenario.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString() + " " + ScenarioStepContext.Current.StepInfo.Text);
                }
                else if (stepType == "When")
                {
                    step = scenario.CreateNode<When>(ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString() + " " + ScenarioStepContext.Current.StepInfo.Text);
                }
                else if (stepType == "Then")
                {
                    step = scenario.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString() + " " + ScenarioStepContext.Current.StepInfo.Text);
                }
            }
            else if (ScenarioContext.Current.TestError != null)
            {
                if (stepType == "Given")
                {
                    step = scenario.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString() + " " + ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);
                }
                else if (stepType == "When")
                {
                    step = scenario.CreateNode<When>(ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString() + " " + ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);
                }
                else if (stepType == "Then")
                {
                    step = scenario.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString() + " " + ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);
                }
            }
            if (TestResult.ToString() == "StepDefinitionPending")
            {
                if (stepType == "Given")
                {
                    step = scenario.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString() + " " + ScenarioStepContext.Current.StepInfo.Text).Skip("Step Definition Pending");
                    step.Log(Status.Skip, "STEP DEFINITION PENDING");
                }
                else if (stepType == "When")
                {
                    step = scenario.CreateNode<When>(ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString() + " " + ScenarioStepContext.Current.StepInfo.Text).Skip("Step Definition Pending");
                    step.Log(Status.Skip, "STEP DEFINITION PENDING");
                }
                else if (stepType == "Then")
                {
                    step = scenario.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString() + " " + ScenarioStepContext.Current.StepInfo.Text).Skip("Step Definition Pending");
                    step.Log(Status.Skip, "STEP DEFINITION PENDING");
                }
            }

9) Add end report and end log statements to AfterTestRun method.


10) Add logs as needed in project


11) Build and run the tests. Navigate to location where report is created.


12) Open MyReport.html to view test report.

13) Open index.html to view the log.



No comments:

Post a Comment