1

I'm contributing to a framework called CodeSpec and it is built on top of SpecFlow.

Using the ScenarioContext I could get the currently executing scenario's title. but I want to get the step definitions too.

Say the scenario is

Scenario: Google for Cats
Given I navigate to "http://google.com"
And I enter value "Cats" to the "searchBox" with the "id" of "lst-ib"
And I click on element "searchButton" with the "xpath" of "id('sblsbb')/button" and wait "4" seconds
Then The page contains text pattern "The domestic cat is"

I could get the title with the following code,

[AfterScenario("UIAutomationReport")]
public static void AfterScenario()
{
    var title = ScenarioContext.Current.ScenarioInfo.Title;
   //title now is "Google for Cats"
    
}

I want to get the step definitions too like Given I navigate to "http://google.com", And I enter value "Cats" to the "searchBox" with the "id" of "lst-ib", etc.

How can i do this?

Community
  • 1
  • 1
Milindu Sanoj Kumarage
  • 2,714
  • 2
  • 31
  • 54

3 Answers3

3

You can't get this in the current version of specflow, but in the next release (v2) the information about the step text and step type will be available from the ScenarioStepContext class

you can get a beta build of the new version from the CI build nuget feed.

Sam Holder
  • 32,535
  • 13
  • 101
  • 181
2

You can use following code to get your step definition:

String title = ScenarioContext.Current.StepContext.StepInfo.Text; 

Console.WriteLine("Step Definition Title : " + title);
Mukul Varshney
  • 3,131
  • 1
  • 12
  • 19
1

You can find a caller name and attributes of caller

StackFrame frame = new StackFrame(1);

MethodBase method = frame.GetMethod();

message = String.Format("{0}.{1} : {2}", method.DeclaringType.FullName, method.Name, message);

Console.WriteLine(message);

See C# Method Caller

In atributes is Specflow step description

Community
  • 1
  • 1
Jan Regent
  • 21
  • 5