23

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    Could you please clarify your question? – Argote Sep 28 '11 at 08:42
  • 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 Apr 18 '17 at 00:16
  • 1
    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 Oct 06 '18 at 06:27
  • 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 Oct 06 '18 at 06:46

6 Answers6

46

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:

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
11

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}
msudder
  • 505
  • 3
  • 14
4

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
2
# 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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Please edit your answer and add description or information about how it work to make others can understand it easily – koceeng Jan 05 '17 at 10:35
1

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.

Brandon Olson
  • 619
  • 2
  • 6
  • 19
0
*** Keyword ***
My Keyword
    [Arguments]       ${input}
    ${result}         Execute Command     lsblk -o mountpoint,label | grep ${input}
    [return]          ${result}

Basically, I created a keyword in which we pass the value as an argument then execute the command this command will take argument ${input} value and it will be stored into ${result},Then I simply return ${result} value.