Cucumber Java - 遍历 Java 中的列表列表



我有这样的场景文件

 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 
你可以

迭代类似于以下内容的 for 循环:

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

试试这个:

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

这应该适合您

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

最新更新