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?

asked Jun 12, 2019 at 10:30
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);
IAmMilinPatel
7,7667 gold badges44 silver badges68 bronze badges
answered Nov 14, 2022 at 7:42
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.

answered Jun 13, 2019 at 8:09
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
}
Kate Paulk
31.5k8 gold badges56 silver badges109 bronze badges
answered Sep 13, 2019 at 10:01
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
}
answered Dec 31, 2023 at 11:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.