【问题标题】:Defining Examples for multiple Scenario Outlines为多个场景大纲定义示例
【发布时间】:2015-05-06 11:11:38
【问题描述】:

在我们使用 cucumber-jvm 进行 web 测试的项目中,我遇到了一个到目前为止我无法解决的问题:Se 有几个 Scenario Outlines 应该都使用相同的Examples。现在,我当然可以将这些示例复制到每个示例中,但是如果您可以执行以下操作,它会更短(并且可能更容易理解):

Background:
  Examples:
    | name  |
    | Alice |
    | Bob   |

Scenario Outline: Flying to the conference
  Given I'm flying to a confernce
  When I try to check in at the airport
  And my name is <name>
  Then I should get my plane ticket

Scenario Outline: Collecting the conference ticket
  Given I'm at a conference
  When I ask for the ticket for <name>
  And show my business card to prove my id
  Then I should get my conference ticket

Scenario Outline: Collectiong my personalized swag bag
  Given I'm at a conference
  When I go to the first booth
  And show them my conference ticket with the name <name>
  Then they'll give me a swag bag with the name <name> printed onto it

这样的事情可能吗?如果是这样,怎么做?我会按照here 的建议使用某种工厂吗?如果有,有什么推荐的吗?

【问题讨论】:

  • 您是否需要将整个功能运行两次,一次使用 alice,一次使用 bob,或者您可以在所需步骤中使用名称列表吗?
  • @Dude 这个想法是用每个名字运行整个功能。
  • 对所有用户运行所有场景的额外好处是什么?为什么不混合和匹配(一个用户运行一些,另一个用户运行一些)。如果用户 Bob 和 Alice 都应该通过所有场景,那么您通过为两个用户运行一个场景来真正测试什么?
  • 当然要看情况;虽然我不记得我问这个问题时的具体情况,但可能的原因是某些场景对于分配给他们的某些数据库中具有不同值的不同用户应该发挥相同的作用。在上面的例子中,也许我想确保女性和男性得到平等对待。或者那个年龄不会改变结果。

标签: cucumber cucumber-jvm gherkin


【解决方案1】:

如果您将这三个场景组合成一个场景,那么您想要实现的目标是微不足道的。将它们分解为不同的场景允许

  • 每个都独立运行(和失败)
  • 在报告中有单独的一行

如果您愿意放弃前者并将三者合二为一,那么我可以建议一个支持后者的解决方案。 Cucumber-jvm 确实支持将文本写入(After) hook via the Scenario object 的报告中。在您的步骤定义类中定义为类变量

private List<String> stepStatusList;

您可以在步骤定义的类构造函数中对其进行初始化。

this.stepStatusList = new ArrayList<String>();

在正式的三个场景的最后一步中,将文本添加到 stepStatusList 中,其中包含您希望在报告中显示的状态。

this.stepStatusList.add("Scenario sub-part identifier, some status");

在 After 钩子中,将行写入报告中。此示例假定您要独立于场景的成功或失败来编写行。

@Before
public void setup_cucumber_spring_context(){
    // Dummy method so cucumber will recognize this class as glue
    // and use its context configuration.
}

@After
public void captureScreenshotOnFailure(Scenario scenario) {

    // write scenario-part status into the report
    for (String substatus : this.stepStatusList) {
        scenario.write(substatus);
    }

    // On scenario failure, add a screenshot to the cucumber report
    if (scenario.isFailed() && webDriver != null) { 

        try {
            WebDriver augemented = new Augmenter().augment(webDriver);
            byte[] screenshot = ((TakesScreenshot) augemented).getScreenshotAs(OutputType.BYTES);

            scenario.embed(screenshot, "image/png");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

【讨论】:

    猜你喜欢
    • 2014-10-12
    • 1970-01-01
    • 2016-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 2019-12-15
    相关资源
    最近更新 更多