Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we retrieve/get the feature and scenario title in step definitions?

How can we retrieve/get feature, scenario title and tag name in step definitions?

For example I have a feature file booksearch with a feature:

Feature: Book Search
Scenario: Title should be matched

I perform a simple search on 'abc'
------------------------
------------------------


    [When(@"I perform a simple search on '(.*)'")]
    public void WhenIPerformASimpleSearchOn(string searchTerm)
    {
        --------
        ----------
        //custom log    
        WriteLogs(int stepNum,string scenarioName,string tagname,string stepDescription,string stepResult)
    }

How can we retrieve/get Feature and scenario title and tag name in step definitions for given scenario?

We are using MSTest as unit test provider.

like image 414
shiv Avatar asked Jan 12 '23 20:01

shiv


2 Answers

You can retrieve the feature and scenario title by querying the FeatureInfo and ScenarioInfo classes.

For example, placing the following code in you step definition (i.e. WhenIPerformASimpleSearchOn() ):

var featureTitle = FeatureContext.Current.FeatureInfo.Title;
var featureTags  = FeatureContext.Current.FeatureInfo.Tags;
var featureDescription = FeatureContext.Current.FeatureInfo.Description;    

var scenarioTitle = ScenarioContext.Current.ScenarioInfo.Title;
var scenarioTags  = ScenarioContext.Current.ScenarioInfo.Tags;

Will retrieve the feature title, tags and description as well as the scenario title and tags.

like image 50
Ben Smith Avatar answered Jan 14 '23 10:01

Ben Smith


They are part of the context, you'll probably need to look at both the ScenarioContext and the FeatureContext to get the details you want.

like image 38
AlSki Avatar answered Jan 14 '23 09:01

AlSki