10

How can we use if statement in robot framework. I would like to execute keyword only if it satisfies certain condition else it execute other code.

3
  • 1
    In case you're wondering why you have downvotes, it's because you did not read the documentation. Commented Jun 24, 2015 at 10:55
  • 2
    Surprisingly, if you google for "robotframework if clause" or "robotframework if statement", this question is the first hit and the answer led me to the right section of the documentation. I up voted the question for this usefulness. Commented Aug 21, 2017 at 8:49
  • 2
    Pardon guys, but I googled and this is the first result for me. Downvote the post owner is quite unfair. And even documentation is very unclear what's the things can I do. Ex. I want to approach Run Keyword If A!=B || B!=C || C!=D It doesn't define such example anywhere. And from what I tried I got error from condition clause I wrote. Commented Jul 10, 2018 at 4:53

4 Answers 4

24

This is described in the Robot Framework User Guide under the section Conditional Execution, where it mentions Run Keyword If and Run Keyword Unless among other solutions. Documentation for these can be found in the documentation for the BuiltIn keyword library.

Here is a brief example:

*** Test cases ***
| Example
| | ${result}= | Set variable | 42
| | Run keyword if | "${result}" == "42"
| | ... | log | the result is 42
| | ... | ELSE 
| | ... | log | the result is NOT 42
Sign up to request clarification or add additional context in comments.

3 Comments

When I use Else, it shows following error. "'Else' is a reserved keyword" how to solve this??
@user3068846: that likely means you forgot the continuation characters ("..."), ELSE works when used correctly.
Thank you, continuation helps.
2

With robot 4.0 we also have native if else support

IF  '${status}' == 'true'
    ${i}  Set Variable  10
    log to console  inside if
ELSE IF  '${status}' == 'false'
    ${i}  Set Variable  20
    log to console  inside else if
ELSE
    ${i}  Set Variable  30
    log to console  inside else
END

1 Comment

0

In case multiple keywords need to be executed on the IF branches, this is the syntax:

${result} =   Set variable   42
Run keyword if   "${result}" == "42"   Run Keywords
...   log to console   First
...   AND   log to console   Second
...  ELSE   Run Keywords
...   log to console   Else First
...   AND   log to console   Else Second

Comments

0

Beware that setting variables does not work with "Run Keyword If". It works in "IF" but if you are using an older version of Robot then use "Set Variable If" instead (https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Set%20Variable%20If):

${var1}=  Set Variable If  ${rc} == 0  val1  val2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.