if-statementrobotframeworkreturn-valuemultiple-return-values

Get return value from keyword from within Run Keyword If conditional Robot Framework


Usually getting the return value from a keyword isn't a big deal,

${myVar}=     My Keyword

This will work as expected.

The issue I'm trying to resolve would in cases similar to the following. I can usually find a work around but I'd like to find a better way.

Run Keyword If     '${someVar}' == 'True'     Run Keywords
     ...     Do Something
     ...     AND     ${myVar}=     My Keyword
     ...     AND     Do More Stuff

When I need to write something like this the My Keyword keyword isn't recognized as a keyword and I cannot run my test. I could pull this out of the conditional and run the keyword just before but that means I'm going to be wasting time running a keyword that should not be ran and will sometimes lead to a failure. I found this, How to get Returned value from a Keyword called under Run Keyword If in Robot Framework?, but that is for a single keyword and I'm not sure that I could expand to multiple keywords. Also, other situations may come up where there is something like the following and would further complicate things,

Run Keyword If     '${someVar}' == 'True'     Run Keywords
     ...     Do Something
     ...     AND     ${myVar}=     My Keyword
     ...     AND     Do More Stuff
     ...     AND     ${myVar_2}=     My Other Keyword
     ...     AND     Do Other Stuff

Anyone have any suggestions? Should I just break the one Run Keyword If up into several Run Keyword If 's so the I can utilize the example from the above link? I'd appreciate any suggestions.

Update: If someone else comes across this what I did to resolve the issue is as follows,

Run Keyword If     '${someVar}' == 'True'     Do Something
${myVar}=     Run Keyword If     '${someVar}' == 'True'     My Keyword
Run Keyword If     '${someVar}' == 'True'     Run Keywords
     ...     Do More Stuff
     ...     AND     And Do Other More Stuff

I will later look into the new IF conditional but I wanted to update this now before I forget.


Solution

  • The question you linked to is the same solution rather you run one keyword or many.

    ${result}=  Run keyword if  '${someVar}'  Run Keywords  ...
    

    In this case, whatever Run Keywords returns is what will be assigned to ${result}. There is no way to do a variable assignment within the arguments to Run Keywords.

    If you need to run multiple steps, the best thing you can do is create a keyword with those multiple steps, and then call that keyword from Run keyword if

    Note: robotframework 4.0 (which is in beta at the time that I read this) supports a native IF statement. It has been said that it will be released by the end of 2020. The native IF statement allows you to assign variables in the body of the IF block.

    With the new support for IF statements, you could rewrite your example like this:

    IF    '${someVar}' == 'True' 
        Do Something
        ${myVar}=     My Keyword
        Do More Stuff
        ${myVar_2}=     My Other Keyword
        Do Other Stuff
    END