Robot Framework Multiple Statements in If Condition
Using "Run keywords" is the correct solution. However, in order to run multiple keywords you must separate keywords with a literal AND
:
... ELSE run keywords
... log ${status}
... AND FAIL Values Do Not Match
Handle multiple statements in Run Keyword If - Robot Framework
Off-course we can execute multiple statements in Run Keyword If
The If Statement should be
Run Keyword If ${isElementExist} Run Keywords click element id=btn1
... AND click element id=btn2
... AND click element id=btn3
The Complete Code is
*** Settings ***
Library Selenium2Library
Library Collections
*** Keywords ***
Parent Routine
${isElementExist} Run Keyword And Return Status Element Should Be Visible id=txt1
Run Keyword If ${isElementExist} Run Keywords click element id=btn1
... AND click element id=btn2
... AND click element id=btn3
*** Test Cases ***
Sample Test Case
[Documentation] Simple test for If Condition
Parent Routine
How to write multiple conditions of 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).
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 |
------------------------------------------------------------------------------
Evaluating multiple If conditions in Robot framework
There are at least four problems with the code you've posted:
- there's only one space after the first "if"
- "else if" is lowercase. It must be all uppercase (
ELSE IF
) - you have commas in your code. robot syntax doesn't support commas as field separators
- your code seems to span multiple lines but you aren't using the robot line continuation characters
...
Robot Framework - performing multiple keywords after running keyword if
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.
The other choice is to use a combination of Run keyword if
and Run keywords
, like so:
| | Run Keyword if | '${title}' == 'Some Title'
| | ... | Run Keywords
| | ... | Click Element | xpath=some element
| | ... | AND | Element Text Should Be | xpath=some element | some text
| | ... | ELSE
| | ... | Click Element | xpath=other element
how to use both And and Or condition in robot framework
In the below examples the different combinations are tested against a keyword that validates the combinations using the and/or validations. Please keep in mind that this kind of and/or check can also be separate ELSE IF
statements.
*** Test Cases ***
TC
[Template] Validate App and Contains
App Name true
App Name false
My app Name true
My app Name false
Not My Name true
Not My Name false
*** Keywords ***
Validate App and Contains
[Arguments] ${App_Name} ${Contains_True}
Run Keyword If
... ('${Contains_True}'=='true' and '${App_Name}'=='App Name') or '${App_Name}'=='My app Name' Return From Keyword Accept
... ELSE IF '${Contains_True}'== 'true' Log App Name is not as expected:"${App_Name}" level=WARN console=${True}
... ELSE Fail Of the combo "${App_Name}"/"${Contains_True}" None of the values are correct.
IF ELSE in robot framework with variables assignment
Having IF/THEN/ELSE with multiple statements in each block does not work in Robot (or you would have to use "Run Keywords" I suppose, but that would become unreadable). So I would refactor your code this way:
Choose Particular Filter ${FILTER} And Uncheck All Values
${is_filter_opened}= is filter opened ${AVAILABLE FILTERS} ${FILTER}
run keyword if ${is_filter_opened} actions_when_unchecked
... ELSE actions_when_checked
actions_when_unchecked
uncheck all in filter ${AVAILABLE FILTERS} ${FILTER}
click element ${uncheck_all_button}
actions_when_checked
${particular_filter}: find particular filter ${AVAILABLE FILTERS} ${FILTER}
click element ${particular_filter}
${uncheck_all_button}: uncheck all in filter ${AVAILABLE FILTERS} ${FILTER}
click element ${uncheck_all_button}
Hope this helps.
Related Topics
A Better Way Than Looping and Calling Functions That Loop and Call Another Functions
Most Efficient Way to Construct Similarity Matrix
Why Does Tkinter Image Not Show Up If Created in a Function
Install Utils Package in Python Facing With Error Package Not Found
Opencv Typeerror: Expected Cv::Umat for Argument 'Src' - What Is This
How to Calculate a Gaussian Kernel Matrix Efficiently in Numpy
Pip Install Not Working With Jenkins
Fill With Nan When Length of Values Does Not Match Length of Index
Implement K-Fold Cross Validation in Mlpclassification Python
Splitting Dataframe into Multiple Dataframes
Is There a Short-Hand for Nth Root of X in Python
Defining and Calling a Function Within a Python Class
Filtering Dataframe Using the Length of a Column
Beautifulsoup: Get the Contents of a Specific Table
How to Plot in Real-Time in a While Loop Using Matplotlib