Reputation: 105
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!
Upvotes: 8
Views: 7786
Reputation: 123
You can use the keywords 'Scenario Outline' or 'Scenario Template':
Scenario Outline: eating
Given there are <start> cucumbers
When I eat <eat> cucumbers
Then I should have <left> cucumbers
Examples:
| start | eat | left |
| 12 | 5 | 7 |
| 20 | 5 | 15 |
DOC: https://cucumber.io/docs/gherkin/reference/#scenario-outline
Upvotes: 4
Reputation: 34327
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
Upvotes: 2
Reputation: 32817
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.
Upvotes: 8