【问题标题】:How to write multiple conditions of if-statement in Robot Framework如何在 Robot Framework 中编写 if 语句的多个条件
【发布时间】:2014-07-14 19:44:14
【问题描述】:

我在 Robot Framework 中编写 if 条件时遇到问题。

我要执行

Run Keyword If '${color}' == 'Red' OR '${color}' == 'Blue'  OR '${color}' == 'Pink'    Check the quantity

我可以在一个条件下使用这个“Run keyword If”关键字,但是对于多个条件,我得到了这个错误:

失败:关键字名称不能为空。

我也想使用这些关键字:

Run Keyword If '${color} == 'Blue' AND '${Size} == 'Small' AND '${Design}' != '${Simple}'      Check the quantity

和

Run Keyword Unless '${color}' == 'Black' OR '${Size}' == 'Small' OR '${Design}' == 'Simple'

但我最终会遇到错误。

【问题讨论】:

  • 由于您正在对条件进行 OR'ing,因此您可以只使用 3 个 Run Keyword If 语句。
  • 试试Run Keyword If '${color}' in ['Red', 'Blue', 'Pink'] Check the quantity
  • 感谢您的回答。也可以帮我回答编辑的部分吗?谢谢!

标签: python python-2.7 selenium robotframework


【解决方案1】:

只需确保在“and”关键字之前和之后放置一个空格..

【讨论】:

  • 您可以将其添加为评论而不是答案!
【解决方案2】:

以下代码运行良好:

Run Keyword if    '${value1}' \ \ == \ \ '${cost1}' \ and \ \ '${value2}' \ \ == \ \ 'cost2'    LOG    HELLO

【讨论】:

    【解决方案3】:

    您应该使用小型大写字母“or”和“and”而不是 OR 和 AND。

    还要注意关键字和参数之间的空格/制表符(您至少需要两个空格)。

    这是一个代码示例,您的三个关键字工作正常:

    这里是文件ts.txt:

      *** test cases ***
      mytest
        ${color} =  set variable  Red
        Run Keyword If  '${color}' == 'Red'  log to console  \nexecuted with single condition
        Run Keyword If  '${color}' == 'Red' or '${color}' == 'Blue' or '${color}' == 'Pink'  log to console  \nexecuted with multiple or
    
        ${color} =  set variable  Blue
        ${Size} =  set variable  Small
        ${Simple} =  set variable  Simple
        ${Design} =  set variable  Simple
        Run Keyword If  '${color}' == 'Blue' and '${Size}' == 'Small' and '${Design}' != '${Simple}'  log to console  \nexecuted with multiple and
    
        ${Size} =  set variable  XL
        ${Design} =  set variable  Complicated
        Run Keyword Unless  '${color}' == 'Black' or '${Size}' == 'Small' or '${Design}' == 'Simple'  log to console  \nexecuted with unless and multiple or
    

    这是我执行它时得到的结果:

    $ pybot ts.txt
    ==============================================================================
    Ts
    ==============================================================================
    mytest                                                                .
    executed with single condition
    executed with multiple or
    executed with unless and multiple or
    mytest                                                                | PASS |
    ------------------------------------------------------------------------------
    

    【讨论】:

    • 我可以给三个或更多'或者'它会起作用吗?或者如果有条件,机器人中只允许使用两个“或”?
    猜你喜欢
    • 2021-12-17
    • 1970-01-01
    • 2022-11-29
    • 1970-01-01
    • 2010-10-29
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多