Is there a “for each” or equivalent syntax for Ghe

2019-07-13 13:44发布

Is there any equivalent "for each" statement for Gherkin? In the following scenario, the page I am testing has multiple date fields that I'd like to run the same test examples on.

Here is the scenario that I would like to model.

Scenario Outline: Modify precision values for date controls

Given I have just added a record

 When I select <precision>

  And I select <value>

 Then <date> displays in the <date type> field

Examples:

  | date type | precision | value           | date                     |

  | Date 1    | Unknown   | N/A             | "Unknown"                | 

  | Date 1    | Year      | <current year>  | <current year>           |

  | Date 1    | Month     | <current month> | <current month, year>    |

  | Date 1    | Day       | <current day>   | <current month/day/year> | 

  | Date 2    | Unknown   | N/A             | "Unknown"                | 

  | Date 2    | Year      | <current year>  | <current year>           | 

  | Date 2    | Month     | <current month> | <current month, year>    | 

  | Date 2    | Day       | <current day>   | <current month/day/year> | 

Suppose there are 5 date type fields on the same page. It seems unnecessary to have to copy/psate 12 more rows in the table to cover Date 3 - Date 5. That's why I was wondering if there is a "for each" equivalent so that I can perform the same examples for each date type without having to explicitly show that in the Examples table. Or perhaps there is a different way I could structure the scenario?

Thanks for any assistance you can provide!

2条回答
冷血范
2楼-- · 2019-07-13 14:22

Cucumber is not designed to support multi-column iterations, but it is possible to make it work. Here, I want to try each combination of path and role:

  Scenario: cannot access paths
    When I access "path" as "role" then I should see an error
      | /path1 | user1 |
      | /path2 | user2 |
      | /path3 | user3 |

When(/^I access "path" as "role" then I should see an error$/) do |table|
  paths, roles = table.raw.transpose.map { |e| e.reject(&:blank?) }
  roles.each do |role|
    step "I am logged in as #{role}"
    paths.each do |path|
      p "#{role} user visiting #{path}"
      visit path
      step 'I should see the privileges error'
    end
  end
end
查看更多
萌系小妹纸
3楼-- · 2019-07-13 14:25

Nope, cucumber is not designed with looping in mind. Cucumber's scope is to allow defining application expectancies from a user perspective. And since real users don't "loop", it doesn't make sense to have it implemented in cucumber.

What can be done, programatically speaking, is to write a program that generates the same cucumber scripts for every combination in your application.

查看更多
登录 后发表回答