Return results in Robot Framework keyword?

∥☆過路亽.° 提交于 2019-12-09 08:06:02

问题


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

回答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.

Example, using the pipe-separated plain text format:

*** 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
| | log | ${results}



回答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}



回答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.




回答4:


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.




回答5:


# 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


来源:https://stackoverflow.com/questions/7580252/return-results-in-robot-framework-keyword

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!