I'm running a Unit test using SpecFlow in VS2013
I have a method called AfterStep which looks like this:
[AfterStep]
private static void AfterStep()
{
string attribute = ScenarioContext.Current.CurrentScenarioBlock.ToString();
string extractedValue = GetAttributeValue(attribute);
}
Here is the code for the GetAttributeValue method:
public static string GetAttributeValue(string attribute)
{
string attrValue = "";
StackTrace stackTrace = new StackTrace();
MethodBase method = stackTrace.GetFrame(1).GetMethod();
switch (attribute)
{
case "Given":
GivenAttribute givenAttr = (GivenAttribute)method.GetCustomAttributes(typeof(GivenAttribute), true)[0];
attrValue = givenAttr.Regex;
break;
case "When":
WhenAttribute whenAttr = (WhenAttribute)method.GetCustomAttributes(typeof(WhenAttribute), true)[0];
attrValue = whenAttr.Regex;
break;
case "Then":
ThenAttribute thenAttr = (ThenAttribute)method.GetCustomAttributes(typeof(ThenAttribute), true)[0];
attrValue = thenAttr.Regex;
break;
}
return attrValue;
}
But it doesnt seem to return the method that was called before the "AfterStep", ie " Step_ConnectToVMS".
Here is the example of the Step_ConnectToVMS step method:
[When(@"I open VMS and connect")]
public static void Step_ConnectToVMS(Table LoginDetails)
{
//Log into VMS
}
Ive noticed that it works if I call "GetAttributeValue" from within the Step_ConnectToVMS method itself. It just doesnt work if I call it in the "AfterStep" method.
What I essentially want the "AfterStep" method to grab is the the StackFrame for the method that's called before it (in this instance Step_ConnectToVMS) so that I can extract the attribute value.
With these values I can then use them in my custom results logger, which contains extra information (which i'm not at liberty to go into here) but what I really need are these step "descriptions" ie the attribute (Given, When Then) and the value ("I open VMS and connect") to form the headers for this log.
What I do when I get these values is I write them to an XML log, this log then gets picked up by an ASP.NET webserver and generates the page.
As I mentioned in the comments I have a system that works but it involved me putting "GetAttributeValue" in every step method I create. Where as I want it to sit in the "AfterStep" method so that its just in 1 place.