Warm tip: This article is reproduced from serverfault.com, please click

if statement-如果条件机器人框架,则从“运行关键字”中获取关键字的返回值

(if statement - Get return value from keyword from within Run Keyword If conditional Robot Framework)

发布于 2020-12-02 16:21:22

通常从关键字获取返回值没什么大不了的,

${myVar}=     My Keyword

这将按预期工作。

在与以下类似的情况下,我要解决的问题。我通常可以找到解决方法,但我想找到一种更好的方法。

Run Keyword If     '${someVar}' == 'True'     Run Keywords
     ...     Do Something
     ...     AND     ${myVar}=     My Keyword
     ...     AND     Do More Stuff

当我需要编写类似这样的内容时,该My Keyword关键字不会被识别为关键字,并且无法运行测试。我可以将其从条件中删除,然后在之前运行关键字,但这意味着我将浪费时间运行一个不应运行且有时会导致失败的关键字。我发现了这一点,如何在Robot Framework中的Run Keyword If下从一个名为Key的关键字中获取返回值?,但这只是一个关键字,因此我不确定是否可以扩展到多个关键字。此外,在其他情况下,可能还会出现以下情况,这会使事情变得更加复杂,

Run Keyword If     '${someVar}' == 'True'     Run Keywords
     ...     Do Something
     ...     AND     ${myVar}=     My Keyword
     ...     AND     Do More Stuff
     ...     AND     ${myVar_2}=     My Other Keyword
     ...     AND     Do Other Stuff

有人有什么建议吗?我是否应该将一个Run Keyword If拆分成几个Run Keyword If,以便可以利用上面链接中的示例?我将不胜感激任何建议。

更新:如果其他人遇到此问题,我将采取以下措施来解决此问题,

Run Keyword If     '${someVar}' == 'True'     Do Something
${myVar}=     Run Keyword If     '${someVar}' == 'True'     My Keyword
Run Keyword If     '${someVar}' == 'True'     Run Keywords
     ...     Do More Stuff
     ...     AND     And Do Other More Stuff

稍后,我将研究新的IF条件,但是在忘记之前,我想立即对此进行更新。

Questioner
Brian.H
Viewed
11
Bryan Oakley 2020-12-03 01:31:09

你链接到的问题是相同的解决方案,而你只运行一个或多个关键字。

${result}=  Run keyword if  '${someVar}'  Run Keywords  ...

在这种情况下,无论Run Keywords收益如何,都将分配给${result}无法在的参数中进行变量赋值Run Keywords

如果你需要执行多个步骤,那么最好的办法是使用这些多个步骤创建一个关键字,然后从中调用该关键字 Run keyword if

注意:robotframework 4.0(在我读此书时为beta)支持本机IF语句据说它将在2020年底发布。本机IF语句允许你在IF块的主体中​​分配变量。

有了对IF语句的新支持,你可以像这样重写示例:

IF    '${someVar}' == 'True' 
    Do Something
    ${myVar}=     My Keyword
    Do More Stuff
    ${myVar_2}=     My Other Keyword
    Do Other Stuff
END