如何使用List< Class>类型迭代数据表在黄瓜 [英] How to Iterate Datatable with type List<Class> in Cucumber

查看:116
本文介绍了如何使用List< Class>类型迭代数据表在黄瓜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面具有给定注释的功能文件

I have below feature file with Given annotation

Given user have below credentials
|user       |password |
|cucumber1  |cucumber |
|cucumber2  |cucumber |

然后在数据模型下创建我

And i'm created below datamodel

public Class DataModel{
   public string user;
   public String password;
}

尝试按如下所示将数据提取到黄瓜步进定义中

Trying to fetch data into the cucumber stepdefinition as below

Public Class stepdefinition {
 @Given("^user have below credentials$")
   Public void user_have_below_credintials(List<DataModel> dm){

       //Iterator or foreach is required to fetch row,column data from dm
   }
}

请帮助我如何迭代对象'dm'以获取行和列值

Please help me how can I Iterate object 'dm' to get row and column values

推荐答案

// The old way
for (int i = 0; i < dm.size(); i++) {
    DataModel aDataModel = dm.get(i);
    String username = aDataModel.user;
    String password = aDataModel.password;
}

// A better way if java5+
for (DataModel aDataModel : dm) {
    String username = aDataModel.user;
    String password = aDataModel.password;
}

// another way if java8+
dm.forEach(aDataModel -> {
    String username = aDataModel.user;
    String password = aDataModel.password;
});

请注意,在我编写循环的变量之外,变量将不可用。只是演示了如何迭代和访问列表中每个DataModel的属性。

Note that the variables won't be available outside the loop with the way I wrote it. Just a demonstration of iterating and accessing the properties of each DataModel in your list.

要记住的一点是,您将DataModel对象的列表描述为数据表。但这不是一个表,它只是一个对象(包含列表)中包含的值的集合。您可能正在显示它,或者选择将其概念化为大脑中的数据表,但是代码所描述的模型并非如此,这意味着您不会像表那样遍历该表。一旦访问了行,列就没有定义的顺序,则可以按照想要产生相同效果的任何顺序来访问它们。

A thing to keep in mind is that you're describing your list of DataModel objects as a data table. But it's not a table, it's simply a collection of values contained in an object, which you have a list of. You may be displaying it, or choosing to conceptualize it as a data table in your head, but the model that your code is describing isn't that, which means you aren't going to iterate through it quite like a table. Once you access a "row", the "columns" have no defined order, you may access them in any order you want to the same effect.

这篇关于如何使用List&lt; Class&gt;类型迭代数据表在黄瓜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆