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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23863264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:33:47  来源:igfitidea点击:

How to write multiple conditions of if-statement in Robot Framework

pythonpython-2.7seleniumrobotframework

提问by user3477373

I have trouble writing ifconditions in Robot Framework.

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

I want to execute

我想执行

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

I can use this "Run keyword If" keyword with one condition, but for more than one conditions, I got this error:

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

FAIL: Keyword name cannot be empty.

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

And also I would like to use these keywords:

而且我想使用这些关键字:

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

And

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

But I just end up getting errors.

但我最终会出错。

采纳答案by Laurent Bristiel

You should use small caps "or" and "and" instead of OR and AND.

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

And beware also the spaces/tabs between keywords and arguments (you need at least two spaces).

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

Here is a code sample with your three keywords working fine:

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

Here is the file ts.txt:

这是文件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

and here is what I get when I execute it:

这是我执行它时得到的:

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

回答by Bahubali Patil

The below code worked fine:

下面的代码工作正常:

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

回答by kunal krishna

Just make sure put single space before and after "and" Keyword..

只要确保在“and”关键字前后放置一个空格即可。