I am using cucumber 4.3.0 and I would like to send an ArrayList of String in one of my sentences. It would give something like that:
Given I am on page <page>
When I do stuff
Then keywords "toto, tata, titi" are here
So far, I have thought of splitting the String "toto, tata, titi" into an Array (using "," as separator) however, I'm wondering if there are any cleaner solution?
I have tried doing this:
Given I am on page <page>
When I do stuff
Then the following keywords are here: toto, tata, titi
With
@Then("^the following keywords are here: (.*)$")
public void theFollowingKeywordsAreHere(List<String> datas) {
//some code
}
But I get the following error:
cucumber.runtime.CucumberException: Could not convert arguments for step ...
...
Caused by: io.cucumber.cucumberexpressions.CucumberExpressionException: ParameterType {anonymous} failed to transform [toto, tata, titi, tutu] to java.util.List<java.lang.String>
So, any idea on how to make that work without using the string split solution?
-
Is this answer helpful ? stackoverflow.com/questions/37820840/… Or this answer stackoverflow.com/questions/45033860/…SlightlyKosumi– SlightlyKosumi2019年06月12日 19:36:26 +00:00Commented Jun 12, 2019 at 19:36
-
@SlightlyKosumi This answer is what I'm looking for, but I wasn't able to make it work (it gives me the "Could not convert arguments" error)BelovedFool– BelovedFool2019年06月13日 07:45:18 +00:00Commented Jun 13, 2019 at 7:45
4 Answers 4
For me it works only with DataTable
. Like this:
@Then("following keywords are here")
public void theFollowingKeywordsAreHere(DataTable data) {
List<List<String>> dataList = data.asLists(String.class);
String data1 = dataList.get(0).get(0);
String data2 = dataList.get(0).get(1);
String data3 = dataList.get(0).get(2);
The only way to get a List
from the Gherkin
input is to use datatables.
To use this approach in your example you would have to re-implement the Then
step, like this:
...
Then following keywords are here
| toto | tata | titi |
With mapping step like this (notice there is no regular expression to match any keywords, just the step itself):
@Then("^following keywords are here$")
public void theFollowingKeywordsAreHere(List<String> data) {
//some code
}
This would normally accept DataTable
, but List<String>
will also work as cucumber can convert it.
You could try the following :
Then following keywords are here
| toto | tata | titi |
@Then("following keywords are here")
public void theFollowingKeywordsAreHere(List data) {
//some code
}
Try this layout in the feature file
Then the following keywords are here
| toto |
| tata |
| titi |
Then you should then be able to use this step method...
@Then("^the following keywords are here$")
public void theFollowingKeywordsAreHere(List<String> datas) {
//some code
}