【问题标题】:Get current Step definition from of a Scenario in SpecFlow从 SpecFlow 中的场景获取当前步骤定义
【发布时间】:2016-02-04 20:02:38
【问题描述】:

我正在为一个名为 CodeSpec 的框架做出贡献,它建立在 SpecFlow 之上。

使用ScenarioContext 我可以获得当前正在执行的场景的标题。但我也想获得步骤定义。

说场景是

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"

我可以用下面的代码得到标题,

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

我也想获得步骤定义 Given I navigate to "http://google.com"And I enter value "Cats" to the "searchBox" with the "id" of "lst-ib"

我该怎么做?

【问题讨论】:

    标签: c# specflow


    【解决方案1】:

    你可以找到来电者姓名和来电者属性

    StackFrame 框架 = 新 StackFrame(1);

    MethodBase 方法 = frame.GetMethod();

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

    Console.WriteLine(消息);

    C# Method Caller

    在属性中是 Specflow 步骤描述

    【讨论】:

      【解决方案2】:

      您无法在当前版本的 specflow 中获得此信息,但在下一个版本 (v2) 中有关 step text and step type will be available from the ScenarioStepContext class 的信息

      您可以从CI build nuget feed 获取新版本的测试版。

      【讨论】:

      • 我不确定是否有发布时间表。但如果你从我上面链接的 nuget 提要中获得它,你可以使用测试版。
      • ScenarioContext.Current.StepContext.StepInfo.Text 有效。答案现在已经过时了
      【解决方案3】:

      您可以使用以下代码来获取您的步骤定义:

      String title = ScenarioContext.Current.StepContext.StepInfo.Text; 
      
      Console.WriteLine("Step Definition Title : " + title);
      

      【讨论】:

        猜你喜欢
        • 2016-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-25
        • 2023-01-31
        • 2014-12-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多