Advanced Topics in Robot Framework
Welcome to Phase 6 of Robot Framework Tutorial Series. In this tutorial, we will learn advanced Robot Framework concepts used in real-time automation frameworks.
These topics are very important for experienced automation testers and enterprise-level framework development.
Topics Covered
- Exception Handling
- Parallel Execution
- Database Testing
- File Handling
- Dynamic Execution Concepts
21. Exception Handling in Robot Framework
Exception handling is used to manage failures during test execution. Without proper exception handling, automation scripts may stop unexpectedly.
Why Exception Handling is Important?
- Handle unexpected failures
- Continue execution after failure
- Improve framework stability
- Generate proper logs
Run Keyword And Ignore Error
This keyword executes another keyword and ignores failure if it occurs.
Example
*** Test Cases ***
Handle Failure Example
${status} ${message}=
Run Keyword And Ignore Error
... Click Element id=loginButton
Log ${status}
Log ${message}
Possible Output
PASS FAIL
TRY EXCEPT Block
Modern Robot Framework versions support TRY EXCEPT blocks similar to Python.
TRY EXCEPT Example
*** Test Cases ***
Login Validation
TRY
Click Element id=loginButton
EXCEPT
Log Login button not found
END
TRY EXCEPT FINALLY Example
*** Test Cases ***
Application Handling
TRY
Open Browser https://google.com chrome
EXCEPT
Log Browser launch failed
FINALLY
Close Browser
END
Advantages of TRY EXCEPT
- Better failure handling
- Improved debugging
- Stable automation execution
- Cleaner framework design
22. Parallel Execution in Robot Framework
Parallel execution means running multiple test cases simultaneously.
It helps reduce execution time significantly.
Why Parallel Execution is Important?
- Faster test execution
- Reduced CI/CD execution time
- Better resource utilization
- Large suite execution support
Install Pabot
Pabot is the most popular tool for parallel execution in Robot Framework.
Installation Command
pip install robotframework-pabot
Run Tests in Parallel
Basic Command
pabot tests/
Run with Multiple Processes
pabot --processes 4 tests/
How It Works?
The above command executes tests using 4 parallel processes.
Real-Time Example
Suppose execution time is:
- Sequential Execution → 2 Hours
- Parallel Execution → 30 Minutes
Best Practices for Parallel Execution
- Avoid shared test data
- Use independent test cases
- Use thread-safe execution
- Generate separate logs if needed
23. Database Testing in Robot Framework
Database testing is used to validate backend database records.
Automation scripts can:
- Connect database
- Execute SQL queries
- Validate data
- Insert/update records
Install DatabaseLibrary
pip install robotframework-databaselibrary
Import DatabaseLibrary
*** Settings *** Library DatabaseLibrary
Connect Database Example
Connect To Database ... pymysql ... database=testdb ... user=root ... password=root123 ... host=localhost ... port=3306
Execute SQL Query Example
${result}=
Query
... SELECT * FROM users
Validate Database Result
Should Not Be Empty ${result}
Complete Database Validation Example
*** Settings ***
Library DatabaseLibrary
*** Test Cases ***
Validate User Record
Connect To Database
... pymysql
... database=testdb
... user=root
... password=root123
... host=localhost
... port=3306
${result}=
Query
... SELECT name FROM users WHERE id=1
Log ${result}
Disconnect From Database
Advantages of Database Testing
- Backend validation
- Data consistency check
- End-to-end testing support
- Improved test coverage
24. File Handling in Robot Framework
File handling is commonly used for:
- Read test data
- Write logs
- Store runtime data
- JSON/CSV processing
Read Text File Example
${content}=
Get File
... sample.txt
Log ${content}
Write File Example
Create File ... output.txt ... Welcome to Way2Testing
Append Data Example
Append To File ... output.txt ... \nAutomation Testing
JSON File Handling Example
Suppose we have:
data.json
Sample JSON File
{
"username": "admin",
"password": "admin123"
}
Read JSON Using Python
import json
def read_json():
with open("data.json") as file:
data = json.load(file)
return data
Use Python Function in Robot Framework
*** Settings ***
Library custom_library.py
*** Test Cases ***
Read JSON Test
${data}= Read Json
Log ${data}[username]
CSV File Handling Example
username,password admin,admin123 user1,password1
Advantages of File Handling
- Dynamic test data management
- External configuration support
- Runtime data storage
- Reusable automation data
25. Dynamic Execution Concepts
Dynamic execution concepts are advanced framework techniques used in enterprise automation frameworks.
Common Dynamic Execution Features
- Resume execution
- Retry failed tests
- Dynamic test selection
- Runtime data handling
- Execution control
Retry Failed Test Example
Robot Framework supports rerunning failed tests.
Command Example
robot --rerunfailed output.xml tests/
Merge Execution Reports
rebot --merge output.xml rerun.xml
Dynamic Variable Example
${current_time}=
Get Time
Log ${current_time}
Resume Execution Concept
In advanced frameworks, execution can restart from failed step or failed test case.
This is useful in:
- Long-running execution
- Large regression suites
- CI/CD pipelines
Runtime Data Example
*** Variables ***
${EXECUTION_STATUS} PASS
Listener Concept
Listeners are used to monitor execution events dynamically.
Common Listener Use Cases
- Custom logging
- Screenshot capture
- Dynamic reporting
- Execution monitoring
Advanced Framework Flow
Test Execution
↓
Exception Handling
↓
Parallel Execution
↓
Database Validation
↓
File Processing
↓
Dynamic Execution Handling
Best Practices for Advanced Frameworks
- Use proper exception handling
- Avoid unnecessary hardcoded waits
- Keep test cases independent
- Use external test data
- Implement proper logging
Conclusion
In this tutorial, we learned:
- Exception Handling
- Parallel Execution
- Database Testing
- File Handling
- Dynamic Execution Concepts
These advanced concepts are heavily used in enterprise-level automation frameworks. Learning these topics will help you create stable, scalable and industry-ready automation solutions.
Frequently Asked Questions (FAQ)
What is exception handling in Robot Framework?
Exception handling manages failures during automation execution without stopping complete execution.
What is Pabot?
Pabot is a tool used for parallel execution in Robot Framework.
Can Robot Framework connect with database?
Yes, Robot Framework supports database testing using DatabaseLibrary.
Why is file handling important?
File handling helps manage external data, logs and runtime execution data.
No comments:
Post a Comment