For循环给出所有列表而不是机器人框架中的一个项目
For loop gives all the list instead of one item in robot framework
我有这个代码:
Test Check For Loop
@{ret_val} = Read Data From Excel ${filename} ${sheetname}
log to console ${ret_val}
:FOR ${item} IN ${ret_val}
\ log to console ${item}
Read Data From Excel
是我开发的其他关键字之一,效果很好;但我得到了这个输出:
Test Check For Loop [{'key1': 'val1', 'key2': 'val2'}]
[{'key1': 'val1', 'key2': 'val2'}]
| PASS |
------------------------------------------------------------------------------
Scenario.scenario | PASS |
0 critical tests, 0 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Scenario | PASS |
0 critical tests, 0 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
如您所见,${item}
与 ${ret_val}
完全相同。我正在遍历列表, ${item}
应该是列表中存在的项目之一,这意味着它应该是一本字典。所以第二个打印应该是:
{'key1': 'val1', 'key2': 'val2'}
知道为什么框架不迭代 returns 整个列表而不是项目吗?
编辑 1:
我已经按照@Laurent Bristiel的回答解决了这个问题。我应该如下编写列表变量:
:FOR ${item} IN @{ret_val}
您的代码有几个问题
1) 对变量执行 FOR 时,使用 @{variable} 而不是 $(variable)
参见 doc about loop in Robot User Guide。
2) 你循环的数组是一个只有一个元素的数组(字典)所以你只会得到一个元素(字典)
也许您想遍历索引的项目、值或键。
请参阅 Collections documentation。
这是一个循环遍历字典所有项目的示例。
*** Settings ***
Library Collections
*** Test Cases ***
Test Check For Loop
# @{ret_val} = Read Data From Excel ${filename} ${sheetname}
# this creates something like: [{'key1': 'val1', 'key2': 'val2'}]
# let's mock this keyword and build the dict of array ourselves
${dict} = create dictionary key1 val1 key2 val2
@{ret_val} = create list ${dict}
log to console ${\n}Object we want to parse: ${ret_val}
# This shows: [{u'key2': u'val2', u'key1': u'val1'}]
# which is an array with only 1 element, which is a dict
:FOR ${item} IN @{ret_val}
\ log to console Parsing with FOR over the array: ${item}
# whith this you get your only element
${dict} = get from list ${ret_val} 0
${items} = Get Dictionary Items ${dict}
log to console Parsing with FOR over the dict content
:FOR ${item} IN @{items}
\ log to console Item: ${item}
这是输出:
$ pybot for.robot
==============================================================================
For
==============================================================================
Test Check For Loop
Object we want to parse: [{u'key2': u'val2', u'key1': u'val1'}]
Parsing with FOR over the array: {u'key2': u'val2', u'key1': u'val1'}
Parsing with FOR over the dict content
Item: key1
Item: val1
Item: key2
Item: val2
Test Check For Loop | PASS |
------------------------------------------------------------------------------
For | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
我有这个代码:
Test Check For Loop
@{ret_val} = Read Data From Excel ${filename} ${sheetname}
log to console ${ret_val}
:FOR ${item} IN ${ret_val}
\ log to console ${item}
Read Data From Excel
是我开发的其他关键字之一,效果很好;但我得到了这个输出:
Test Check For Loop [{'key1': 'val1', 'key2': 'val2'}]
[{'key1': 'val1', 'key2': 'val2'}]
| PASS |
------------------------------------------------------------------------------
Scenario.scenario | PASS |
0 critical tests, 0 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Scenario | PASS |
0 critical tests, 0 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
如您所见,${item}
与 ${ret_val}
完全相同。我正在遍历列表, ${item}
应该是列表中存在的项目之一,这意味着它应该是一本字典。所以第二个打印应该是:
{'key1': 'val1', 'key2': 'val2'}
知道为什么框架不迭代 returns 整个列表而不是项目吗?
编辑 1:
我已经按照@Laurent Bristiel的回答解决了这个问题。我应该如下编写列表变量:
:FOR ${item} IN @{ret_val}
您的代码有几个问题
1) 对变量执行 FOR 时,使用 @{variable} 而不是 $(variable) 参见 doc about loop in Robot User Guide。
2) 你循环的数组是一个只有一个元素的数组(字典)所以你只会得到一个元素(字典) 也许您想遍历索引的项目、值或键。 请参阅 Collections documentation。 这是一个循环遍历字典所有项目的示例。
*** Settings ***
Library Collections
*** Test Cases ***
Test Check For Loop
# @{ret_val} = Read Data From Excel ${filename} ${sheetname}
# this creates something like: [{'key1': 'val1', 'key2': 'val2'}]
# let's mock this keyword and build the dict of array ourselves
${dict} = create dictionary key1 val1 key2 val2
@{ret_val} = create list ${dict}
log to console ${\n}Object we want to parse: ${ret_val}
# This shows: [{u'key2': u'val2', u'key1': u'val1'}]
# which is an array with only 1 element, which is a dict
:FOR ${item} IN @{ret_val}
\ log to console Parsing with FOR over the array: ${item}
# whith this you get your only element
${dict} = get from list ${ret_val} 0
${items} = Get Dictionary Items ${dict}
log to console Parsing with FOR over the dict content
:FOR ${item} IN @{items}
\ log to console Item: ${item}
这是输出:
$ pybot for.robot
==============================================================================
For
==============================================================================
Test Check For Loop
Object we want to parse: [{u'key2': u'val2', u'key1': u'val1'}]
Parsing with FOR over the array: {u'key2': u'val2', u'key1': u'val1'}
Parsing with FOR over the dict content
Item: key1
Item: val1
Item: key2
Item: val2
Test Check For Loop | PASS |
------------------------------------------------------------------------------
For | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================