I've searched a bit for this but can't find an exact example. I have a form to fill in as a step. The form fields look something like this:
Date:
Currency:
Total:
Description:
But not all of the fields are required to have data entered by the user. Instead of writing several methods to account for the different combinations, like so:
(When I enter the 'Date' and 'Currency' and 'Total' and 'Description')
(When I enter the 'Date' and 'Total')
(When I enter the 'Currency' and 'Description')
etc...
I'd like to somehow implement something like this, instead, in the feature file:
When I enter the following details:
|Date |x |
|Currency |USD |
|Total |100 |
|Description |Test |
And then have a single method to handle any combination of data the user enters in that second column.
I've found sites that have this data table-driven example:
When I enter the following details:
|Date |<date> |
|Currency |<currency> |
|Total |<total> |
|Description |<description> |
Example data:
|date |currency |total |description |
|x |USD |100 |foo |
|y |EUR |200 |test |
|z |HKD |124 |bar |
But that's not what I am after. I don't need to iterate through a list of predetermined example data. I hope I've summarized the problem clearly enough and someone knows a good place to go find an example of this kind of implementation. Thanks for any advice!
Yes, you can use a data table as an argument to a single non-repeated step. The first row of the data table must be a header:
When I enter the following details:
|Name |Value|
|Date |x |
|Currency |USD |
|Total |100 |
|Description |Test |
Here's one possible way to use it in a step:
@Given("^I enter the following details:$")
public void i_enter_the_following_details(Map<String, String> details) throws Throwable {
for Map.Entry<String, String> entry : details.entrySet() {
String key = entry.getKey();
String value = entry.getValue();
switch (key) {
case "Date":
// add the date to the form
break;
// ...
}
}
}
You can also get the table as a DataTable
, a List
of value objects, a List<List<String>>
or a List<Map<String>>
by declaring the parameter with that type. Map<String, String>
seems easiest here.
I wrote the example this way because I'm assuming that you need to write different code to put each value in its field. If the code is the same for every field you might be able to just put the field's CSS selector in the data table and get rid of the switch.