- Alapan.Me (Personal Blog)
Select Page
How to do Conditional Testing(If-Else) in Robot Framework
Posted by Alapan | Jul 6, 2021 | Robot Framework | 0 |
In this article, we will look in detail at how we can do conditional testing(or If-Else) in the Robot framework. Let’s write a simple program to deep dive.
Steps: 1. Go to Wikipedia.org 2. Search for Wikivoyage, if found, verify the title, End test 3. Search for Wikivoyage, if not found, click on Wiktionary, verify the title, End test
Let’s understand the above code –
Test Suite title is Wikipedia Conditional Testing . SelenumLibrary is imported into the test suite, so that we can use Selenium commands.
In the first test case, we are checking the presence of Wikivoyage link on the webpage. To do that, we are saving the count of the element using ${count}= Get Element Count css:[data-jsl10n=”wikivoyage.name”] . Now, If the element is present then the count value will be 1, if not then 0. Then we are using an If condition to check that if the value of count is greater than 0, then we click the Wikivoyage link Run Keyword If ${count} > 0 Click Wikivoyage . And in the else condition we are checking that if the count value is less than 0, then we click the Wiktionary link. Since we know that the element is present on the page and the count value is 1, we are checking the Wikivoyage page title using Title Should Be Wikivoyage timeout=5 . And then finally we are ending the test using Close Browser .
In the second test case, we are intentionally giving a wrong locator value so the count value is 0 using ${count}= Get Element Count css:wrong locator . Now we are using the same If and Else condition as test case 1. But here since the count value is 0, the else condition will be executed, which will click the Wiktionary link … ELSE Click Wiktionary . Then we are checking the Wiktionary page title using Title Should Be Wiktionary timeout=5 . And then finally we are ending the test using Close Browser .
After execution, we should see that the test cases are passed.
Do check out 🙂
Github:Â https://github.com/alapanme/Robot-Framework All Robot Framework Articles: https://testersdock.com/robot-framework-tutorial/
About The Author
A QA by profession and a Coder by passion. Fan of Open-Source projects, Automation, Steve Jobs & Tom Hanks. Brains and Sweat behind Testersdock.
Related Posts
What are Variables in Robot Framework
June 30, 2021
How to work with Shadow DOM in Robot Framework
July 9, 2021
How to switch between multiple Browser Windows in Robot Framework
July 19, 2021
Executing Tests using ‘Tags’ in Robot Framework
August 1, 2021
Leave a reply Cancel reply
Your email address will not be published. Required fields are marked *
This site uses Akismet to reduce spam. Learn how your comment data is processed .
Recent Posts
- Certification (4)
- Cypress (37)
- Nightwatch JS (16)
- Perfecto (1)
- Playwright (14)
- Robot Framework (19)
- Selenium (13)
- Testing Basics (2)
Stackoverflow Stats
Lets Connect!
Pin it on pinterest.
Robot Framework Tutorial #38 – How to use IF ELSE in Robot Framework
In this Robot Framework Tutorial, we will understand how to use if-else in robot framework.
* If/Else – Conditional statement which runs a different set of statements depending on whether an expression is true or false
* Robot Framework has a builtin library for this functionality
* You can also use multiple “ELSE IF” clause
* It can also have an “ELSE” clause
* Format for IF/ELSE is as follows:
Run Keyword If ${condition} == “Some Data” Keyword1
… ELSE IF ${condition} == “Some Other Data” Keyword2
… ELSE Keyword3
You may like these posts
Robot Framework Tutorial #1 – Install Python and PIP on Mac OS
Robot Framework Tutorial #2 – Introduction, Installation of RobotFramework and SeleniumLibrary
Robot Framework Tutorial #3 – Installing PyCharm and IntelliBot Plugin
Robot Framework Tutorial #4 – Create New Robot Framework Automation Project
Robot Framework Tutorial #5 – Key Sections of Robot Framework File
Coding Knowledge Unveiled: Empower Yourself
Robot Framework Variables and Variable Assignment: Managing Data in Your Test Cases
In Robot Framework, variables play a crucial role in managing and using data within your test cases. They allow you to store and manipulate values, making your tests more dynamic and flexible. In this blog post, we’ll explore the concepts of variables and variable assignment in Robot Framework, providing insights into how to work with data effectively.
Understanding Variables
In Robot Framework, variables are used to store and manage data that can be reused throughout your test cases and test suites. Variables can hold a wide range of data types, including strings, numbers, lists, and more. They make your test cases more versatile by allowing you to change input data, conditions, or expected results easily.
Variable Syntax and Naming
Variables in Robot Framework are typically defined and referenced using a dollar sign ( $ ) followed by the variable name. Variable names are case-insensitive and can include letters, numbers, and underscores. Conventionally, variable names are written in uppercase to distinguish them from keywords and other identifiers.
Variable Assignment
Variable assignment is the process of storing a value in a variable. You can assign values to variables in several ways:
Scalar Variables
Scalar variables hold single values such as strings or numbers. You can assign values to scalar variables using the Set Variable keyword or the ${VARNAME}= syntax.
List Variables
List variables can store multiple values. You can assign a list of values to a variable using the Create List keyword or by directly specifying the list within square brackets.
Dictionary Variables
Dictionary variables store key-value pairs. You can assign a dictionary to a variable using the Create Dictionary keyword or by specifying it directly with curly braces.
Variable Usage
Once you’ve assigned values to variables, you can use them in your test cases, either by referencing the variable name directly or by using it as an argument for keywords.
In this test case:
- ${username} and ${age} are used to log the user’s information and verify the user’s name and age.
Variable Modification
You can modify variable values during test execution using various keywords. For example, you can use the Set Variable keyword to change the value of a variable.
- ${count} is initially set to 5 , then incremented to 6 using the Evaluate keyword.
Variables and variable assignment are essential concepts in Robot Framework that empower you to manage and use data effectively in your test cases. By understanding how to define, assign, and use variables, you can create more dynamic and versatile tests that adapt to different scenarios and conditions.
As you gain experience with Robot Framework, you’ll find that variables play a vital role in making your test automation scripts more maintainable and efficient. Whether you’re dealing with test data, configuration settings, or user inputs, variables are your key to success in test automation. Happy testing! 🤖🚀
Leave a Reply Cancel reply
You must be logged in to post a comment.
ELSE IF with RF 4.x : different way of use?
I have installed RF 4.1.3 and suddenly, my code with “ELSE IF” keyword are underlined, as if it was an error. But my test works. I suppose that with the update of RF (RF 3 to RF 4), the way of using this keyword are different but I don’t know (don’t understand also) why. I think that an argument is missing but I haven’t it. Could you help me ?
My configuration :
- Python 3.9.7
- VS Code as IDE
Thanks a lot !
If the syntax is underlined but the test works it means that this is a matter of the plugin you are using to recognize de RF code and not with RF itself
You can view which VSCode plugin is reporting this error by hovering over the underlined keyword or clicking the problems button on the status bar of the editor.
The extensions I used on VS Code are :
- Robocorp Code
- Robot Framework Language Server
It’s surprising because I haven’t the problem when I was on the office’s organisation. Today, I’m working at home and I have this error…
I have only this kind of error for example :
Unexpected argument: '${typeBien}'=='Maison'
Looks similar to this issue
I finally found the solution. In the RF docs, it’s written " NOTE: Robot Framework 4.0 introduced built-in IF/ELSE support and using that is generally recommended over using this keyword."
So I have re-write my keyword as :
Conclusion : The “Run keyword if” is not recommended with RF 4 (but works)
Related Topics
This page summarizes the most important information about variables in Robot Framework. For more information, please refer to the official Robot Framework User Guide .
*** Variables *** Section ​
Variables which are defined in the *** Variables *** section are available in all test cases and keywords in the same file. Variables defined in the *** Variables *** section are suite variables. If a .resource or a .robot file with a *** Variables *** section is imported into a test suite, the variables there also become suite variables.
Set Variables in Test Cases and Keywords ​
Besides the *** Variables *** section, variables can also be set dynamically in test cases and keywords. Variables are set by the return value of a keyword. There are some Keywords which set variables values explicitly, like
- Set Variable
- Set Test Variable
- Set Suite Variable
- Set Global Variable
They can be used to either set a value of a variable or change the scope of a variable.
When to use $ and @ and & and %? ​
Variable Names consist of
- a variable type identifier (e.g. $ , @ , & , % )
- curly braces {}
- a variable name (e.g. my_var , my_list , my_dict , my_var2 )
The variable type identifier is used to define the type of the variable.
- $ is used for scalar variables.
- @ is used for list variables.
- & is used for dictionary variables.
- % is used for environment variables.
What is the difference between a scalar variable, a list variable and a dictionary? A scalar variable can only contain one value . A list variable can contain multiple values . A dictionary variable can contain multiple key-value pairs .
But why can we access a list variable my_list with the syntax ${my_list} and also with the syntax @{my_list} ?
${my_list} accesses the whole list object my_list . It is a container and holds all the items of the list. You can use it to access the whole list variable my_list or to access a specific item of the list with the syntax ${my_list}[index] .
@{my_list} accesses the items of the list variable my_list . It is like the list variable my_list is unpacked and all the items are available as individual variables.
Whenever you want to access the container of a list variable, you have to use the syntax ${my_list} . When you want to break down the list variable into its items, you have to use the syntax @{my_list} .
It's like a beer crate with bottles inside. With the syntax ${beer_crate} you can access the whole beer crate. With the syntax @{beer_crate} you get the bottles. With the syntax ${beer_crate}[index] you get a specific bottle inside the crate.
Variable Files ​
It is possible to load variables from external files, like Python ( .py ) or .yaml files. For Yaml files, the package pyyaml is required.
.py files will be interpreted as Python Code, so you can create simple variables, lists or dictionaries and even complex objects like classes.
Even dynamic variables using are possible by executing any Python code inside the variable file.
.yaml (or .yml ) files will be interpreted as a dictionary.
- *** Variables *** Section
- Set Variables in Test Cases and Keywords
- When to use $ and @ and & and %?
- Variable Files
IMAGES
VIDEO
COMMENTS
From the version above 4, robot framework supporting assignment inside IF-Else refer: https://robocorp.com/docs/languages-and-frameworks/robot-framework/conditional-execution IF ${random} == ${NUMBER_TO_PASS_ON} ${var}= set variable 10 ELSE IF ${random} > ${NUMBER_TO_PASS_ON} ${var}= set variable ${random} ELSE ${var}= set variable ${NUMBER_TO ...
In general, for someone stumbling on this post, the Run Keyword If combined with ELSE Set Variable is a very powerful construct to set/change a variable - based on the fact that it not only runs a keyword(s) conditionally, but also propagates its return values back to the stack.
I try to add some logic using IF ELSE statement in my tests and stuck with assignments the variable the IF statement. Brief overview: I am testing create functionality in CMS, new record ‘name’ have to be unique value.
In this article, we will look in detail at how we can do conditional testing(or If-Else) in the Robot framework. Let’s write a simple program to deep dive. Steps: 1. Go to Wikipedia.org 2. Search for Wikivoyage, if found, verify the title, End test 3. Search for Wikivoyage, if not found, click on Wiktionary, verify the title, End test
I’m trying to write if and else if control loops with multiple conditions. For example, IF “abc” in $ {HOST} or “def” in $ {HOST} or “hij” in $ {HOST} . . . do something. ELSE IF IF “tuv” in $ {HOST} or “xyz” in $ {HOST} . . . do something else. ELSE. . . . do nothing and end.
python: IF ELSE in robot framework with variables assignmentThanks for taking the time to learn more. In this video I'll go through your question, provide va...
In this Robot Framework Tutorial, we will understand how to use if-else in robot framework. * If/Else – Conditional statement which runs a different set of statements depending on whether an expression is true or false. * Robot Framework has a builtin library for this functionality.
Variable assignment is the process of storing a value in a variable. You can assign values to variables in several ways: Scalar Variables. Scalar variables hold single values such as strings or numbers. You can assign values to scalar variables using the Set Variablekeyword or the ${VARNAME}=syntax.
Hello, I have installed RF 4.1.3 and suddenly, my code with “ELSE IF” keyword are underlined, as if it was an error. But my test works. I suppose that with the update of RF (RF 3 to RF 4), the way of using this keyword …
Besides the *** Variables *** section, variables can also be set dynamically in test cases and keywords. Variables are set by the return value of a keyword. There are some Keywords which set variables values explicitly, like. Set Variable; Set Test Variable; Set Suite Variable; Set Global Variable