Core Robot Framework Concepts for Beginners
Welcome to Phase 2 of Robot Framework tutorial series. In this article, we will learn the most important core concepts of Robot Framework with practical examples.
- Sections in Robot Framework
- Variables in Robot Framework
- Keywords
- Control Statements
- Tags
- Setup & Teardown
4. Sections in Robot Framework
A Robot Framework file is divided into different sections. Each section has its own purpose.
Main Sections in Robot Framework
| Section | Purpose |
|---|---|
| *** Settings *** | Used for library imports and resource files |
| *** Variables *** | Used to store variables |
| *** Test Cases *** | Contains all test cases |
| *** Keywords *** | Contains reusable user-defined keywords |
Example of All Sections
*** Settings ***
Library SeleniumLibrary
*** Variables ***
${URL} https://google.com
*** Test Cases ***
Open Google
Open Browser ${URL} chrome
Close Browser
*** Keywords ***
Launch Application
Open Browser ${URL} chrome
Why Sections are Important?
- Improve code readability
- Easy framework maintenance
- Better test organization
- Reusable automation code
5. Variables in Robot Framework
Variables are used to store data in Robot Framework. Using variables makes automation scripts reusable and easy to maintain.
Types of Variables
| Variable Type | Syntax |
|---|---|
| Scalar Variable | ${variable} |
| List Variable | @{list} |
| Dictionary Variable | &{dictionary} |
Scalar Variable Example
*** Variables ***
${NAME} Chandan
*** Test Cases ***
Print Name
Log ${NAME}
List Variable Example
*** Variables ***
@{COLORS} Red Green Blue
*** Test Cases ***
Print Colors
Log ${COLORS}[0]
Log ${COLORS}[1]
Dictionary Variable Example
*** Variables ***
&{USER} username=admin password=admin123
*** Test Cases ***
Login Data
Log ${USER}[username]
Log ${USER}[password]
Advantages of Variables
- Avoid hardcoded values
- Easy maintenance
- Reusable scripts
- Supports dynamic execution
6. Keywords in Robot Framework
Keywords are the most important concept in Robot Framework. Everything in Robot Framework works using keywords.
Types of Keywords
| Keyword Type | Description |
|---|---|
| Built-in Keywords | Provided by Robot Framework |
| Library Keywords | Provided by libraries like SeleniumLibrary |
| User-defined Keywords | Created by users |
Built-in Keyword Example
*** Test Cases ***
Example Test
Log Welcome to Way2Testing
User-defined Keyword Example
*** Test Cases ***
Login Test
Launch Browser
*** Keywords ***
Launch Browser
Open Browser https://google.com chrome
Advantages of Keywords
- Code reusability
- Easy maintenance
- Readable test cases
- Reduced duplicate code
7. Control Statements in Robot Framework
Control statements are used to execute automation flow based on conditions and loops.
IF ELSE Condition Example
*** Variables ***
${AGE} 20
*** Test Cases ***
Age Validation
IF ${AGE} > 18
Log Adult User
ELSE
Log Minor User
END
FOR Loop Example
*** Variables ***
@{FRUITS} Apple Mango Banana
*** Test Cases ***
Print Fruits
FOR ${fruit} IN @{FRUITS}
Log ${fruit}
END
WHILE Loop Example
*** Test Cases ***
Counter Example
${count}= Set Variable 1
WHILE ${count} < 5
Log ${count}
${count}= Evaluate ${count}+1
END
Why Control Statements are Important?
- Dynamic execution
- Data-driven testing
- Decision making
- Loop execution
8. Tags in Robot Framework
Tags are used to categorize test cases. They help in executing selected test cases.
Tag Example
*** Test Cases ***
Login Test
[Tags] smoke regression
Log Login Successful
Run Test Using Tag
robot -i smoke test.robot
Exclude Tag Example
robot -e regression test.robot
Commonly Used Tags
| Tag | Purpose |
|---|---|
| smoke | Basic validation tests |
| regression | Complete feature testing |
| sanity | Quick validation after fixes |
9. Setup & Teardown in Robot Framework
Setup and Teardown are used to execute pre-condition and post-condition steps.
What is Setup?
Setup runs before test execution.
What is Teardown?
Teardown runs after test execution.
Test Setup & Teardown Example
*** Settings ***
Test Setup Open Browser https://google.com chrome
Test Teardown Close Browser
*** Test Cases ***
Search Test
Input Text name=q Robot Framework
Suite Setup & Suite Teardown
Suite Setup runs once before all test cases. Suite Teardown runs once after all test cases.
Example
*** Settings *** Suite Setup Log Starting Test Suite Suite Teardown Log Closing Test Suite
Advantages of Setup & Teardown
- Avoid duplicate code
- Centralized execution flow
- Easy maintenance
- Better framework structure
Best Practices for Beginners
- Use reusable keywords
- Avoid hardcoded values
- Use proper tags
- Create modular test cases
- Use setup and teardown properly
Conclusion
In this tutorial, we learned the core concepts of Robot Framework:
- Sections
- Variables
- Keywords
- Control Statements
- Tags
- Setup & Teardown
These concepts are extremely important before starting Selenium automation using Robot Framework.
In the next tutorial, we will learn:
- SeleniumLibrary Installation
- Browser Automation
- Locators
- Web UI Automation
Frequently Asked Questions (FAQ)
What are keywords in Robot Framework?
Keywords are reusable actions used to perform automation tasks in Robot Framework.
Why are variables important?
Variables help store reusable and dynamic data in automation scripts.
What is the use of tags?
Tags help categorize and selectively execute test cases.
What is Setup and Teardown?
Setup executes before tests and Teardown executes after tests.
No comments:
Post a Comment