3

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?

2

4 Answers 4

2

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);
6

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.

0

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

}
0

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
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.