定义多个场景大纲的示例

时间:2015-05-06 11:11:39

标签: cucumber cucumber-jvm gherkin

在我们使用cucumber-jvm进行网络测试的项目中,我遇到了一个迄今为止我无法解决的问题:Se有几个Scenario Outline s应该全部使用相同的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使用某种工厂吗?如果是这样,任何推荐?

1 个答案:

答案 0 :(得分:0)

如果将这三个方案组合成一个方案,那么您想要实现的目标是微不足道的。将它们分成单独的场景可以

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

如果您愿意放弃前者并将这三种结合成一个方案,那么我可以建议一个支持后者的解决方案。 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();
        }
    }
}
相关问题