21
votes

How can I return the results after running a keyword?

Example:

mykey word [Arguments] input
   ${results}=  getme input

But I want to use these results:

 ${results} = mykey word  newinput
5
Could you please clarify your question? - Argote
The most important thing to know about RF is, if you have ever programmed in any other language ever, pretend you know none of that. (Unless the language in question was BASIC, in which case, you'll be fine.) - Keith Tyler
Wow @Keith Tyler, that's one of the more unproductive comments I've seen in SO recently (still, not the "most" one), and uncalled for. RF is a generic automated testing framework, it is not even trying to be a programming language; though some are trying to push in into that (me - guilty as charged). While generic, and purposed for automated testing, it does provide some rudimentary coding concepts - from control flow statements (loops, branches), through subroutunes (keywords), to scopes. - Todor Minakov
And in the context of this question, returning a value is hardly a Basic exclusive capability ;). In RF this is accomplished in the same way as in (pretty much) every mainstream programming language - declaring to pass back a result/outcome of its execution. Not surprisingly, that is done by a statement called [Return] and providing the to-be-returned value(s) (yes, plural, can return tuples, take that, Basic! 👊) - personally, my natural expectation having seen some other coding languages. - Todor Minakov

5 Answers

38
votes

The Robot Framework user's guide describes how to return a value from a keyword. See User keyword return values.

The short version is: set a variable in your keyword, and use the [return] testcase setting to return that variable.

Here's an example:

*** Keywords ***
mykey word
  [Arguments]  ${input}
  ${string}=  set variable  the string is "${input}"
  [return]  ${string}

*** Test Cases ***
Call custom keyword and get result
  ${results}=  mykey word  newinput
  Should be equal    ${results}    the string is "newinput"

Robot also provides several keywords to explicitly return a value from anywhere in a keyword:

11
votes

A simple example may help:

*** Keywords ***
Convert temperature F To Centigrade
  [Arguments]  ${ftemp}
  ${ftemp} =  Convert To Float  ${ftemp}
  ${ctemp} =  ${0.9} * ${ftemp} - ${32}
  [Return]  ${ctemp}

Convert temperature C To Fahrenheit
  [Arguments]  ${ctemp}
  ${ctemp} =  Convert To Float  ${ctemp}
  ${ftemp} =  ${1.8} * ${ctemp} + ${32}
  [Return]  ${ftemp}

*** Test Cases ***
Verify Temperature Conversion  
  ${result} =  Convert temperature F To Centigrade  ${32}
  Should Be Equal  ${result}  ${0}
  ${result} =  Convert temperature C To Fahrenheit  ${0}
  Should Be Equal  ${result}  ${32}
4
votes

Use [Return] to return results.

An example is:

Time Stamp

      [Return]  ${time_stamp}
      ${secs}=  Get Time  epoch
      ${time}=  Get Time
      ${time_stamp}=  Convert To String      ${secs}

The value of ${time_stamp} will be stored in the Time Stamp keyword.

2
votes
# This example will explain the usage of build in library keywords.
# The "Evaluate", "Log", and "Return" settings by using Fahrenheit to Centigrade
# conversion logic on the variable ${var1}.

*** Variables ***
${var1}     32
*** Keywords ***
Convert temperature Fahrenheit To Centigrade
  [Arguments]  ${ftemp}
  ${ftemp} =  Convert To Number     ${ftemp}
  ${ctemp} =  evaluate  (5 * (${ftemp} - 32))/9
  [Return]  ${ctemp}


*** Test Cases ***
Verify Temperature Conversion F to C
  ${result} =  Convert temperature Fahrenheit To Centigrade  ${var1}
  Log  ${result}
  Should Be Equal As Numbers    ${result}   0.0
1
votes

The easiest way is to use the suggested [Return] tag at the end of your keyword, though other ways exist.

Using the keyword Set Global Variable, you can make a variable accessible outside of the keyword it's run in without having to return anything from the keyword itself. This is useful if you want to avoid cluttering up your main variable list and have a few variables sitting in the background, but use it with as much caution as you would any global variable.