如何获得方案大纲示例的迭代编号

use*_*874 5 c# scenarios specflow

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

Scenario Outline: Verify query result
    Given I'm logged in
    When I enter "<query>"
    Then I should see the correct result

    Examples: 
    | query  |
    | Query1 |
    | Query2 |
    | Query3 |
Run Code Online (Sandbox Code Playgroud)

在每个方案之后,我将屏幕截图保存到基于ScenarioContext.Current.ScenarioInfo.Title命名的文件中。但是,我找不到区分这些迭代的好方法,因此屏幕截图被覆盖了。我可以在“示例”表中添加一列,但我想要一个更通用的解决方案...

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

Ben*_*ith 2

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

[When(@"I enter (.*)")]
public void WhenIEnter(string query)
{
 ScenarioContext.Current["query"] = query;
}
Run Code Online (Sandbox Code Playgroud)

然后在 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 ); 
}
Run Code Online (Sandbox Code Playgroud)