如何获取场景概要示例迭代号?

本文关键字:迭代 代号 何获取 获取 | 更新日期: 2023-09-27 18:03:32

我有以下测试,使用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 |

在每个场景之后,我将屏幕截图保存到一个文件中,该文件的命名基于scenario context . current . scenario info . 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      |

如果你查看生成的代码,那么<name>不会保存在任何地方,它不会出现在ScenarioContext或FeatureContext

报告生成器中的TestExecutionResults似乎有这个信息,Model.TestExecutionResults[]. testtemresult . testnode。Description是一个复合字符串,其中包含<name>元素;但它是如何到达那里的对我来说是个谜。

在规范运行程序生成的代码和报告生成器

中检查。

名称将不会出现,除非您已经在您的场景中定义,我可以看到您没有。你的特性可以这样写

  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 |

上面构造a的优点是,您可以利用相同的场景来执行几个操作,如"加","减","乘"等