For循环提供了所有列表,而不是机器人框架中的一项 [英] For loop gives all the list instead of one item in robot framework

查看:52
本文介绍了For循环提供了所有列表,而不是机器人框架中的一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

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}

从Excel读取数据是我开发的其他关键字之一,并且效果很好.但我得到了以下输出:

Read Data From Excel is one of the other keywords which I have developed and it works fine; but I got this output:

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} 应该是列表中现有的项目之一,这意味着它应该是字典.因此,第二个打印应该是:

As you see, ${item} is exactly the same as ${ret_val}. I'm iterating over the list and ${item} should be one the items existing in the list which means it should be a dictionary. So the second print should be:

{'key1': 'val1', 'key2': 'val2'}

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

Any idea why the framework does not iterate and returns the whole list instead of the item?

我已经按照@Laurent Bristiel的回答解决了这个问题.我应该将list变量编写如下:

I have solved the issue according to @Laurent Bristiel's answer. I should write the list variable as below:

:FOR    ${item}     IN      @{ret_val}

推荐答案

您的代码有几个问题

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

1) when you do a FOR over a variable, use @{variable} instead of $(variable) See doc about loop in Robot User Guide.

2)您要遍历的arrary是一个具有单个元素的数组(一个字典),因此您只会得到一个元素(该字典)也许您想遍历索引的项目,值或键.为此,请参见收藏文档.这是我遍历字典所有项目的示例.

2) the arrary you are looping over is an array with a single element (a dict) so you will get only one element (the dict) Maybe you would like to loop over the items, values or keys of your index. See Collections documentation for that. Here is an example where I loop over all the items of the 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
==============================================================================

这篇关于For循环提供了所有列表,而不是机器人框架中的一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆