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

问题描述 投票:0回答:3

我有这样的场景文件

 Scenario: Login Page with Valid credentials
    Given user is on Application landing page
    Then we verify following user exists
      | name    | email           | phone |
      | Shankar | [email protected] |   999 |
      | Ram     | [email protected]   |   888 |
      | Sham    | [email protected]  |   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,[email protected],999
Ram,[email protected],888 
Sham,[email protected],666 
java cucumber-jvm
3个回答
0
投票

您可以使用for循环进行迭代,类似于:

for (List<String>currentData : data) {
    org.apache.commons.lang3.StringUtils.join(currentData, ",")
    System.out.println(System.getProperty("line.separator"));
}

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));
            }

        }
    }

-1
投票

这应该适合你

for(List<String> list: data){
        System.out.println(list.toString());
}
© www.soinside.com 2019 - 2024. All rights reserved.