定义多个场景大纲的示例

问题描述 投票:3回答:1

在我们为我们的网络测试使用cucumber-jvm的项目中,我遇到了一个问题,到目前为止我无法解决: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?如果是这样,任何推荐?

cucumber cucumber-jvm gherkin
1个回答
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 hook中,将行写入报表。此示例假定您要独立于场景的成功或失败来编写行。

@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();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.