如何在 运行 运行 关键字 If - else 之后分配变量?

How to Assign a variable after running Run keyword If - else ?

我正在尝试分配一个变量 - body,具体取决于另一个变量 phonenumber_id 的状态。如果 phonenumber_idNULLbody 被赋值为 False。
但它似乎没有用。仅当他 phonenumber_id 不是 NULL 时才有效。

${body}=    Run keyword if  '${phonenumber_id}'!='NULL'     Set variable    TRUE
...         Else            Set Variable    FALSE   

不确定我做错了什么。

关键字Set Variable If将根据给定条件设置变量

${body}=    Set Variable If    '${phonenumer_id} != 'NULL'
...    ${True}    ${False}

您几乎答对了 - 只是打错了 ELSE - 它必须是大写字母,才能被视为 Run Keyword If 的一部分。因此,在您的特定情况下,它应该是:

${body}=    Run keyword if  '${phonenumber_id}'!='NULL'     Set variable    TRUE
...         ELSE            Set Variable    FALSE

对于仅设置新常量值的简单情况,@ILostMySpoon 就足够了 - 甚至更多 "readable".


一般来说,对于遇到这个 post 的人来说,Run Keyword IfELSE Set Variable 相结合是一个非常强大的 set/change 变量构造 - 基于事实它不仅有条件地运行关键字,而且还将其 return 值传播回堆栈。

考虑这个例子:

${var}=    Run Keyword If    ${bool condition}   Do Some Action Returning A Value    
           ...    ELSE    Set Variable    ${var}

其中 {var} 将设置为 Do Some Action Returning A Value 的 return 值,仅当 ${bool condition} 计算结果为真时,否则将保留其旧值。

另一个人为但不那么抽象的例子:

    ${value}=    Run Keyword If    ${should be int}     Convert To Integer   ${value}
                 ...    ELSE IF    ${should be float}   Convert To Number    ${value}
                 ...    ELSE    Set Variable    ${value}