How to get current step information in Specflow

We are trying to take screenshots for each step.

Everything works perfectly. But we cannot match the screenshots with the steps that created them.

We need something like FeatureContext.FeatureInfo and ScenarioContext.ScenarioInfo . But on an individual step level. So that we can mark the screenshots accordingly.

+4
source share
3 answers

EDIT

Added classes that show:

ScenarioStepContext.StepInfo.Text and ScenarioStepContext.StepInfo.StepDefinitionType

That should give you what you want.

Original answer This is currently not possible, although I just (yesterday) submitted a migration request that adds this functionality. If you are happy with the creation of specflow itself, then you can clone my repflow fork and switch to the ScenarioStepContext branch, then open TechTalk.Specflow_VS2013.sln and build the project yourself.

First you need to specify specflow the new version number for the nuget package. Open the SpecFlow.nuspec file and edit the version as something larger than the current version (I used 1.9.3.4), then create a solution (you need to install the VS2013 SDK and other VS VS files if you want to create these versions).

Once the solution is built, you will need to install vsix from

\SpecFlow\IdeIntegration\Vs2013Integration\bin\Debug\TechTalk.SpecFlow.Vs2013Integration.vsix

and then add the nuget package from

\SpecFlow\Installer\NuGetPackages\bin\SpecFlow.1.9.3.4.nupkg .

Once you do this, you can access ScenarioStepContext.StepInfo.Text and ScenarioStepContext.StepInfo.StepDefinitionType to be able to mark the items you want with the data for the current step.

We are currently using this, but please raise any issues in PR on the giptub Specflow main page and I will fix them if I can.

+6
source

Implementing your own LogTraceListener allows you to get the current definition of the step:

 public class LogTraceListener : ITraceListener { static public string LastGherkinMessage; public void WriteTestOutput(string message) { LastGherkinMessage = message; Console.WriteLine(message); } public void WriteToolOutput(string message) { Console.WriteLine(message); } } 

This needs to be registered in the SpecFlow section in App.Config:

 <specFlow> <trace listener="MyNameSpace.LogTraceListener, MyAssemblyName" /> <unitTestProvider name="NUnit" /> </specFlow> 

Having done this, the LastGherkinMessage property will contain only the text of the entered step. You can access this from a step definition or from another place.

+3
source

I was a bit late to the party, but this method will return a string containing the current text of the step:

 private static string GetCurrentPositionText() { int currentPositionText = 0; try { var frames = new StackTrace(true).GetFrames(); if (frames != null) { var featureFileFrame = frames.FirstOrDefault(f => f.GetFileName() != null && f.GetFileName().EndsWith(".feature")); if (featureFileFrame != null) { var lines = File.ReadAllLines(featureFileFrame.GetFileName()); const int frameSize = 20; int currentLine = featureFileFrame.GetFileLineNumber() - 1; int minLine = Math.Max(0, currentLine - frameSize); int maxLine = Math.Min(lines.Length - 1, currentLine + frameSize); for (int lineNo = currentLine - 1; lineNo >= minLine; lineNo--) { if (lines[lineNo].TrimStart().StartsWith("Scenario:")) { minLine = lineNo + 1; break; } } for (int lineNo = currentLine + 1; lineNo <= maxLine; lineNo++) { if (lines[lineNo].TrimStart().StartsWith("Scenario:")) { maxLine = lineNo - 1; break; } } for (int lineNo = minLine; lineNo <= maxLine; lineNo++) { if (lineNo == currentLine) { currentPositionText = lineNo - minLine; return String.Format("->" + lines[lineNo]); } } } } } catch (Exception ex) { Debug.WriteLine(ex, "GetCurrentPositionText"); } return String.Format("(Unable to detect current step)"); } 

Gaspar Nagy, taken from his post, made some time ago, when a similar question was asked, and changed slightly to return the line of the current step.

0
source

All Articles