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.
4 Answers
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
3 Comments
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
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
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
Run Keyword If A!=B || B!=C || C!=DIt doesn't define such example anywhere. And from what I tried I got error from condition clause I wrote.