javalistselenium-webdriverhashmaplinkedhashmap

How to read list of maps


I have a TestNG scenario method as

@Then("User Enters Data")
public void feedData(IDataReader dataTable) {
    List<Map<String,String>> data= dataTable.getAllRows();
 }

Data is being returned as a list of map as follows:

[{TextBox=TextValue-1,DropDown=DD-1, Description=Desc-1},
{TextBox=TextValue-2,DropDown=DD-2, Description=Desc-2},
{TextBox=TextValue-3,DropDown=DD-3, Description=Desc-3}]

Selenium script required to feed the above data in the UI. How to read this hence values can be feed to UI in a sequential order. Like

TextBox=TextValue-1,DropDown=DD-1, Description=Desc-1

Then click on save button.


Solution

  • Incase the data is being returned is a List of Map<String,String> as follows:

    [{TextBox=TextValue-1, DropDown=DD-1, Description=Desc-1}, {TextBox=TextValue-2, DropDown=DD-2, Description=Desc-2}, {TextBox=TextValue-3, DropDown=DD-3, Description=Desc-3}]
    

    To read the keys and values you can use the following solution:

    // System.out.println(list);
    // [{TextBox=TextValue-1, DropDown=DD-1, Description=Desc-1}, {TextBox=TextValue-2, DropDown=DD-2, Description=Desc-2}, {TextBox=TextValue-3, DropDown=DD-3, Description=Desc-3}]
    for (Map<String, String> map : list) {
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String key = entry.getKey();
            System.out.println(key);
            Object value = entry.getValue();
            System.out.println(value);
        }
    }
    

    Output:

    TextBox
    TextValue-1
    DropDown
    DD-1
    Description
    Desc-1
    TextBox
    TextValue-2
    DropDown
    DD-2
    Description
    Desc-2
    TextBox
    TextValue-3
    DropDown
    DD-3
    Description
    Desc-3