Return results in Robot Framework keyword?

88,713

Solution 1

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:

Solution 2

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}

Solution 3

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.

Solution 4

# 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

Solution 5

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.

Share:
88,713
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    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
    
  • koceeng
    koceeng over 7 years
    Please edit your answer and add description or information about how it work to make others can understand it easily
  • Adam Mierzwiak
    Adam Mierzwiak over 3 years
    I use robotframework 3.2.1 and return with small letter r was not recognized, it require R
  • Russell Smith
    Russell Smith over 3 years
    @AdamMierzwiak: that isn't true. Robot doesn't care about the case. The code in this answer works fine with robot 3.2.1
  • Adam Mierzwiak
    Adam Mierzwiak over 3 years
    ok, so it have to be problem of plugin for robot in pycharm
  • Russell Smith
    Russell Smith over 3 years
    @AdamMierzwiak: that's entirely possible. I don't use pycharm so I have no idea.