C# 如何获取场景大纲示例迭代编号

C# 如何获取场景大纲示例迭代编号,c#,specflow,scenarios,C#,Specflow,Scenarios,我使用Specflow、Selenium WebDriver和C#进行了以下测试: 场景大纲:验证查询结果 考虑到我已经登录 当我进入“ 那么我应该看到正确的结果 示例: |质疑| |问题1| |问题2| |问题3| 在每个场景之后,我将一个屏幕截图保存到一个基于ScenarioContext.Current.ScenarioInfo.Title命名的文件中。 然而,我找不到一个好方法来区分迭代,所以屏幕截图被覆盖了。我可以在示例表中添加一列,但我想要一个更通用的解决方案 有没有办法知道正在执

我使用Specflow、Selenium WebDriver和C#进行了以下测试:

场景大纲:验证查询结果
考虑到我已经登录
当我进入“
那么我应该看到正确的结果
示例:
|质疑|
|问题1|
|问题2|
|问题3|
在每个场景之后,我将一个屏幕截图保存到一个基于ScenarioContext.Current.ScenarioInfo.Title命名的文件中。 然而,我找不到一个好方法来区分迭代,所以屏幕截图被覆盖了。我可以在示例表中添加一列,但我想要一个更通用的解决方案


有没有办法知道正在执行哪个迭代?

在When步骤定义中,您可以在ScenarioContext中记录当前查询

[When(@"I enter (.*)")]
public void WhenIEnter(string query)
{
 ScenarioContext.Current["query"] = query;
}
然后,在您的AfterScenario步骤中,您可以检索该值,以确定刚刚运行的示例迭代

[AfterScenario]
void SaveScreenShot()
{
 var queryJustRun = ScenarioContext.Current["query"];

 // You could subsequently append queryJustRun to the screenshot filename to 
 // differentiate between the iterations
 // 
 // e.g. var screenShotFileName = String.Format("{0}_{1}.jpg",
 //                                ScenarioContext.Current.ScenarioInfo.Title,
 //                                queryJustRun ); 
}

据我所知,这是不可能的; 举个更清楚的例子

Scenario Outline: Add two numbers
    Given I have entered <first> into the calculator
    And I have entered <last> into the calculator
    When I press add
    Then the result should be <result> on the screen
Examples: 
| name   | first | last | result |
| simple | 1     | 1    | 2      |
| zero   | 0     | 0    | 0      |
场景大纲:添加两个数字
假设我已经输入了计算器
我已经输入了计算器
当我按add时
然后结果应该显示在屏幕上
示例:
|名称|第一|最后|结果|
|简单的| 1 | 1 | 2|
|零| 0 | 0 | 0|
如果查看生成的代码,则
不会保存在任何位置,它不会出现在ScenarioContext或FeatureContext上

报表生成器中的TestExecutionResults似乎确实包含以下信息:Model.TestExecutionResults[]。TestItemResult.TestNode.Description是一个复合字符串,其中包含
元素;但它是如何到达那里的对我来说是个谜


这在spec runner生成的代码和报告生成器中进行了检查,名称将不会出现,除非您在场景中进行了定义,我可以看到您没有这样做。你的特写可以这样写

  Scenario Outline: Performing mathematical operation
    Given I am on <name> scenario
    And I have entered <first> into the calculator
    And I have entered <last> into the calculator
    When I do the <operation> 
    Then the result should be <result> on the screen
Examples: 
| name   | first | last | result | operation |
| simple | 1     | 1    | 2      | add       |
| subs   | 6     | 2    | 4      | substract |
场景大纲:执行数学运算
考虑到我的情况
我已经输入了计算器
我已经输入了计算器
当我做这件事的时候
然后结果应该显示在屏幕上
示例:
|名称|第一个|最后一个|结果|操作|
|简单| 1 | 1 | 2 |相加|
|subs | 6 | 2 | 4 | substract|

构建上述结构的优点是,您可以利用同一场景执行多个操作,如“加”、“减”、“乘”等

这会丢失有关您的问题的大量信息。您是否在SQL语句中?C#?
  Scenario Outline: Performing mathematical operation
    Given I am on <name> scenario
    And I have entered <first> into the calculator
    And I have entered <last> into the calculator
    When I do the <operation> 
    Then the result should be <result> on the screen
Examples: 
| name   | first | last | result | operation |
| simple | 1     | 1    | 2      | add       |
| subs   | 6     | 2    | 4      | substract |