【问题标题】:Cucumber: loop through steps within scenarioCucumber:循环执行场景中的步骤
【发布时间】:2019-10-03 16:00:24
【问题描述】:

我正在使用 Java、Intellij、Selenium + Cucubmer 编写测试。 我有一个简单的测试场景:

  Scenario: Add X random products to the shopping cart

      Given I choose a random product from a list
      When I add random quantity of the product to the shopping cart
      Then I see that number of products in the cart was updated

在最后一步之后,我应该去第一步并重复所有过程 X 次。我不知道如何在不复制步骤的情况下在一个场景中实现它。是否可以循环运行步骤 1-3?

【问题讨论】:

    标签: java testing cucumber


    【解决方案1】:

    您可以将其转换为如下所示的虚拟ScenarioOutline,并将示例表扩展到您想要的任何时间。

      ScenarioOutline: Add X random products to the shopping cart
    
          Given I choose a random product from a list
          When I add random quantity of the product to the shopping cart
          Then I see that number of products in the cart was updated
    
        Examples:
        | id |
        | 1  |
        | 2  |
        | 3  |
    

    另一种方法是破解运行器代码,但这仅适用于您想要重复的单个场景。 How to execute same cucumber feature or scenario n times?

    【讨论】:

    • 我不想重复整个场景,而是想跳到 Given 步骤并从那里开始。为什么它在场景中如此重要。
    【解决方案2】:

    尝试使用以下

    Scenario: Add X random products to the shopping cart
    
          * configure retry = { count: <no-of-counts>, interval: <interval-between-each-retry-in-ms>}
          Given I choose a random product from a list
          And retry until <your-conditions>
          When I add random quantity of the product to the shopping cart
          Then I see that number of products in the cart was updated
    

    例子:

    Scenario: Add X random products to the shopping cart
    
          * configure retry = { count: X, interval: 0}
          Given I choose a random product from a list
          And retry until true
          When I add random quantity of the product to the shopping cart
          Then I see that number of products in the cart was updated
    

    这里我使用了计数 X,因为我必须添加 X 个随机产品并且不想在它们之间等待,所以间隔为 0 毫秒。我还使条件始终为真,因为我想在没有任何条件的情况下运行 X。您可以编写一些条件,例如 X==Y 或 X>Y 或任何其他产生布尔值的表达式。

    【讨论】:

    • 你能告诉我一个如何定义条件的例子吗?
    最近更新 更多