【问题标题】:Error while incrementing the value in Robot Framework在 Robot Framework 中增加值时出错
【发布时间】:2021-03-05 06:49:03
【问题描述】:
*** Settings ***
Library  DateTime

*** Test Cases ***
Test title
    ${TIME}=  get current date  result_format=%H
    RUN KEYWORD IF
    ...  int(${TIME})%2==0
    ...  ${TIME}=  catenate  SEPARATOR=  ${TIME}  :00
    ...  ELSE
    ...  ${TIME}=  Evaluate  int(${TIME})+1
    ...  ${TIME}=  catenate  SEPARATOR=  ${TIME}  :00
    log to console  ${TIME}

我收到以下错误:

找不到名称为“11=”的关键字。

【问题讨论】:

    标签: robotframework variable-assignment increment


    【解决方案1】:

    您不能在Run Keyword If 中进行变量赋值 - 正如其名称所示,它仅用于运行关键字,并且不支持赋值(它将变量视为必须运行的另一个关键字)。因此错误 - 框架用它的值 (11) 替换了${TIME},并尝试执行它。 在 4.0 版中,它支持正确的 IF/ELSE 块,但此限制不适用:

    Test title
        ${TIME}=  get current date  result_format=%H
        IF    int(${TIME})%2==0
             ${TIME}=  catenate  SEPARATOR=  ${TIME}  :00
        ELSE
             ${TIME}=  Evaluate  int(${TIME})+1
             ${TIME}=  catenate  SEPARATOR=  ${TIME}  :00
         END
        log to console  ${TIME}
    

    与此同时,您可以使用另一种方法来解决它 - 使用 Set Variable IfRun Keyword If 很相似,它是有条件的,但只接受值。
    框架中有一个功能,您可以就地进行计算(严格来说 - 调用方法),我们将使用它 - 将值增加 1:

    Test title
        ${TIME}=  get current date  result_format=%H
        ${TIME}=  Convert To Integer    ${TIME}    # to be sure it's type is this, I haven't checked what the keyword returns
        ${TIME}=    Set Variable If     ${TIME}%2==0    ${TIME}:00
                             ...           ${TIME + 1}:00    # this is the ELSE block
        log to console  ${TIME}
    

    【讨论】:

      猜你喜欢
      • 2020-11-18
      • 2016-11-21
      • 2017-01-20
      • 2014-03-24
      • 1970-01-01
      • 2020-07-21
      • 2019-05-06
      • 2021-10-18
      • 2015-10-01
      相关资源
      最近更新 更多