为什么背景中的步骤不能与Behat中的示例结合使用

时间:2014-09-11 09:48:47

标签: automated-tests bdd behat mink gherkin

背景技术的步骤不能与实例相结合。这是对Behat的限制。

我有以下功能文件。

Background:
        Given I have the login Page
        When I login to the application using "<username>"
        Then the list is displayed
        When I select an item from the list
        Then I am taken to the Dashboard
        When I navigate to the Overview Page
        Then the Overview Page is displayed

@javascript @frontend @devlocal
Scenario Outline: To verify the Overview page content   
        Then overview page main headings are displayed
Examples:
        | username  | role          |
        | RoleUser  | ROLE_USER     |       
        | RoleAdmn  | ROLE_ADMIN    |

这会产生错误: [贝哈特\小黄瓜\异常\ ParserException]
  预期的评论或场景或大纲或步骤令牌,但在线获得示例:文件中的15:/var/Features/Overview.feature

我在“背景”中有这么多步骤的原因是因为这是到达页面的逻辑流程。我有几个使用这些步骤的场景。

我确实有一个解决此问题的工作解决方案,该解决方案正在使用“场景”中“背景”中的所有步骤。这绝对没问题。但是我有5个不同的场景用于此功能,如果我在所有5个场景中复制并粘贴相同的步骤,它看起来非常麻烦和麻烦。 我认为这是我们使用背景部分的主要原因吗?

以下是我现在正在使用的内容:

背景:             鉴于我有登录页面

@javascript @frontend @devlocal
Scenario: To verify the Overview page content   
        When I login to the application using "<username>"
        Then the list is displayed
        When I select an item from the list
        Then I am taken to the Dashboard
        When I navigate to the Overview Page
        Then the Overview Page is displayed
        Then overview page main headings are displayed
Examples:
        | username  | role          |
        | RoleUser  | ROLE_USER     |       
        | RoleAdmn  | ROLE_ADMIN    |

想象一下,对于所有不同的场景,从场景部分中获取所有这些步骤

1 个答案:

答案 0 :(得分:1)

如果没有情景大纲,那将是非常混乱和繁琐的。我可以与问题联系,但这只是它的工作方式。场景背景应该与功能中的所有场景相结合,并且应该作为独立块工作。如果您有五个方案大纲和一个方案,则您的功能将无效,因为后台无法知道如何处理<username>

理论上这可以相当容易实现,但是熟悉Behat代码,我觉得这将是一个非常非常大的变化......实际上我提出了this issue来提出一个新功能请求,有这样的东西会很高兴。您可以订阅并查看相关人员的意见。我几乎可以确信这个很好的因素不会赢得复杂性。

另一方面,没有什么可以阻止你将这个逻辑组合到另一个步骤并在那里传递必要的参数。您应该能够从mink访问其他上下文并将调用步骤作为标准方法。将该逻辑从该功能转移到上下文中会稍微清理一下,但imho这有点hacky并且不是更好的方法。事实上,我并不认为你现在拥有的东西有什么不妥。

另外,从逻辑角度来看,你试图在后台测试太多东西。相反,您可以创建另一个方案来测试背景中的所有内容,而其他五个方案在开头只有两个步骤而不是六个步骤(When I login to the application using "<username>"Then I successfully navigate the Overview Page is displayed)。