Obtain scenario name and tags in Specflow test
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.
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:
1 2 3 4 5 6 7 8 9 10 |
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:
1 2 3 |
var title = ScenarioContext.Current.ScenarioInfo.Title; var tags = ScenarioContext.Current.ScenarioInfo.Tags; |
Easy as that.