【问题标题】:Assign a variable within the keyword "Run Keywords" possible?可以在关键字“运行关键字”中分配变量吗?
【发布时间】:2019-05-15 19:14:41
【问题描述】:

以下代码产生了一些问题:

API Setup
[Arguments]                           ${url}                           ${username}    ${password}    ${run}=True    ${fail}=False
Run Keyword If                        ${run}
...  Run Keywords     
     ...  ${passed}=                  Run Keyword And Return Status    Setup          ${url}         ${username}    ${password}
     ...  AND  Log To Console         ${passed}
     ...  AND  Should Not Be Equal    ${fail}                          ${passed}

当我尝试执行该操作时,我的 RF 显示:Variable '${passed}' not found

RED IDE 也说

Multiple markers at this line:
  • Variable 'passed' is used, but not defined
  • Keyword name '${passed}=' contains variables. RED is unable to validate 
    arguments given to this keyword

关键字“运行关键字”是否不允许将任何值分配给变量,如果是,是否有任何“最佳实践”方法来做我想做的事情?

谢谢!

【问题讨论】:

    标签: testing automated-tests robotframework


    【解决方案1】:

    Run Keywords 确实不允许 允许分配,这是真的。这来自解析器 - 关键字的目的是运行其他关键字;它来到赋值的那一行 (${passed}= Run...),尝试用它的值替换“传递”的变量,以便它可以执行它,但是此时 var 仍然是未定义的 - 它失败了。

    看起来您想要做的是仅在条件为真时运行特定关键字(Setup),并且仅当条件为真时才记录其结果,并断言它(结果)是想要的。

    这可以通过将块分成两部分来实现。关键字Run Keyword If 返回嵌入关键字的值,所以这将起作用:

    ${passed}=    Run Keyword If    ${run}    Run Keyword And Return Status    Setup          ${url}         ${username}    ${password}
                                 ...    ELSE    Set Variable    ${False}
    

    如果${run} == True 将运行Setup 关键字,并且{passed} 将在执行通过时保持真/假值。如果${run} != True 我们将${passed} 设置为False,那么它就没有None 的值(在这种情况下它的值并不重要,但这样做可以保证它拥有的数据类型的一致性)。

    现在其他两个关键字可以自己在 if 块中 - 只有在 ${run} == True 时才起作用:

    Run Keyword If    ${run}   Run Keywords     Log To Console         ${passed}
     ...  AND  Should Not Be Equal    ${fail}    ${passed}
    

    【讨论】:

      【解决方案2】:

      确实Run Keywords不允许在里面进行变量赋值

      创建一个包含Run Keywords 中所有内容的关键字,然后直接调用它。

      例如

      *** Test Cases ***
      API Setup
      [Arguments]                           ${url}                           ${username}    ${password}    ${run}=True    ${fail}=False
      Run Keyword If                        ${run}                           Keyword A
      
      *** Keywords ***
      Keyword A
          # put everything you need here
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-30
        • 2012-12-08
        • 2015-04-02
        • 2020-11-06
        • 2012-04-19
        • 2021-07-07
        • 2023-02-15
        • 1970-01-01
        相关资源
        最近更新 更多