Step definitions
Each Scenario consists of multiple steps in a Given, When, Then (GWT) format. The definition of these steps are called Step definitions. These contain the actual Test steps that will execute and automate your application. Step definitions work very much like parts of test cases, in which you can also use groups with test steps.
Variable step definitions
One powerful feature in JOSF, is the use of variable step definitions. Assume the following scenario:
Scenario: Add two numbers
Given the calculation of "89 + 11"
When I press equals
Then the result should be "100"
In above example, we can implement the step definition by right clicking the step definition in the editor, and open the Step definition. From there you can implement the actual required steps to perform the calculation, but it will always depend on the step with the 89 + 11 value.
Now, if we’d like to reuse this step, but then for different values, we should consider parameterizing the Step Definition. We achieve this by replacing the values 89 + 11, with a variable, with the correct variable syntax. Example: ${variableName}.
This way, the ${calculation}
variable will be filled with the given value from the scenario, and is usable in the test steps of the Step Definition, just like in test cases.
Note that when a step definition hasn’t been implemented yet, the Feature editor will show a yellow line underneath the text, to note that this Step Definition is not implemented. While executing scenarios with unimplemented Step Definitions, JOSF will show a warning for each unimplemented Step Definition, but the scenario does not fail because of it.
Step arguments
One way to add more information into a step definition, is by adding a step argument. This requires a data table to be written underneath the step from your gherkin scenario, like so;
Scenario: Get some groceries
Given the basket contains these products
| Milk |
| Eggs |
| Bread |
The step definition has a step argument in the form of a list data table. There are six types of data table;
- List
- Mapping
- Multiple lists
- Multiple mappings
- Mapping for lists
- Cross mappings
Data table: List
The list is the most simplistic data table. It represents a list of words, that you may use in your test data. An example;
Scenario: Get some groceries
Given the basket contains these products
| Milk |
| Eggs |
| Bread |
In the step definition, we can extract the data with the ${table.#}
syntax. If you aren’t quite familiar with buffers, take a look at buffers and the Set keyword.
For example, if we need the Oranges data from the list, we can use ${table.2}
as it’s the item with the index of 2 (lists start at an index of 0)
# | Action name | Object | Data |
1 | type | name=searchbox | ${table.2} |
2 | verifyValue | name=searchbox | Bread |
But that’s not all. The power of lists really come into play when you combine them with a Repeating group
When using the Repeating group, with the repeater set at Data table, you can iterate over your list. JOSF will take care of getting the correct data out of your list, as long as you provide the right syntax; ${table.#}
(see, no index number, just the hashtag sign).
# | Action name | Object | Data |
1 | type | name=searchbox | ${table.#} |
2 | click | name=find | Bread |
JOSF will now iterate over your test steps within the Repeating group, as long as there is data. In this example, it will iterate four times!
Data table: Mapping
to be described
Data table: Multiple lists
to be described
Data table: Multiple mappings
to be described
Data table: Mapping for lists
to be described
Data table: Cross mapping
to be described
Repeating group
The Data Table repeater will only work in Step Definitions. If a step definition has a data table, you can execute a group iteratively, by selecting the Data Table repeater. Note that the type of data table, must be either list or multiple mappings.
When using the data table, JOSF will execute the group for each row in the table. To use values from the rows, instead of directly access the data by row numbers, replace the number by a hashtag (#) in the buffer name.
An example; List – using data from a LIST in a repeating group
From a Gherkin scenario, we have the following step with a list
Given I have a shopping list
| Apples |
| Pears |
| Oranges |
| Milk |
Usually, to access Apples, Pears, Oranges or Milk, we’d need to access the table buffer and append a row number, starting from 0. So Milk would be accessed by using ${table.0} and Oranges by using ${table.2}. By replacing the row number with a hashtag, JOSF will repeat the group for each row.
# | Action name | Object | Data |
1 | type | name=search-box | ${table.#} |
An example; List – using data from a LIST in a repeating group
From a Gherkin scenario, we have the following step with a list
Given these people go to the party
| first name | last name | occupation |
| John | Deere | Farmer |
| Tim | Cook | Traveler |
| Eric | Foreman | Legend |
Usually, to access these rows, we’d need to access the table buffer, append a row number, starting from 0, and append a column name. So John would be accessed by using ${table.0.first name} and Traveller by using ${table.1.occupation}. By replacing the row number with a hashtag, JOSF will repeat the group for each row.
# | Action name | Object | Data |
1 | type | name=first-name | ${table.#.first name} |
2 | type | name=last-name | ${table.#.last name} |
3 | type | name=occupation | ${table.#.occupation} |