Warning: fopen(/tmp/SghXTngBZPli-XH8KtS.tmp): failed to open stream: Disk quota exceeded in /home/executea/public_html/blog/wp-admin/includes/class-wp-filesystem-ftpext.php on line 139
Warning: unlink(/tmp/SghXTngBZPli-XH8KtS.tmp): No such file or directory in /home/executea/public_html/blog/wp-admin/includes/class-wp-filesystem-ftpext.php on line 142
Generating Extent Report with Specflow and Selenium C#
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
40 Replies to “Generating Extent Report with Specflow and Selenium C#”
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
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
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());
}
}
}
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?
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()
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.
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.
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.
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.
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
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.
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?
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.
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.”
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
Any update on this 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
I have a request wherein client want me not to mark fail cases which got failed due to no data in application as fail instead they wanted me to mark in some other status may be like warning or info how to achieve the same using BDD in c#.
Note : I developed my framework based on your BDD series pls help me on the same.
Thanks karthik is there any other way to achieve the same using ScenarioContext.Current as how we did for pass and fail classification in Hooks.cs file ?
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);
Hello Bishal,
Did you get the solution for above, if so pleas share
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);
Nopes, did not work for me 🙁
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
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());
}
}
}
Wow looks like an cool solution, I will try updating the post to latest version of Extent report then !
Thanks,
Karthik KK
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?
Hi Karthik,
For me i see that ScenarioStepContext is not appearing.
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);
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()
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.
Make it as Step pending, which will be skip
ScenarioContext.Current.Pending();
Hello kartik
How do I insert logs from the test in each step
Hi Karthik,
How can i insert Screenshot in extent report.
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.
You can use tags in scenarios which you can pass as an input in the command line to execute particular scenarios.
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 ?
Hi Sunil, please can you share your code for extent report generation for the new version of specflow?
For me i see that ScenarioStepContext is not appearing.
Please update your specflow to latest version
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
I guess with free version we dont have much control over that, but should be available in Pro version.
Thanks,
Karthik KK
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 ?
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.
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.
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
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.
Hi, thank you
Please let me know how to attach screenshot in each step in BDD Reports.
Kindly help me on this .
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
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?
You can catch the exception and show using this
ScenarioContext.Current.TestError.InnerException
Thanks,
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.
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:
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
Any update on this 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
Hi Karthik,
I have a request wherein client want me not to mark fail cases which got failed due to no data in application as fail instead they wanted me to mark in some other status may be like warning or info how to achieve the same using BDD in c#.
Note : I developed my framework based on your BDD series pls help me on the same.
Try using Assert.Warn instead https://docs.nunit.org/articles/nunit/writing-tests/Warnings.html
Thanks karthik is there any other way to achieve the same using ScenarioContext.Current as how we did for pass and fail classification in Hooks.cs file ?