19

I need to pass the List of strings from cucumber scenario which works fine as below

Scenario Outline: Verify some scenario 
Given something
When user do something 
Then user should have some "<data>" 
Examples: Some example
|data|
|Test1, Test2, Test3, Test4|

In the step definition I use List to retrieve the values of something variable. But when one of the value of data variable contains comma(,) e.g. Tes,t4 it becomes complex,since it considers "Tes" and "t4" as two different values

 Examples: Some example
 |something|
 |Test1, Test2, Test3, Tes,t4|  

So is there any escape character that i can use or is there is there any other way to handle this situation

Daniel Fintinariu
  • 2,733
  • 1
  • 14
  • 16
Yogiraj
  • 223
  • 1
  • 4
  • 15
  • Try escaping the required comma with '\'... – Grasshopper Jul 11 '17 at 15:26
  • i had tried this already, it doesn't work – Yogiraj Jul 12 '17 at 06:22
  • Can you add the step definition for this? I think you need to use the Transform annotation to create your own custom logic for escaping the comma. Split it with using a regular expression for ignoring comma immediately after the escape character... – Grasshopper Jul 12 '17 at 07:53
  • Take a look at this: http://blog.dupplaw.me.uk/articles/2019-07/cucumber-list-of-strings-parameter-type-in-java – vvill Nov 14 '19 at 13:37

6 Answers6

19

Found an easy way. Please see the below steps.

  • Here is my feature file.

    feature file

  • Here is the corresponding code to map feature step with code.

    code for the corresponding feature

  • Oh yes. Result is important. You can see the debug view.

    result in the debug view

SUMIT
  • 540
  • 1
  • 4
  • 19
11

This should work for you:

Scenario: Verify some scenario 
Given something
When user do something 
Then user should have following
| Test1 |
| Test2 |
| Test3 |
| Tes,t4| 

In Step definitions

Then("^user should have following$")
 public void user_should_have_following(List<String> testData) throws Throwable {
 #TODO user your test data as desired
 }
Ranjith's
  • 4,508
  • 5
  • 24
  • 40
2

In Transformer of TypeRegistryConfigurer, you can do this

@Override
public Object transform(String s, Type type) {
    if(StringUtils.isNotEmpty(s) && s.startsWith("[")){
        s = s.subSequence(1, s.length() - 1).toString();
        return Arrays.array(s.split(","));
    }
    return objectMapper.convertValue(s, objectMapper.constructType(type));
}
Mai Hữu Lợi
  • 559
  • 2
  • 8
  • 18
2

Examples:

Colors color-count
Red, Green 5
Yellow 8
def function("{colors}"):
context.object.colors = list(colors.split(","))
for color in context.object.colors:
    print(color)
1

Try setting the Examples in a column, like this:

| data   |
| Test1  |
| Test2  |
| Test3  |
| Tes,t4 |

This will run the scenario 4 times, expecting 'something' to change to the next value. First 'Test1', then 'Test2', etc.

In the step definition you can use that data like so:

Then(/^user should have some "([^"]*)"$/) do |data|
  puts data
end

If you want to use |Test1, Test2, Test3, Tes,t4|, change the ',' to ';' ex: |Test1; Test2; Test3; Tes,t4| and in the step definition split the data:

data.split("; ") which results in ["test1", "test2", "test3", "te,st"]

Converting the data to a List (in Java):

String test = "test1; test2; test3; tes,t4";
String[] myArray = test.split("; ");
List<String> myList = new ArrayList<>();
for (String str : myArray) {
    myList.add(str);
}
System.out.print(myList);

More on this here

Daniel Fintinariu
  • 2,733
  • 1
  • 14
  • 16
  • I need all these values together to execute a single scenario, this will not work in my case. – Yogiraj Jul 11 '17 at 13:45
  • You can separate `|Test1, Test2, Test3, Tes,t4|` with **';'**, and you would get `|Test1; Test2; Test3; Tes,t4|`. Now, in the step definition you can split the data like so: `data.split("; ")` and you will get an array of of 4 elements including the troublesome "Tes,t4" => result is: `["test1", "test2", "test3", "te,st"]` – Daniel Fintinariu Jul 11 '17 at 14:16
  • I tried this it doesn't work, since i am using List in step definition to retrieve these values. List considers comma(,) as a delimeter. hence it results in same issue. – Yogiraj Jul 12 '17 at 06:33
  • `String data = "test1; test2; test3; tes,t4"; String[] myArray = test.split("; "); List myList = new ArrayList<>(); for (String str : myArray) { myList.add(str); } System.out.print(myList);` – Daniel Fintinariu Jul 12 '17 at 08:18
  • More on this http://javadevnotes.com/java-array-to-list-examples if Java is what you need. – Daniel Fintinariu Jul 12 '17 at 08:27
-6

Don't put the data in your scenario. You gain very little from it, and it creates loads of problems. Instead give your data a name and use the name in the Then of your scenario

e.g.

 Then the user should see something

Putting data and examples in scenarios is mostly pointless. The following apply

  1. The data will be a duplication of what should be produced
  2. The date is prone to typos
  3. When the scenario fails it will be difficult to know if the code is wrong (its producing the wrong data) or the scenario is wrong (you've typed in the wrong data)
  4. Its really hard to express complex data accurately
  5. Nobody is really going to read your scenario carefully enough to ensure the data is accurate
diabolist
  • 3,990
  • 1
  • 11
  • 15