如何在Robot Framework中编写if语句的多个条件

时间:2014-05-26 05:28:02

标签: python python-2.7 selenium robotframework

我在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'

但我最终得错了。

3 个答案:

答案 0 :(得分:34)

您应该使用小型大写字母“或”和“和”代替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 |
------------------------------------------------------------------------------

答案 1 :(得分:0)

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

答案 2 :(得分:-1)

以下代码运行良好:

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