Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For loop gives all the list instead of one item in robot framework

I have this code:

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

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?

EDIT 1:

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

:FOR    ${item}     IN      @{ret_val}
like image 220
Zeinab Abbasimazar Avatar asked Dec 29 '25 05:12

Zeinab Abbasimazar


1 Answers

There are a couple of problems with your code

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

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}

Here is the output:

$ 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
==============================================================================
like image 185
Laurent Bristiel Avatar answered Jan 01 '26 15:01

Laurent Bristiel