I'm using Cucumber and Java to write BDD tests.
I wanted to migrate from usung datatables in feature files, because I have a lot of fields and it's dublicated for a few steps, to using link to the table with values. From this one approach:
GIVEN I created account with
| name | type |
| test | basic |
to the approach:
GIVEN I created account with account.table
account.table file has value:
| name | type |
| test | basic |
I don't know how to parse 'path to the file with dataTable' to the actual 'dataTable'. I tried write step like this:
@GIVEN("^I created account with \"([^\"]*)\"$")
public void createAccount(DataTable dataTable) { ... }
But it's not working. Because there are no automatic convertion from path to file to dataTable. Here's the error:
cucumber.runtime.CucumberException: Don't know how to convert "resources\testdata\account.table" into cucumber.api.DataTable. Try writing your own converter: @cucumber.deps.com.thoughtworks.xstream.annotations.XStreamConverter(DataTableConverter.class)
Is there any examples of parsing it or any ideas?
Did it with following code, but wanted something easy and out of Cucumber box itself:
public void readFromExternalTable(String fileName) throws IOException {
List<String> lines = readLinesFromFile(fileName);
List<String> headers = getColumnsFromRow(lines.get(0));
List<String> values = getColumnsFromRow(lines.get(1));
DataTable table = createTable(headers, values);
}
static List<String> readLinesFromFile(String fileName) {
List<String> lines = new ArrayList<>();
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(fileName));
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return lines;
}
static List<String> getColumnsFromRow(String line) {
List<String> list = new ArrayList<>();
String[] row = line.split("\\|");
for (int i = 1; i < row.length; i++) {
list.add(row[i].trim());
}
return list;
}
static <String> DataTable createTable(List<String> headers, List<String> values) {
List<List<String>> rawTable = new ArrayList<>();
rawTable.add(headers);
rawTable.add(values);
return DataTable.create(rawTable);
}