Search code examples
robotframeworkoptional-parametersoptional-arguments

How to set default value for an optional list parameter in keyword


I've previously used a list as parameter to take in a variable/optional number of arguments to a keyword, and this has worked perfectly:

Keyword Name
[Arguments]  ${otherVariable}  @{args}

....

My question is how do I set up a default value for this, if the user omits any more values?

i.e. something like

Keyword Name
[Arguments]  ${otherVariable}  @{args}=['0']

....

Solution

  • Check is ${args} empty, and if so - set the default value to it:

    Keyword Name
        [Arguments]  ${otherVariable}  @{args}
        ${args}=    Run Keyword If    not $args    Create List    0
                                 ...    ELSE       Set Variable   ${args}    # varags were passed, leave it as is
    

    This is analogous to this python code (RF is based on it, so a lot of approaches / recipes are the same/pretty close):

    def keword(otherVariable, *args):
        if not args: args = [0]