Cucumber Java - 迭代Java中的列表列表

时间:2017-05-28 11:54:19

标签: java cucumber-jvm

我有像这样的场景文件

 Scenario: Login Page with Valid credentials
    Given user is on Application landing page
    Then we verify following user exists
      | name    | email           | phone |
      | Shankar | san@email.com |   999 |
      | Ram     | ram@email.com   |   888 |
      | Sham    | sham@email.org  |   666 |

在我的步骤定义中,我想使用forforeach循环进行迭代。我尝试使用while循环。它工作正常。谁能告诉我如何使用for and foreach loop进行迭代?

@Then("^we verify following user exists$")
public void we_verify_following_user_exists(DataTable datatable)
        throws Throwable {
    List<List<String>> data = datatable.raw();

    Iterator<List<String>> it = data.iterator();

    while (it.hasNext()) {
        System.out.println(it.next());
    }

我期待下面的输出

name,email,phone
Shankar,san@email.com,999
Ram,ram@email.com,888 
Sham,sham@email.org,666 

3 个答案:

答案 0 :(得分:0)

你可以使用类似于:

的for循环进行迭代
{{1}}

答案 1 :(得分:0)

尝试一下:

@When("^User enters below credentials$")
    public void user_enters_below_credentials(DataTable credentials) throws Throwable {
        List<List<String>> list = credentials.raw();
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < 4; j++) {
                System.out.println(list.get(i).get(j));
            }

        }
    }

答案 2 :(得分:-1)

这应该对你有用

for(List<String> list: data){
        System.out.println(list.toString());
}