How can I return the results after running a keyword?
Example:
mykey word [Arguments] input
${results}= getme input
But I want to u
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.
# 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
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:
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}
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.