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

时间:2015-01-04 11:34:30

标签: 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}

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'}

任何想法为什么框架不迭代并返回整个列表而不是项目?

编辑1:

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

:FOR    ${item}     IN      @{ret_val}

1 个答案:

答案 0 :(得分:7)

您的代码存在一些问题

1)当您对变量执行FOR时,请使用@ {variable}而不是$(变量) 请参阅doc about loop in Robot User Guide

2)你循环的arrary是一个带有单个元素(dict)的数组,所以你只能获得一个元素(dict) 也许您想循环索引的项目,值或键。 请参阅Collections documentation。 这是一个循环遍及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}

这是输出:

$ 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
==============================================================================