简体   繁体   English

如何在C#对象中获取Specflow方案

[英]How to get the specflow scenarios in c# objects

I want to read each feature file and create objects for each scenario. 我想阅读每个功能文件并为每种情况创建对象。 After reading the .feature file. 读取.feature文件后。 I should get objects like below: 我应该得到如下对象:

Input 输入项

@mytag  
Scenario: Add two numbers  
    Given I have entered 50 into the calculator  
    And I have entered 70 into the calculator  
    When I press add  
    Then the result should be 120 on the screen

Expected Output 预期产量

Scenarios - Gives all the scenarios in feature file. Scenarios -在功能文件中提供所有方案。
Scenario.Steps - Gives all the Given when then of that scenarios. Scenario.Steps给出当时所有的“给定”场景。
Scenario.Examples - Gives all the examples. Scenario.Examples提供所有示例。
Scenarios.Tags - All the tags Scenarios.Tags所有标签

Code

var lines = File.ReadAllText(@"P:\Test.feature");
var scenarios = lines.Split(new string[] { "Scenario: "}, StringSplitOptions.RemoveEmptyEntries);
var scenarioList = new List<Scenario>();
for (int i = 1; i < scenarios.Length; i++)
{
    var ind = scenarios[i].IndexOf("\n");

    var scenario = new Scenario();
    scenario.Name = scenarios[i].Substring(0, ind);
    var toInd=scenarios[i].IndexOf("@");
    if(toInd>1)
        scenario.Steps = scenarios[i].Substring(ind,toInd);
    else
        scenario.Steps = scenarios[i].Substring(ind);
    scenarioList.Add(scenario);
}

Gherkin Parser should do what you're expecting. Gherkin Parser应该可以完成您的期望。 You can get it at Nuget gallery 您可以在Nuget画廊购买

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

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