簡體   English   中英

從SpecFlow中的場景獲取當前的步驟定義

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

我正在為一個名為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"

我怎樣才能做到這一點?

您無法在當前版本的specflow中獲得此功能,但在下一版本(v2)中,有關步驟文本和步驟類型的信息將可從ScenarioStepContext類中獲得。

您可以從CI build nuget feed中獲得新版本的beta版本。

您可以使用以下代碼來獲取步驟定義:

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

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

您可以找到呼叫者姓名和呼叫者屬性

StackFrame frame =新的StackFrame(1);

MethodBase方法= frame.GetMethod();

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

Console.WriteLine(message);

請參見C#方法調用者

歸功於Specflow步驟說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM