Wrong step name displayed in Extent Report And rep

2019-08-27 18:51发布

I am using the given code in spec flow to generate the extent report. Test execute and report generated successfully but wrong steps name displayed in the extent report, And in the extent report replace with When. Steps come under And displayed in When while in the feature file steps written in And section.

Steps written under And syntax in feature files, displaying under When in Extent report.

I am using the following code to generate extent report.

using AventStack.ExtentReports;
using AventStack.ExtentReports.Gherkin.Model;
using AventStack.ExtentReports.Reporter;
using AventStack.ExtentReports.Reporter.Configuration;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TechTalk.SpecFlow;

namespace ALD_Belgium
{
    [Binding]
    [TestFixture]
    class Hooks
    {
        public static ExtentTest featureName;
        public static ExtentReports extent;
        public static ExtentHtmlReporter htmlReporter;
        public static ExtentTest test;

        //  public static object Theme { get; private set; }

        static Hooks()
        {
            if (extent == null)
            {
                BasicSetUp();
            }

        }

        [BeforeFeature]
        public static void BeforeFeature()
        {
            featureName = extent.CreateTest<Feature>(FeatureContext.Current.FeatureInfo.Title);

        }

        [BeforeScenario]
        public static void Setup()
        {
            BasePage.Intitialize();
            BasePage.Navigate();
            //  test = extent.CreateTest(ScenarioContext.Current.ScenarioInfo.Title);
            test = featureName.CreateNode<Scenario>(ScenarioContext.Current.ScenarioInfo.Title);
        }

        [AfterScenario]
        public void TearDown()
        {
            if (ScenarioContext.Current.TestError != null)
            {
                var error = ScenarioContext.Current.TestError;
                var errormessage = "<pre>" + error.Message + "</pre>";

                extent.AddTestRunnerLogs(errormessage);
                test.Log(Status.Error, errormessage);
                test.Fail(errormessage);

            }
            BasePage.Quit();
        }

        [OneTimeSetUp]
        public static void BasicSetUp()
        {
            string pth = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
            // string pth = System.IO.Directory.GetCurrentDirectory();
            string actualPath = pth.Substring(0, pth.LastIndexOf("bin"));
            string projectPath = new Uri(actualPath).LocalPath;
            Console.WriteLine(" -----------Project Path--------------------------------------");
            Console.WriteLine(projectPath);
            string reportPath = projectPath + "Reports\\TestExecutionRunReport.html";
            // Console.WriteLine("Report Path is " + reportPath);


            htmlReporter = new ExtentHtmlReporter(reportPath);
            htmlReporter.Configuration().Theme = Theme.Dark;


            htmlReporter.Configuration().DocumentTitle = "SpecFlow Test Resport Document";

            htmlReporter.Configuration().ReportName = "Feature Run Results";

            extent = new ExtentReports();

            extent.AttachReporter(htmlReporter);
            //extent.LoadConfig(projectPath + "Extent-Config.xml");

        }


        [AfterTestRun]
        public static void EndReport()
        {
            extent.Flush();

        }

        [AfterStep]
        public static void InsertReportingSteps()
        {
            var stepType = ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString();


            if (ScenarioContext.Current.TestError == null)
            {
                if (stepType == "Given")
                    test.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.Text);
                else if (stepType == "When")
                    test.CreateNode<When>(ScenarioStepContext.Current.StepInfo.Text);
                else if (stepType == "And")
                    test.CreateNode<And>(ScenarioStepContext.Current.StepInfo.Text);
                else if (stepType == "Then")
                    test.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.Text);

            }
            else if (ScenarioContext.Current.TestError != null)
            {
                if (stepType == "Given")
                    test.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);
                else if (stepType == "When")
                    test.CreateNode<When>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);
                else if (stepType == "And")
                    test.CreateNode<And>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);
                else if (stepType == "Then")
                    test.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);

            }

        }

    }
}

enter image description here

1条回答
手持菜刀,她持情操
2楼-- · 2019-08-27 19:27

The problem maybe because in SpecFlow main keywords are -

Given, When, Then

Can be that this report takes steps descriptions not from Features file but from code itself, where method, I think, is written with "When" keyword.

查看更多
登录 后发表回答