user3477373 user3477373 - 1 year ago 173
Python Question

How to write multiple conditions of if-statement in robot framework

I am having troubles with writing if conditions in 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,
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


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

But just end up having errors.

Answer Source

you should use small caps "or" and "and" instead of OR and AND. And beware also the spaces/tabs between keywords and arguments (you need at least 2 spaces). Here is a code sample with your 3 keywords working fine:

Here is the file ts.txt:

  *** test cases ***
    ${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
mytest                                                                .
executed with single condition
executed with multiple or
executed with unless and multiple or
mytest                                                                | PASS |
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download