Obtain scenario name and tags in Specflow test

Posted by on May 4, 2016 in Asp.net, WebDev

I was recently tasked with obtaining the tag name(s) and title of a Specflow feature within its step definition file (C# code). It turns out this is quite simple to do and exposed naturally within the step definitions through the TechTalk.SpecFlow.ScenaroContext class. ScenarioContext.Current 1 ScenarioContext.Current exposes the current scenario context, from which a ScenarioInfo property is declared. Quite conveniently, this class definition contains a string array of tags and the title, all neatly wrapped up in a single, consice class: namespace TechTalk.SpecFlow { public class ScenarioInfo { public ScenarioInfo(string title, params string[] tags); public string[] Tags { get; } public string Title { get; } } } 12345678910 namespace TechTalk.SpecFlow{    public class ScenarioInfo    {        public ScenarioInfo(string title, params string[] tags);         public string[] Tags { get; }        public string Title { get; }    }} Therefore, anywhere in your current stepdefinition you should be able to get the title and the tags using: var title = ScenarioContext.Current.ScenarioInfo.Title; var tags = ScenarioContext.Current.ScenarioInfo.Tags; 123 var title = ScenarioContext.Current.ScenarioInfo.Title; var tags = ScenarioContext.Current.ScenarioInfo.Tags; Easy as...

Read More »