首页 > 解决方案 > 有没有办法用黄瓜中的不同数据集一个一个地重复执行几个步骤

问题描述

考虑一个场景 第 1 步:网站中可用的过滤器很少 第 2 步:我们必须从每个过滤器中选择值 第 3 步:然后根据应用的过滤器验证显示的数据

预期的:

我已经尝试使用数据表从过滤器中选择值,但它是一个一个地选择所有数据,并且验证仅在最后发生而不是在选择每个值之后发生所以有没有办法选择和验证然后选择和验证像这样

标签: cucumberbddcucumber-java

解决方案


您必须使用示例表而不是数据表来使用场景大纲。如果您想使用不同的值执行相同的场景,那么您需要创建一个场景大纲,并且在示例表中您需要传递与过滤器相关的数据。

这就是你可以实现的方式。

特点

Feature: Title of your feature
  I want to use this template for my feature file


  Scenario Outline: Title of your scenario outline
    Given I select a value from the "<filters>"
    When I check for the filter in step
    Then I verify the filter in step

    Examples: 
      | filters  |
      | Data1   |
      | Data2   |
      | Data3   |

步骤定义:

boolean result = false;
    String filter = null;
    List<String> expectedFilters = new ArrayList<>();
    {
        expectedFilters.add("Data1");
        expectedFilters.add("Data2");
        expectedFilters.add("Data3");
    }




    @Given("I select a value from the {string}")
    public void i_select_a_value_from_the_filters(String filter)
    {
        result = false;
        this.filter = filter;
    }

    @When("I check for the filter in step")
    public void i_check_for_the_filter_in_step()
    {
        if( this.expectedFilters.contains(this.filter))
        {
            result = true;
        }
    }

    @Then("I verify the filter in step")
    public void i_verify_the_filter_in_step()
    {
        if( result )
        {
            System.out.println("Validation is successful for data [ " + this.filter + " ]" );
        }
        else
        {
            System.out.println("Validation failed!");
        }
    }

推荐阅读