Piyum Rangana
Piyum Rangana

Reputation: 71

How to get parameters of a step with multiple pipelines in cucumber for the java code?

I have below steps in my feature file for a scenario.

Given my_first_step

And my_second_step
  | Themes          | one | three |

  | Service Windows | two | four  |

And my_third_step

  | Create Apps |

  | Config      |

we can get parameters of 'my_third_step' as below in the java code as a list

public void my_third_step(List listOfItems) {}

but how can get parameters in 'my_second_step' ? I need to get a rows as array of elements in the java code. How can I do that ?

Upvotes: 0

Views: 1126

Answers (2)

Using Header we can implement Data Table in much clean & precise way and considering Data Table looks like below one -

And my_second_step
    | Heading_1        | Heading_2 | Heading_3 | 
    |  Themes          |    one    | three     |
    |  Service Windows |    two    | four      |

public void my_second_step(DataTable table) throws Throwable {

List<Map<String, String>> list = table.asMaps(String.class,String.class); 

System.out.println(list.get(0).get("Heading_1") + " : " + list.get(1).get("Heading_1"));
System.out.println(list.get(0).get("Heading_2") + " : " + list.get(1).get("Heading_2"));
System.out.println(list.get(0).get("Heading_3") + " : " + list.get(1).get("Heading_3"));

}

Upvotes: 0

burm87
burm87

Reputation: 848

You have to pass a list of objects, your object will look like

public class MyObject {
    private Integer themes;
    private Integer service;

    public Integer getThemes() {
       return this.themes;
    }

    public void setThemes(Integer themes) {
       this.themes = themes;
    }

    public Integer getService() {
       return this.service;
    }

    public void setService(Integer service) {
       this.service = service;
    }
}

Then you can pass a List<MyObject> to the method.

public void my_second_step(List<MyObject>) {
...
}

In the feature file change the definition as follows:

And my_second_step
  | Themes          | Service |
  | one             | two     |
  | three           | four    |

I hope this helps.

Upvotes: 1

Related Questions