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
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
Robot Framework 用户指南描述了如何从关键字返回值。请参阅用户关键字返回值。
简短的版本是:在关键字中设置一个变量,并使用[return]
testcase 设置返回该变量。
这是一个例子:
*** 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 还提供了几个关键字来显式地从关键字中的任何位置返回值:
一个简单的例子可能会有所帮助:
*** 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}
使用 [Return] 返回结果。
一个例子是:
Time Stamp
[Return] ${time_stamp}
${secs}= Get Time epoch
${time}= Get Time
${time_stamp}= Convert To String ${secs}
${time_stamp} 的值将存储在Time Stamp
关键字中。
# 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
最简单的方法是在关键字末尾使用建议的 [Return] 标签,尽管还有其他方法。
使用关键字设置全局变量,您可以使变量在运行它的关键字之外可访问,而不必从关键字本身返回任何内容。如果您想避免弄乱主变量列表并在后台放置一些变量,这很有用,但要像使用任何全局变量一样谨慎使用它。