AbdulQASDET
AbdulQASDET

Reputation: 61

How to add logs to Extent Report for Specflow C# BDD tests?

From Step Definitions, I am unable to add logs to Extent report

I see only Given When Then Description in Report.

Hooks.cs File:

[Binding]
public class GlobalHooks
{        
[ThreadStatic]
public static ExtentReports? extent;

[ThreadStatic]
public static ExtentTest? feature;

[ThreadStatic]
public static ExtentTest? scenario;

[ThreadStatic]
public static ExtentTest? step;

[BeforeTestRun]
public static async Task BeforeTestRun()
{
    string resPath = FileUtil.getFullFolderPath("Test Results");
    var spark = new ExtentSparkReporter(resPath + "/extent.html");
    spark.Config.ReportName = "Test Report";
    spark.Config.DocumentTitle = "Test Automation Report";
    spark.Config.Theme = Theme.Standard;

    extent = new ExtentReports();
    extent.AttachReporter(spark);
}

[BeforeFeature]
public static async Task BeforeFeature(FeatureContext featureContext)
{
    feature = extent?.CreateTest<Feature>(featureContext.FeatureInfo.Title);
}

[BeforeScenario]
public static async Task BeforeScenario(FeatureContext featureContext, ScenarioContext scenarioContext)
{
    scenario = feature?.CreateNode<Scenario>(scenarioContext.ScenarioInfo.Title);
}

TestFeatureStepDefinitions.cs

[Binding]
public class TestFeatureStepDefinitions : GlobalHooks
{
    [Given(@"some step (.*)")]
    public async Task GivenSomeStep()
    {
      step?.Log(Status.Info,"This step I want to log in extent report");
    }
}

enter image description here

Upvotes: 1

Views: 797

Answers (1)

Greg Burghardt
Greg Burghardt

Reputation: 18783

The SpecFlow Output API should get you what you need.

[Binding]
public class TestFeatureStepDefinitions : GlobalHooks
{
    private readonly ISpecFlowOutputHelper output;

    TestFeatureStepDefinitions(ISpecFlowOutputHelper output)
    {
        this.output = output;
    }

    [Given(@"some step (.*)")]
    public async Task GivenSomeStep()
    {
        output.WriteLine("This step I want to log in extent report");
    }
}

You can augment this API with your own extension methods to indicate a log level, but you will need to write that implementation yourself:

public static class SpecFlowOutputExtensions
{
    public static void WriteLine(this ISpecFlowOutputHelper output, Status status, string text)
    {
        // your own implementation here
        output.WriteLine($"{DateTime.Now} {status}: {text}");
    }
}

Then in your step definition:

output.WriteLine(Status.Info, "This step I want to log in extent report");

Upvotes: 1

Related Questions