【问题标题】:Writing step definition in cucumber for a feature file with multiple scenarios在黄瓜中为具有多个场景的特征文件编写步骤定义
【发布时间】:2019-09-10 10:11:25
【问题描述】:

如何在黄瓜中为单个功能文件编写多个场景的步骤定义?

【问题讨论】:

  • 比如说我有一个包含 3 个场景的功能文件,在这我的步骤定义应该如何?我应该为所有场景编写一个步骤定义吗?或 3 个场景的 3 个步骤定义
  • 一步定义就够了。
  • 请参阅:How do I do X? SO 的期望是,提出问题的用户不仅会进行研究以回答他们自己的问题,还会分享该研究、代码尝试和结果。这表明您已经花时间尝试帮助自己,它使我们免于重复明显的答案,最重要的是它可以帮助您获得更具体和相关的答案!另见:How to Ask

标签: selenium-webdriver bdd cucumber-java


【解决方案1】:

来自Gherkin reference上的 Cucumber.io 页面:

Feature: Guess the word

  # The first example has two steps
  Scenario: Maker starts a game
    When the Maker starts a game
    Then the Maker waits for a Breaker to join

  # The second example has three steps
  Scenario: Breaker joins a game
    Given the Maker has started a game with the word "silky"
    When the Breaker joins the Maker's game
    And the Maker starts a game
    Then the Breaker must guess a word with 5 characters

在上面的示例中,the Maker starts a game 步骤将使用类似

的方式实现
@When("the Maker starts a game")
public void theMakerStartsAGame(){
    // implementation
}

在上面的示例中,步骤定义将映射到(识别)When the Maker starts a gameAnd the Maker starts a game 步骤。

该步骤可以根据需要在@CucumberOptions 下定义的文件夹内的所有功能文件中重复使用。

【讨论】:

  • 比如说我有一个包含 3 个场景的功能文件,在这我的步骤定义应该如何?我应该为所有场景编写一个步骤定义吗?或 3 个场景的 3 个步骤定义
  • 小黄瓜场景中的1步与1步定义有关。如果您已经创建了步骤定义,则无需再次创建它 - 您可以在其他场景中使用相同的步骤。这些步骤可重复使用。
  • @AshwiniArunachalam 在上面的示例中,步骤定义将映射到(识别)When the Maker starts a gameAnd the Maker starts a game 步骤。该步骤可以根据需要在@CucumberOptions 下定义的文件夹内的所有功能文件中重复使用。
【解决方案2】:
  1. 创建功能文件
  2. 确保在黄瓜运行器类中的 @CucumberOptions 中为特征和步骤定义类添加正确的路径,例如:

    @CucumberOptions( features = "src/test/resources/features", 胶水=“src/main/java/stepDefs” )

  3. 使用@Given、@When 等黄瓜注释在步骤定义包中编写您的步骤实现。

    @Given("计数 {int} 次") 公共无效计数方法(整数计数){ }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多