简体   繁体   中英

Iterate through list returned by python keyword in robot framework

I have a function in my (python) keyword library that returns a list, but when I pass it into the robot testcase, I get the error "Value of variable @{LIST} is not list or list-like". Here is my code

Robot code:

Generate Data
    @{LIST}=    Create Data 

Do Thing For All Values In List
    :FOR    ${value}    IN  @{LIST} 
    \   Do The Thing     ${value}

Python code for this:

def create_data():
    data = []
    for i in range(0, 10):
        data_val = do_a_bunch_of_selenium_automation(i)
        data.append(data_val)
    return data

How do I do this correctly? Thanks all.

In the code in your question, You are properly iterating over a list. However, you're creating the data in one test case and then trying to use it in another. The data is in a local variable in that first test case.

If you want to share data between test cases, you need to set the variable as a test suite variable, which you can do with the built-in keyword Set suite variable

Generate Data
    @{LIST}=    create data
    set suite variable  @{LIST}

To be honest your examples work for me. Made a few additions to get to a working example but nothing major:

ListCreationLibrary.py

class ListCreationLibrary(object):

    ROBOT_LIBRARY_VERSION = 1.0

    def __init__(self):
        pass

    def create_data(self):
        data = []
        for i in range(0, 10):
            data_val = self.do_a_bunch_of_selenium_automation(i)
            data.append(data_val)
        return data

    def do_a_bunch_of_selenium_automation(self, i):
        return  "some_string" + str(i)

test_script.robot

*** Settings ***
Library    ListCreationLibrary

*** Test Cases ***
TC
    ${list_example}    Create Data
    Do Thing For All Values In List    ${list_example}

*** Keywords ***
Do Thing For All Values In List
    [Arguments]    ${LIST}
    :FOR    ${value}    IN  @{LIST} 
    \   Do The Thing     ${value}

Do The Thing
    [Arguments]    ${value}
    Log    ${value}

Put the two files in the same directory and you should be OK.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM