简体   繁体   English

从 Run Keyword If 条件 Robot Framework 中获取关键字的返回值

[英]Get return value from keyword from within Run Keyword If conditional Robot Framework

Usually getting the return value from a keyword isn't a big deal,通常从关键字中获取返回值并不是什么大问题,

${myVar}=     My Keyword

This will work as expected.这将按预期工作。

The issue I'm trying to resolve would in cases similar to the following.我试图解决的问题类似于以下情况。 I can usually find a work around but I'd like to find a better way.我通常可以找到解决方法,但我想找到更好的方法。

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

When I need to write something like this the My Keyword keyword isn't recognized as a keyword and I cannot run my test.当我需要写这样的东西时, My Keyword关键字不被识别为关键字,我无法运行我的测试。 I could pull this out of the conditional and run the keyword just before but that means I'm going to be wasting time running a keyword that should not be ran and will sometimes lead to a failure.我可以将它从条件中拉出来并在之前运行关键字,但这意味着我将浪费时间运行一个不应该运行的关键字,有时会导致失败。 I found this, How to get Returned value from a Keyword called under Run Keyword If in Robot Framework?我发现了这个, How to get Returned value from a Keyword called under Run Keyword If in Robot Framework? , but that is for a single keyword and I'm not sure that I could expand to multiple keywords. ,但这是针对单个关键字的,我不确定是否可以扩展到多个关键字。 Also, other situations may come up where there is something like the following and would further complicate things,此外,可能会出现其他情况,例如以下情况并使事情变得更加复杂,

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

Anyone have any suggestions?有人有什么建议吗? Should I just break the one Run Keyword If up into several Run Keyword If 's so the I can utilize the example from the above link?我是否应该将一个Run Keyword If分成几个Run Keyword If ,这样我就可以利用上面链接中的示例? I'd appreciate any suggestions.我会很感激任何建议。

Update: If someone else comes across this what I did to resolve the issue is as follows,更新:如果其他人遇到这个我为解决问题所做的事情如下,

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

I will later look into the new IF conditional but I wanted to update this now before I forget.我稍后会研究新的IF条件,但我想在忘记之前更新它。

The question you linked to is the same solution rather you run one keyword or many.您链接到的问题是相同的解决方案,而不是您运行一个或多个关键字。

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

In this case, whatever Run Keywords returns is what will be assigned to ${result} .在这种情况下,无论Run Keywords返回什么都将分配给${result} There is no way to do a variable assignment within the arguments to Run Keywords .没有办法在 arguments 中进行变量赋值来Run Keywords

If you need to run multiple steps, the best thing you can do is create a keyword with those multiple steps, and then call that keyword from Run keyword if如果您需要运行多个步骤,您可以做的最好的事情是使用这些多个步骤创建一个关键字,然后从Run keyword if

Note: robotframework 4.0 (which is in beta at the time that I read this) supports a native IF statement .注意:robotframework 4.0(在我阅读本文时处于测试阶段)支持原生 IF 语句 It has been said that it will be released by the end of 2020. The native IF statement allows you to assign variables in the body of the IF block.据说2020年底会发布。原生的IF语句可以让你在IF块体中赋值变量。

With the new support for IF statements, you could rewrite your example like this:有了对IF语句的新支持,您可以像这样重写您的示例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 'Else'是保留关键字Robot Robot脚本中的错误 - 'Else' is a reserved keyword Error in Robot Framework script 如何在 Selenium Robot 框架中使用具有多个条件的“Exit For Loop IF”关键字 - How to use 'Exit For Loop IF' keyword with multiple condition in Selenium Robot framework “运行关键字如果”并设置变量 - "Run Keyword If" and setting a variable Google工作表,关键字返回IF公式中逻辑表达式参数的值? - Google sheet, keyword to return the value of the parameter of the logical expression in IF Formula? 从内部循环返回值不显示 - return value from within loop not showing 带有关键字“and”的条件 if 语句给了我意想不到的结果 - Conditional if statement with keyword “and” giving me unexpected results C#中的简写条件类似于'''关键字中的SQL' - Shorthand conditional in C# similar to SQL 'in' keyword 将文本文件中找到的关键字从关键字列表返回到新文件? - Returning keywords found in a text file from a keyword list to a new file? 仅从提及关键字的字符串中提取数字 - Extract numbers only from the strings in which a keyword is mentioned 如何从if else块获取返回值 - How to get return value from if else block
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM