Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have trouble 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 

And

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

But I just end up getting errors.

like image 745
user3477373 Avatar asked May 26 '14 05:05

user3477373


People also ask

How do you write multiple conditions in an if statement in Robot Framework?

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 two spaces). Show activity on this post.

How do you write if else condition in robot framework?

END Use Run Keyword If in Robot Framework Run Keyword If ${True} Log This line IS executed. Run Keyword If ${False} Log This line is NOT executed. Use Run Keyword Unless in Robot Framework Run Keyword Unless ${True} Log This line is NOT executed. Run Keyword Unless ${False} Log This line IS executed.

How do I run multiple keywords in Robot Framework?

You can do a couple of things. The first is to create a new keyword that calls all the other keywords, and then call that from Run keyword if . This might be the most readable solution, but at the expense of having to write and document another keyword.


1 Answers

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 two spaces).

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

Here is the file 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 | ------------------------------------------------------------------------------ 
like image 79
Laurent Bristiel Avatar answered Sep 19 '22 11:09

Laurent Bristiel