Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Robot framework: looping through kwargs

I have need to take a list of keyword arguments in robot and convert them to a string of the form key=value if the keyword is in a certain list and set key=value if not. In python, it looks like this:

keywords=#list of keywords

def convert(**kwargs):
    s = ''
    for k,v in kwargs.iteritems():
        if k in keywords:
            s = s + '-%s=%s ' % (k,v)
        else:
            s = s + '-set %s=%s ' % (k,v)
    return s

I know this could be simply written as a python keyword, but I'm trying to figure out if it's possible using Robot. I can iterate using :For ${arg} in ${kwargs}, but I'm not sure how it would work to retrieve the keys and values individually.

like image 477
ewok Avatar asked May 05 '26 01:05

ewok


2 Answers

It's doable in robot, but the code will be really hard to read. This would definitely best be done in python.

Starting in robot framework 2.8, user keywords can accept keyword arguments. The syntax uses & rather than $ or @. So, to write a keyword that accepts keyword arguments you would do it like this:

*** Keywords ***
| Convert
| [Arguments] | &{args}

To iterate over the keys and values you can convert the dictionary to a list, then iterate over the list two at a time. It would look something like this:

| | ${result}= | set variable | ${EMPTY}
| | ${items}= | Get Dictionary items | ${args}
| | :FOR | ${key} | ${value} | IN | @{items}
| | | ${result}= | run keyword if | '${key}' in ${keywords}
| | | ... | catenate | ${result} | ${key}=${value}
| | | ... | ELSE
| | | ... | catenate | ${result} | set ${key}=${value}
| | [return] | ${result}

You might need to tweak what gets returned. I wasn't sure exactly what you wanted (your example includes dashes, but the description did not)

like image 167
Bryan Oakley Avatar answered May 07 '26 14:05

Bryan Oakley


Here is one simple example. I won't try to re-create your Python function here because doing conditional things in Robot gets convoluted really quickly. There is absolutely nothing wrong with using your own Python library in Robot, so that might be the better path. That said, you can extract kwargs in a Robot keyword:

*** Test Cases ***
mytestcase
    mykeyword    abc=1    def=2

*** Keywords ***
mykeyword
    [Arguments]    @{kwargs}
    :FOR    ${arg}    IN    @{kwargs}
    \    ${key}    ${value}=    Evaluate    "${arg}".split("=")
    \    Log    ${key}, ${value}

So in this script each iteration of the loop will extract the ${key} and ${value}` for each keyword argument supplied to the function.

I haven't used it yet, but the newest version of Robot Framework (2.9) also has dictionary variables, which might make a task like this a little easier, i.e., no loop. You will have to refer to the Robot Framework Docs for this, though.

like image 42
Raceyman Avatar answered May 07 '26 13:05

Raceyman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!