简体   繁体   English

从SpecFlow中的场景获取当前的步骤定义

[英]Get current Step definition from of a Scenario in SpecFlow

I'm contributing to a framework called CodeSpec and it is built on top of SpecFlow . 我正在为一个名为CodeSpec的框架做贡献,该框架建立在SpecFlow的基础上。

Using the ScenarioContext I could get the currently executing scenario's title. 使用ScenarioContext我可以获得当前正在执行的方案的标题。 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. 我也想获得步骤定义,就像“ Given I navigate to "http://google.com"And I enter value "Cats" to the "searchBox" with the "id" of "lst-ib"

How can i do this? 我怎样才能做到这一点?

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 您无法在当前版本的specflow中获得此功能,但在下一版本(v2)中,有关步骤文本和步骤类型的信息将可从ScenarioStepContext类中获得。

you can get a beta build of the new version from the CI build nuget feed . 您可以从CI build nuget feed中获得新版本的beta版本。

You can use following code to get your step definition: 您可以使用以下代码来获取步骤定义:

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

Console.WriteLine("Step Definition Title : " + title);

You can find a caller name and attributes of caller 您可以找到呼叫者姓名和呼叫者属性

StackFrame frame = new StackFrame(1); StackFrame frame =新的StackFrame(1);

MethodBase method = frame.GetMethod(); MethodBase方法= frame.GetMethod();

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

Console.WriteLine(message); Console.WriteLine(message);

See C# Method Caller 请参见C#方法调用者

In atributes is Specflow step description 归功于Specflow步骤说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM