For循环给出了所有列表而不是机器人框架中的一个项目

Zei*_*zar 1 for-loop robotframework

我有这个代码:

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}
Run Code Online (Sandbox Code Playgroud)

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
==============================================================================
Run Code Online (Sandbox Code Playgroud)

如你所见,${item}完全一样${ret_val}.我正在迭代列表,${item}应该是列表中存在的项目之一,这意味着它应该是一个字典.所以第二次印刷应该是:

{'key1': 'val1', 'key2': 'val2'}
Run Code Online (Sandbox Code Playgroud)

知道为什么框架不迭代并返回整个列表而不是项目?

编辑1:

我根据@Laurent Bristiel的回答解决了这个问题.我应该写下列表变量如下:

:FOR    ${item}     IN      @{ret_val}
Run Code Online (Sandbox Code Playgroud)

Lau*_*iel 7

您的代码存在一些问题

1)当您对变量执行FOR时,请使用@ {variable}而不是$(变量)请参阅" 机器人用户指南"中有关循环的文档.

2)你正在循环的arrary是一个带有单个元素(dict)的数组,所以你只能获得一个元素(dict).也许你想循环索引的项目,值或键.请参阅集合文档.这是一个循环遍及dict所有项目的例子.

*** 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}
Run Code Online (Sandbox Code Playgroud)

这是输出:

$ 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
==============================================================================
Run Code Online (Sandbox Code Playgroud)