Cucumber sending string list as parameter



  • 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?



  • 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 ``` will also work as cucumber can convert it.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2