在Robot Framework中处理Argument变量作为返回值

时间:2017-06-27 09:31:43

标签: robotframework

我在一些操作后在Return中返回一个Argument变量时遇到了问题。

请查看代码:注意这是一个示例代码

*** Settings ***
Library    Selenium2Library
Library    Collections

*** Keywords ***
Parent Routine
    ${index}    Set Variable    0
    ${index}    Set Variable      Child Routine     ${index}
    log to console    ${index}

Child Routine
    [Arguments]    ${index}
    ${index}    Set Variable      Grand Child Routine    ${index}
    #\    Some other manipulation
    [Return]    ${index}

Grand Child Routine
    [Arguments]    ${index}
    : For    ${i}     IN RANGE    1    5
    \    ${index}    Set Variable      ${index} +  1
    #\    Some other manipulation
    [Return]    ${index}

*** Test Cases ***
Sample Test Case
    [Documentation]   Simple test for Return Value
    Parent Routine

请查看输出窗口

enter image description here

预计输出很可能是 5 ,但显示 [u'Child Routine', u'0']

请帮助我如何获得预期的输出。

3 个答案:

答案 0 :(得分:1)

设置变量

您使用的是set variable错误。该关键字用于设置变量的值,而不是用于调用其他关键字。

请考虑以下代码:

${index}    Set Variable      Child Routine     ${index}

您正在创建一个列表,其中第一个值是文字字符串" Child Routine"第二个值是${index}中的任何值,然后您将变量${index}设置为该列表。

如果你想调用一个关键字并保存它的返回值,你所要做的就是在任何变量之后首先使该关键字成为关键字。例如,在下面的代码中,我们调用Child Routine,将其作为唯一参数传递${index}。结果将保存在${index}中。

${index}    Child Routine     ${index}

做数学

您无法添加" + 1"变量。机器人并不是一种编程语言。如果要将一个添加到变量,则需要使用一些关键字来执行此操作。内置关键字Evaluate可用于此类目的。

换句话说,而不是:

${index}    Set Variable      ${index} +  1

你需要这样做:

${index}   evaluate   ${index} + 1

答案 1 :(得分:0)

是的,我们可以使用......

在您的代码中,您错过了 Evaluate 关键字。在进行任何数学运算之前,我们应该使用 Evaluate 关键字。

仅更改${index}= Evaluate ${index} + 1

最后你的代码是

*** Settings ***
Library    Selenium2Library
Library    Collections

*** Keywords ***
Parent Routine
    ${index}    Set Variable    0
    ${index}    Set Variable      Child Routine     ${index}
    log to console    ${index}

Child Routine
    [Arguments]    ${index}
    ${index}    Set Variable      Grand Child Routine    ${index}
    #\    Some other manipulation
    [Return]    ${index}

Grand Child Routine
    [Arguments]    ${index}
    : For    ${i}     IN RANGE    1    5
    \    ${index}=    Evaluate      ${index} + 1
    #\    Some other manipulation
    [Return]    ${index}

*** Test Cases ***
Sample Test Case
    [Documentation]   Simple test for Return Value
    Parent Routine

答案 2 :(得分:0)

这是因为您尝试通过将用户关键字传递给只需Set Variable值的String来调用您的用户关键字。传入多个值时,最终会得到List

如果您想调用用户关键字并将其返回值分配给变量,则需要使用Run Keyword

Parent Routine
    ${index}    Set Variable    0
    ${index}    Run Keyword      Child Routine     ${index}
    log to console    ${index}

Child Routine
    [Arguments]    ${index}
    ${index}    Run Keyword      Grand Child Routine    ${index}
    #\    Some other manipulation
    [Return]    ${index}