如何阅读地图列表[关闭]

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

我有一个 TestNG 场景方法

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

数据作为地图列表返回,如下所示:

[{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 脚本需要在 UI 中提供上述数据。如何读取此值,因此可以按顺序将值提供给 UI。喜欢

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

然后点击保存按钮。

java list selenium-webdriver hashmap linkedhashmap
1个回答
0
投票

如果返回的数据是

List
Map<String,String>
,如下所示:

[{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}]

要阅读

keys
values
,您可以使用以下解决方案:

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

输出:

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
© www.soinside.com 2019 - 2024. All rights reserved.