【问题标题】:How to call a feature inside another feature in cucumber-jvm?如何在 cucumber-jvm 的另一个功能中调用一个功能?
【发布时间】:2012-04-24 03:17:26
【问题描述】:

我有一个功能文件

Feature: Create Profile

Scenario: Create Profile
Given I want to create a profile
When I create a profile
Then I should be navigated to Home Page
Then sign out link should exist

所以上述运行一切正常,并断言它确实回到了主页中并且存在退出链接。

现在我有了另一个功能文件。

Feature: Go to my account page

Scenario: Go to my account page
Given I want to go to my account page    
When I go to my account page
Then I should be navigated to the my account page

在运行"When I go to my account page" 步骤之前,用户应该"Create Profile"

所以我所做的就是添加了

When I create a profile
Then I should be navigated to Home Page
Then sign out link should exist

When I go to my account page之前。

但我发现我复制了“创建配置文件”功能/场景中的相同代码。

如何在“转到我的帐户页面”场景中运行整个“创建个人资料”功能/场景?

我正在使用带有 Selenium 和 JUnit 的 cucumber-jvm。

【问题讨论】:

  • 您可能需要创建一个名为 createOrVerifyProfile 的方法,该方法将调用与功能 CreateProfile 或功能 VerifyProfile 关联的所有方法

标签: selenium junit cucumber cucumber-jvm


【解决方案1】:

您看到背景 DSL 功能了吗?它适用于您的情况,但可能不是您真正要问的。在这种情况下,您可以要求用户通过以下方式创建配置文件:

Feature: Create Profile
  Background:
    Given I create a profile
    And I should be navigated to Home Page
    And sign out link should exist

  Scenario: Create Profile
    # do nothing because all actions are in background

  Scenario: Go to my account page
    When I go to my account page
    Then I should be navigated to the my account page

但是你必须将你的两个功能文件合并到一个单独的功能文件中。

还可以查看 @Before 和 @After 黄瓜注释 - 如果以前的解决方案不适合您,您可以运行一些代码来初始化(或创建)测试帐户。

【讨论】:

  • 是否可以链接到“背景:”中的单独功能文件。那将是完美的。我认为“@Before”和“@After”也无法运行功能文件。
【解决方案2】:

Create Profile 功能指定 Given/When/Then,因为这是驱动功能实现的原因。

许多其他功能都需要有效的配置文件。您不应通过重复 Create Profile 的规范来为这些功能创建配置文件。相反,包含类似于Given I have a valid profile 的内容(可能在背景部分)并将其连接到创建配置文件的夹具代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    • 2021-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多