Robot Framework is a generic open source automation framework. It can be used for test automation and robotic process automation (RPA). It uses a keyword-driven approach, making it easy to write and understand test cases. Robot Framework is extensible, meaning you can add custom libraries and tools to it. It is often used for acceptance testing and acceptance test-driven development (ATDD).
Robot Framework is a popular open-source automation framework used for :
Here's what makes Robot Framework special :
.robot
files).Given the user navigates to the login page
When they enter valid credentials
Then they should be logged in successfully
Robot Framework is quite versatile and can be used for various testing purposes. Here are some of the common types of test cases you can create with it:
1. Functional Tests:
2. UI Tests:
3. API Tests:
4. Data-Driven Tests:
5. Performance Tests:
6. Acceptance Tests:
7. Regression Tests:
These are just some of the common types of test cases you can create with Robot Framework. Its flexibility and extensibility allow you to adapt it to various testing needs and scenarios.
You can install Robot Framework using pip, the Python package installer. Here's a breakdown of how to do it, along with some best practices:
1. Prerequisites:
Python: Robot Framework is written in Python, so you'll need Python installed on your system. It's generally recommended to use a relatively recent, stable version of Python. You can check if you have Python installed by opening a terminal or command prompt and typing python --version
(or python3 --version
depending on your system). If you don't have it, download it from python.org.
pip: pip usually comes bundled with Python. You can check if you have pip by typing pip --version
(or pip3 --version
) in your terminal. If it's missing or outdated, you can usually upgrade it with python -m ensurepip --upgrade
(or the equivalent python3
command).
2. Installation:
The simplest way to install Robot Framework is using pip :
pip install robotframework
If you're using Python 3, you might need to use pip3
instead of pip
.
pip3 install robotframework
3. Verification:
After the installation is complete, you can verify it by checking the Robot Framework version:
robot --version
This should print the installed version of Robot Framework.
4. Important Libraries (Highly Recommended):
Robot Framework itself provides the core functionality, but you'll almost certainly want to install some additional libraries for specific testing tasks:
* SeleniumLibrary: For web UI testing.
pip install robotframework-seleniumlibrary
* RequestsLibrary: For API testing :
pip install robotframework-requests
* BuiltIn: This library is already included with Robot Framework and provides essential keywords for various tasks (e.g., variable manipulation, flow control, etc.). You don't need to install it separately.
5. RIDE (Optional but Useful):
RIDE (Robot Integrated Development Environment) is a lightweight IDE that can make it easier to write and run Robot Framework tests. It's not strictly required, but many people find it helpful. You can install it like this :
pip install robotframework-ride
Be aware that RIDE might have some dependencies that need to be installed separately. If you encounter issues, refer to the RIDE documentation for troubleshooting.
6. Virtual Environments (Best Practice):
It's highly recommended to use virtual environments when working with Robot Framework (and Python projects in general). This isolates your project's dependencies and prevents conflicts between different projects. Here's how to do it :
* Create a virtual environment :
python3 -m venv .venv # Creates a virtual environment named ".venv"
(You can name the environment whatever you like.)
* Activate the virtual environment:
.venv\Scripts\activate
source .venv/bin/activate
.pip install robotframework
pip install robotframework-seleniumlibrary
# ... other libraries
By following these steps, you'll have a clean and working Robot Framework installation, ready for your automation projects. Remember to activate your virtual environment whenever you work on a Robot Framework project to keep your dependencies managed effectively.
Robot Framework's default test case file format is plain text, using a tabular structure. This means you organize your test data (test cases, keywords, variables, etc.) in tables within text files.
Here's a simple example of what a Robot Framework test case file might look like :
***Settings***
Library SeleniumLibrary
***Variables***
${BROWSER} chrome
${URL} https://www.example.com
***Test Cases***
Open Browser and Navigate to Website
Open Browser ${URL} ${BROWSER}
Maximize Browser Window
Title Should Be Example Domain
***Keywords***
Open Browser and Navigate to Website
[Arguments] ${url} ${browser}
Open Browser ${url} ${browser}
Maximize Browser Window
Key aspects of the format:
***Settings***
, ***Variables***
, ***Test Cases***
, and ***Keywords***
, to organize different types of data.Why this format?
While plain text with a tabular structure is the default and most common format, Robot Framework also supports other formats like:
However, the plain text format remains the most widely used due to its simplicity and readability.
You're right to ask about supported file formats! While Robot Framework is most known for its plain text, tabular format, it's actually quite flexible. Here's a breakdown of the file formats you can use for your test cases:
1. Plain Text (Tabular Format):
|
) as separators between columns in the tables..txt
Example:
***Settings***
Library SeleniumLibrary
***Test Cases***
Open Browser and Navigate to Website
Open Browser ${URL} ${BROWSER}
Maximize Browser Window
Title Should Be Example Domain
2. HTML:
.html
3. TSV (Tab Separated Values):
.tsv
4. reStructuredText:
.robot.rst
, .rst
, .rest
5. JSON:
.json
Important Notes:
If you're starting out with Robot Framework, I'd recommend sticking with the plain text format. It's the easiest to learn and use, and it's well-suited for most testing scenarios.
***Settings***
Library SeleniumLibrary
***Variables***
${BROWSER} chrome
${URL} https://www.example.com
***Test Cases***
Open Browser and Navigate to Website
Open Browser ${URL} ${BROWSER}
Maximize Browser Window
Title Should Be Example Domain?
Explanation:
Sections: Robot Framework files are divided into sections, denoted by triple asterisks. The most important sections for a basic test case are:
***Settings***
: Used to import libraries, set variables, and configure other settings.***Variables***
: Defines variables that can be used in your test cases. This helps make your tests more readable and maintainable.***Test Cases***
: Contains the actual test cases.Keywords: Robot Framework uses keywords to represent actions or steps. Keywords can be:
Log
, Should Be Equal
).Open Browser
, Maximize Browser Window
from SeleniumLibrary).Test Case Definition:
Open Browser and Navigate to Website
). This name should be descriptive.Keyword Arguments: Many keywords take arguments. For example, Open Browser
takes the URL and browser name as arguments. Arguments are separated by two spaces.
Variables: Variables are defined using the ${variable_name}
syntax. They are used to store values that can be reused in your test cases. This makes your tests more flexible and easier to maintain.
Example Breakdown:
***Settings***
: We import the SeleniumLibrary
, which provides keywords for browser automation.***Variables***
: We define two variables: ${BROWSER}
and ${URL}
. This makes it easy to change the browser or URL later without modifying the test case itself.***Test Cases***
: We define a test case named "Open Browser and Navigate to Website."Open Browser ${URL} ${BROWSER}
: This line uses the Open Browser
keyword from SeleniumLibrary to open the specified URL in the specified browser. The variables ${URL}
and ${BROWSER}
are used to pass the values.Maximize Browser Window
: This line uses the Maximize Browser Window
keyword (also from SeleniumLibrary) to maximize the browser window.Title Should Be Example Domain
: This line uses the Title Should Be
keyword to verify that the title of the web page is "Example Domain." This is an assertion—a check to see if the actual result matches the expected result. If the title is not "Example Domain," the test will fail.Key Principles:
In Robot Framework, test suites are a way to group related test cases together. Think of them as containers or folders that organize your tests. They provide structure and make it easier to manage and run large sets of tests.
Here's a breakdown of what test suites are and why they are important:
What they are:
Why they are important:
How to create and use them:
Directory structure: The most common way to define suites is through the directory structure of your test files. Each directory represents a test suite. Test cases within that directory belong to that suite.
Suite names: The name of the directory becomes the name of the test suite.
Sub-suites: If a directory contains other directories, those become sub-suites.
Running suites: You can specify the directory (suite) to run when you execute Robot Framework. Robot Framework will then discover and execute all the test cases within that directory and any sub-directories.
Example:
Let's say you have the following directory structure :
my_project/
??? tests/
? ??? login/
? ? ??? login_tests.robot
? ??? product_search/
? ? ??? search_tests.robot
? ??? checkout/
? ??? checkout_tests.robot
In this example:
tests
is the top-level suite.login
, product_search
, and checkout
are sub-suites.login_tests.robot
, search_tests.robot
, and checkout_tests.robot
contain the individual test cases.To run all the tests, you would run Robot Framework on the tests
directory :
robot tests
This would execute all the test cases in all the suites. You could also run a specific sub-suite, like :
robot tests/login
This would only run the tests in the login
suite.
There are several ways to execute test scripts in Robot Framework. Here's a breakdown of the most common methods:
1. From the command line:
This is the most common way to execute Robot Framework tests. You use the robot
command followed by the path to your test suite or test case file.
* Running a single test case file :
robot my_test_case.robot
* Running all test cases in a directory (test suite) :
robot my_test_suite_directory/
* Running multiple test case files or directories :
robot test_case_1.robot test_suite_directory/ test_case_2.robot
* Running tests based on tags :
robot -i tag1 -i tag2 my_test_suite_directory/ # Include tests with tag1 AND tag2
robot -e tag3 my_test_suite_directory/ # Exclude tests with tag3
* Setting variables from the command line :
robot -v my_variable:my_value my_test_case.robot
* Specifying the output directory :
robot -d results my_test_suite_directory/
* Generating logs and reports: Robot Framework automatically generates HTML reports and logs. You can customize their location and names.
2. Using the ride
command (if using RIDE):
If you're using the RIDE IDE, you can execute tests directly from the RIDE interface. RIDE provides a graphical interface for running tests, viewing results, and debugging. You typically right-click on a test suite or test case and select "Run."
3. Programmatically (using the Robot Framework API):
You can embed Robot Framework execution within your Python code. This is useful for integrating Robot Framework with other tools or for more complex execution scenarios.
from robot.api import run_from_cli
if __name__ == '__main__':
run_from_cli(['my_test_case.robot']) # Pass arguments as a list
# Or run_from_cli(['-d', 'results', 'my_test_suite_directory/'])
4. Using a build tool (e.g., Jenkins, Maven, Gradle):
For continuous integration and continuous delivery (CI/CD), you'll often integrate Robot Framework execution into your build process using tools like Jenkins, Maven, or Gradle. These tools allow you to automate test execution as part of your build pipeline.
Key options and considerations:
-d
(outputdir): Specifies the directory where Robot Framework should write its output files (logs, reports, etc.).-i
(include): Runs tests whose tags match the given pattern.-e
(exclude): Excludes tests whose tags match the given pattern.-v
(variable): Sets a variable that can be used in your tests.-l
(log level): Sets the log level (e.g., DEBUG, INFO, WARN, ERROR).-r
(report): Sets the name of the generated report file.-g
(log): Sets the name of the generated log file.--processes
or --threads
options.In Robot Framework, the ***Settings***
, ***Test Cases***
, and ***Keywords***
sections are fundamental for structuring your test automation scripts. Each section serves a distinct purpose:
1. ***Settings***
:
SeleniumLibrary
for web UI testing, RequestsLibrary
for API testing). This is essential, as without importing the necessary libraries, you won't be able to use their keywords.***Settings***
Library SeleniumLibrary
Resource common_keywords.robot
Test Setup Open Browser and Navigate
Test Teardown Close Browser
2. ***Test Cases***
:
***Test Cases***
Login with valid credentials
Input Text username_field ${VALID_USERNAME}
Input Password password_field ${VALID_PASSWORD}
Click Button login_button
Title Should Be Welcome Page
3. ***Keywords***
:
***Keywords***
Open Browser and Navigate
[Arguments] ${url}
Open Browser ${url} chrome
Maximize Browser Window
In summary:
***Settings***
: Configures the test environment and imports necessary libraries.***Test Cases***
: Contains the actual test scenarios, using keywords to define the steps.***Keywords***
: Defines reusable blocks of steps (like functions) that can be called from test cases or other keywords.Variables are essential in Robot Framework for making your tests more flexible, readable, and maintainable. Here's a breakdown of how you handle them:
***Variables***
section: This is the most common way to define variables. You list the variable name and its value :***Variables***
${BROWSER} chrome
${URL} https://www.example.com
${USERNAME} myuser
@{COLORS} red green blue # List variable
&{USER_DATA} name=John age=30 # Dictionary variable
robot -v BROWSER:firefox -v URL:https://staging.example.com my_test.robot?
* Within test cases or keywords: You can use keywords like Set Variable
, Set Test Variable
, Set Suite Variable
, or Set Global Variable
to define variables dynamically during test execution :
***Test Cases***
My Test Case
${dynamic_value} Set Variable some value
Log ${dynamic_value}
* From external files: You can load variables from resource files (.robot
), Python files (.py
), or YAML files (.yaml
). This is great for managing larger sets of variables.
${...}
): These hold a single value (string, number, etc.).${USERNAME} myuser?
* List variables (@{...}
): These hold a list of values.
@{COLORS} red green blue
* Dictionary variables (&{...}
): These hold key-value pairs.
&{USER_DATA} name=John age=30
* Environment variables (%env{...}
): These access environment variables from your operating system.
%env{PATH}
${USERNAME}
) to access the variable's value.***Test Cases***
Login User
Input Text username_field ${USERNAME}?
* In keyword arguments: You can pass variables as arguments to keywords .
***Keywords***
My Keyword
[Arguments] ${message}
Log ${message}
***Test Cases***
My Test Case
My Keyword ${USERNAME}
* In strings: You can embed variables within strings.
${greeting} Set Variable Hello, ${USERNAME}!
Log ${greeting}
Robot Framework supports several types of variables to handle different kinds of data and scenarios in your tests. Here's a breakdown of the main variable types:
1. Scalar Variables (${...}
):
${USERNAME} myuser
${AGE} 30
${IS_ADMIN} True
2. List Variables (@{...}
):
@{COLORS} red green blue
@{USER_ROLES} admin editor viewer
3. Dictionary Variables (&{...}
):
&{USER_DATA} name=John age=30 city=New York
&{PRODUCT} id=123 name=Laptop price=999
4. Environment Variables (%env{...}
):
%{PATH}
%{USERNAME}
5. Built-in Variables:
${CURDIR}
: The absolute path to the directory where the test data file is located.${TEMPDIR}
: The absolute path to the system temporary directory.${EXECDIR}
: The absolute path to the directory where test execution was started from.6. Extended Variable Syntax:
Key Considerations:
${...}
, @{...}
, &{...}
, and %env{...}
are used to identify the type of the variable.Robot Framework's architecture is designed for extensibility and flexibility. Here's a high-level overview:
Test Data: This is where you define your test cases, keywords, and other test-related information. It's typically written in plain text files using a tabular format, but other formats like HTML, TSV, reStructuredText, and JSON are also supported. The test data is the input to the Robot Framework engine.
Robot Framework Engine: This is the core of Robot Framework. It's responsible for parsing the test data, executing the tests, and generating reports. The engine doesn't actually perform the actions in your tests (like clicking buttons or making API calls); it delegates that to libraries.
Libraries: Libraries are collections of keywords that provide the actual functionality for your tests. They are the "action" part of your tests. There are two main types:
BuiltIn
, Collections
, OperatingSystem
, String
)SeleniumLibrary
: For web UI testing.RequestsLibrary
: For API testing.DatabaseLibrary
: For database testing.Resource Files: Resource files are like mini-libraries. They contain reusable variables, keywords, and settings that can be imported into your test suites. This helps you organize your test data and avoid duplication.
Listeners: Listeners are components that can be used to monitor and interact with the test execution process. They can be used for logging, reporting, or integrating with other tools. They are essentially callbacks that are triggered at various points during the test run.
Runners: Robot Framework can be executed in several ways. The most common is using the robot
command-line tool. RIDE (Robot Integrated Development Environment) provides a graphical interface for running tests. You can also embed the Robot Framework engine within your Python code to execute tests programmatically.
Reports and Logs: After the test execution is complete, Robot Framework generates HTML reports and logs. These provide detailed information about the test results, including which tests passed or failed, the steps that were executed, and any errors that occurred.
Workflow:
Key Architectural Principles:
Robot Framework | Selenium |
---|---|
Robot is a Python acceptance/functional testing framework. Robot is a simple plain text-based automated test framework that may be readily extended with Python or Java frameworks. It can run on both the. net-based IronPython and the Java-based Jython. | Selenium is an open-source web application testing tool. Selenium is a robust testing tool that can transmit common Python commands to a variety of browsers, regardless of design differences. It also includes extensions to simulate user interaction with browsers, as well as a distribution server for scaling browser allocation and infrastructure for W3C WebDriver implementations, which allow you to develop interchangeable code for all major web browsers. |
Robot has a large library and can be readily coupled with Selenium to automate the testing of front-end components in the browser. | It's mostly a front-end component and functionality testing tool for browsers. |
In Robot, there is no built-in way to work with fixtures, but it can be integrated with unittest and used that way. | By using PyTest to write your Selenium WebDriver tests, you gain access to Pytest's rich fixture model. |
Robot Framework allows you to define a set of fixed, particular data states for a collection of tests (group-fixtures) by combining it with unittest. | When using Pytest's fixture model, one can group fixtures. |
Test data generators are supported. Input data for tests is generated by data generators. The test is then performed for each piece of input data generated in this manner. The Robot Framework Faker library is a library created by Robot. It has 147 keywords that are used to generate random test data. | Selenium can generate test data by making use of a library like Faker or Fake-factory. |
Mocks are objects that mimic the actions of real-world items. Using mocks allows you to test a specific section of code in isolation (with other parts mocked when needed). For mocking, Robot Framework can use Python's mock library. | By default, Selenium has support for mocking. |
Robot Framework | Cucumber |
---|---|
It is based on the Python programming language. | It is based on the Ruby programming language. |
The robot is a Python acceptance/functional testing framework. The robot is a simple plain text-based automated test framework that may be readily extended with Python or Java frameworks. It can run on both the. net-based IronPython and the Java-based Jython. | Cucumber is a behaviour-driven development automation tool. The specifications are written in plain English, making them understandable to all parties involved. Cucumber Framework can also be used with languages other than Ruby, such as Java, JavaScript, and Scala. |
The robot framework allows you to test code on a client, such as a web browser. The robot has a large library and can be readily coupled with Selenium to automate the testing of front-end components in the browser. | Cucumber and Selenium can be used to test the front-end, such as the GUI; they work well together to test your front-end. |
In Robot Framework, group fixtures can be made by integrating them with the unit tests. |
Group Fixtures may be made in two steps using the cucumber extension Aruba:
|
The Robot Framework has Apache License 2.0. | The Cucumber Framework has an MIT License. |
The Robot Framework can be used to generate a test suite. |
Cucumber allows you to arrange tests using tag Cucumber feature files or individual tests. Then, during test execution, you may specify the tag (grouped) tests to execute using a Cucumber tagged expression. |
Library
setting in the ***Settings***
section of your test data file. Here's a detailed explanation:
1. Basic Import:
The most common way to import a library is simply to specify its name after the Library
keyword.
***Settings***
Library SeleniumLibrary
This imports the SeleniumLibrary
, which is commonly used for web browser automation.
2. Library with Arguments:
Some libraries require arguments during import. You provide these arguments after the library name, separated by two spaces.
***Settings***
Library MyLibrary arg1 arg2 arg3
3. Library Aliases:
You can give a library an alias if you want to refer to it by a different name in your tests. This can be useful if you have libraries with similar keyword names or if you just want a shorter name.
***Settings***
Library SeleniumLibrary WITH NAME Browser
Now, you would use Browser
instead of SeleniumLibrary
when calling keywords from this library.
4. Importing Multiple Libraries:
You can import multiple libraries in the ***Settings***
section. Each library should be on a separate line, preceded by the Library
keyword.
***Settings***
Library SeleniumLibrary
Library RequestsLibrary
Library OperatingSystem
5. Importing Libraries from a Path:
If your library is not installed in a standard location, you can provide the full path to the library.
***Settings***
Library /path/to/my/library.py
6. Importing Resource Files (for Keywords):
While not strictly "libraries," resource files are often used in a similar way to extend Robot Framework's capabilities. They contain reusable keywords, variables, and settings. You import them using the Resource
setting.
***Settings***
Resource my_keywords.robot
7. Library Search Order:
Robot Framework searches for libraries in the following order:
ROBOT_LIBRARY_PATH
environment variable.
8. Best Practices:
The BuiltIn library in Robot Framework is like a toolbox full of essential tools that you'll use constantly. It's automatically included in every Robot Framework test suite, so you don't need to explicitly import it. Think of it as the foundation upon which you build your tests.
Here's a breakdown of its key purposes and some commonly used keywords:
Purpose:
The BuiltIn library provides a set of generic keywords that are frequently needed in test automation. These keywords cover a wide range of tasks, including:
You can pass arguments to keywords in Robot Framework using the [Arguments]
setting within the keyword definition. Here's a breakdown of how it works:
1. Defining Keywords with Arguments:
Inside the ***Keywords***
section, when you define a keyword that accepts arguments, you use the [Arguments]
setting followed by the argument names.
***Keywords***
My Keyword
[Arguments] ${arg1} ${arg2}
Log Argument 1: ${arg1}
Log Argument 2: ${arg2}
In this example, My Keyword
is defined to accept two arguments: ${arg1}
and ${arg2}
.
2. Calling Keywords with Arguments:
When you call a keyword that has arguments, you provide the values for those arguments after the keyword name, separated by two spaces.
***Test Cases***
My Test Case
My Keyword value1 value2
Here, My Keyword
is called with the values "value1" and "value2" for ${arg1}
and ${arg2}
, respectively.
3. Default Values for Arguments:
You can provide default values for arguments. If a value is not provided when the keyword is called, the default value will be used.
***Keywords***
My Keyword
[Arguments] ${arg1} ${arg2}=default_value
Log Argument 1: ${arg1}
Log Argument 2: ${arg2}
***Test Cases***
My Test Case
My Keyword value1 # ${arg2} will use the default value "default_value"
My Keyword value1 another_value # ${arg2} will be "another_value"
4. Named Arguments:
You can use named arguments to specify the values for arguments by their names. This can improve readability, especially when a keyword has many arguments.
***Keywords***
My Keyword
[Arguments] ${arg1} ${arg2} ${arg3}
Log Argument 1: ${arg1}
Log Argument 2: ${arg2}
Log Argument 3: ${arg3}
***Test Cases***
My Test Case
My Keyword arg2=second_value arg1=first_value arg3=third_value
5. Variable Arguments (*args
):
You can use *args
to define a keyword that can accept a variable number of arguments. These arguments are passed as a list.
***Keywords***
My Keyword
[Arguments] *args
Log Many @{args}
***Test Cases***
My Test Case
My Keyword one two three
6. Keyword Arguments (**kwargs
):
You can use **kwargs
to define a keyword that can accept a variable number of keyword arguments. These arguments are passed as a dictionary.
***Keywords***
My Keyword
[Arguments] **kwargs
Log ${kwargs}
***Test Cases***
My Test Case
My Keyword name=John age=30 city=New York
7. Combining Argument Types:
You can combine different argument types in a single keyword definition.
***Keywords***
My Keyword
[Arguments] ${arg1} ${arg2}=default *args **kwargs
Log ${arg1}
Log ${arg2}
Log Many @{args}
Log ${kwargs}
FOR
Loops :FOR
Loop: This iterates over a range of values.***Test Cases***
Example Loop
FOR ${i} IN RANGE 1 6
Log Value of i: ${i}
END?
This loop will execute 5 times, with ${i}
taking values from 1 to 5.
* Iterating over a List: You can loop through items in a list.
***Variables***
@{FRUITS} apple banana orange
***Test Cases***
Fruit Loop
FOR ${fruit} IN @{FRUITS}
Log Fruit: ${fruit}
END
This will print each fruit in the list.
* Iterating over a Dictionary: Loop through keys or key-value pairs in a dictionary.
***Variables***
&{USER} name=John age=30 city=New York
***Test Cases***
User Data Loop
FOR ${key} IN &{USER}
Log Key: ${key}, Value: ${USER}[${key}]
END
FOR ${key} ${value} IN &{USER} # Loop through key-value pairs
Log ${key}: ${value}
END
* IN ZIP
for Parallel Iteration: If you need to iterate over multiple lists simultaneously, use IN ZIP
.
***Variables***
@{NAMES} John Jane Mike
@{AGES} 25 32 28
***Test Cases***
Parallel Loop
FOR ${name} ${age} IN ZIP @{NAMES} @{AGES}
Log Name: ${name}, Age: ${age}
END
Exit For Loop If
: Use this to exit the loop based on a condition.***Test Cases***
Conditional Exit
FOR ${i} IN RANGE 1 10
Exit For Loop If ${i} > 5
Log Value: ${i}
END?
The loop will stop when ${i}
becomes greater than 5.
* Continue For Loop If
: Skips the current iteration if a condition is met.
***Test Cases***
Skip Iteration
FOR ${i} IN RANGE 1 10
Continue For Loop If ${i} == 3
Log Value: ${i}
END
This will skip the iteration where ${i}
is 3.
You can nest loops for more complex scenarios.
***Test Cases***
Nested Loops
FOR ${i} IN RANGE 1 3
Log Outer loop: ${i}
FOR ${j} IN RANGE 1 3
Log Inner loop: ${j}
END
END
WHILE
Loops (Simulated) :Robot Framework doesn't have a direct WHILE
loop, but you can simulate it using FOR
loops and Exit For Loop If
.
***Test Cases***
Simulated While Loop
${condition} Set Variable True
FOR ${i} IN RANGE 1 1000 # Large range to ensure loop runs
Exit For Loop If '${condition}'=='False'
Log Looping...
${condition} Set Variable False # Example: Condition changes
END
Robot Framework provides several ways to handle exceptions, allowing you to gracefully manage errors and prevent your tests from abruptly stopping. Here's a breakdown of the common techniques:
1. Run Keyword And Expect Error
:
***Test Cases***
Verify Invalid Login
Run Keyword And Expect Error Invalid username or password Login User invalid_user wrong_password
Log Error message was expected
2. Run Keyword And Ignore Error
:
***Test Cases***
Attempt to Close Window
Run Keyword And Ignore Error Close Window
Log Window closing attempted
3. TRY / EXCEPT
Blocks:
try-except
blocks in Python.TRY
, and if any keyword within that block raises an exception, the execution jumps to the corresponding EXCEPT
block. You can have multiple EXCEPT
blocks to handle different types of exceptions or error messages.***Test Cases***
Handle Database Connection
TRY
Connect to Database
Query Database
EXCEPT Database connection failed
Log Failed to connect to the database
EXCEPT Database query error
Log Error during database query
ELSE
Log Database operations successful
FINALLY
Disconnect from Database # Always disconnect, even if there were errors
4. Assertions:
Should Be Equal ${actual_value} ${expected_value}
Element Should Be Visible login_button
5. Fatal Error
:
Fatal Error
, Robot Framework will stop the test run immediately.from robot.api.exceptions import FatalError
def my_keyword():
if some_critical_error:
raise FatalError("Critical error occurred!")
Both Should Be Equal
and Should Match
are used for verification in Robot Framework, but they serve different purposes and have distinct ways of comparing values:
Should Be Equal:
Should Be Equal
is case-sensitive for strings.${actual_value} Set Variable Hello
${expected_value} Set Variable Hello
Should Be Equal ${actual_value} ${expected_value} # Pass
${actual_value} Set Variable hello
${expected_value} Set Variable Hello
Should Be Equal ${actual_value} ${expected_value} # Fail (case-sensitive)
Should Match:
Should Be Equal
and is useful for verifying that a string conforms to a certain format or contains specific substrings.Should Match
is case-insensitive. You can use flags in your regular expression to make it case-sensitive if needed.${actual_value} Set Variable Hello, world!
${pattern} Set Variable Hello.*
Should Match ${actual_value} ${pattern} # Pass (matches the pattern)
${actual_value} Set Variable hello, world!
${pattern} Set Variable Hello.*
Should Match ${actual_value} ${pattern} # Pass (case-insensitive by default)
${actual_value} Set Variable hello, world!
${pattern} Set Variable (?i)Hello.* # (?i) flag for case-insensitive matching
Should Match ${actual_value} ${pattern} # Pass (case-insensitive)
Key Differences:
Feature | Should Be Equal | Should Match |
---|---|---|
Comparison | Strict equality | Pattern matching (regular expressions) |
Flexibility | Less flexible | More flexible |
Use cases | Exact value matching | Partial matching, format verification |
Case sensitivity | Case-sensitive (by default) | Case-insensitive (by default) |
Dynamic web elements are those whose attributes (like ID, name, or XPath) change during runtime. This makes it challenging to locate them reliably using static locators. Robot Framework, combined with SeleniumLibrary, provides several strategies to handle such elements:
1. Relative XPaths:
# Locate a stable ancestor element
${ancestor} Locator of stable ancestor
# Locate the dynamic element relative to the ancestor
${dynamic_element} XPath=//div[@id='${ancestor_id}']//button[contains(@class, 'dynamic-button')]
Click Element ${dynamic_element}
2. CSS Selectors with Partial Matching:
# If the class name changes but always contains 'product'
Click Element css=button[class*='product']
# Or if the ID always starts with 'item-'
Click Element css=div[id^='item-']
# Or if the ID always ends with '-details'
Click Element css=a[id$='-details']
3. Contains Text or Value:
Contains Text
or Element Should Contain
to locate it.Element Should Contain dynamic_element_locator Part of the stable text
4. Using Variables in Locators:
${product_id} Get Text product_id_element # Get ID dynamically
${locator} Set Variable id=product_${product_id}
Click Element ${locator}
5. Waiting Strategies:
Wait Until Element Is Visible
, Wait Until Page Contains Element
, Wait Until Element Is Enabled
Wait Until Element Is Visible dynamic_element_locator 5s Element not found after 5 seconds
Click Element dynamic_element_locator
6. Keyword Combinations:
${product_name} Get Text product_name_element
${locator} Set Variable xpath=//div[contains(@class, 'product')]//a[contains(text(), '${product_name}')]
Wait Until Element Is Visible ${locator}
Click Element ${locator}
7. Custom Locators (Advanced):
8. Test Data Setup:
Robot Framework, often in conjunction with libraries like RequestsLibrary
for API testing, offers good support for working with JSON and XML data. Here's how you can handle these data formats:
Working with JSON:
Parsing JSON:
Using Evaluate
(for simple cases): For very basic JSON structures, you can use the Evaluate
keyword with Python's built-in json
library.
***Variables***
${json_string} Set Variable {"name": "John", "age": 30}
***Test Cases***
Parse JSON
${data} Evaluate json.loads('${json_string}') json
Log Name: ${data}[name]
Log Age: ${data}[age]
Using Convert To Dictionary
(from Collections
library): This is a better approach for more complex JSON and integrates well with Robot Framework's variable handling.
***Variables***
${json_string} Set Variable {"name": "John", "age": 30, "address": {"city": "New York"}}
***Test Cases***
Parse JSON
${data} Convert To Dictionary ${json_string}
Log Name: ${data}[name]
Log City: ${data}[address][city]
Creating JSON:
Using Evaluate
: You can construct JSON strings using Python's json.dumps()
method.
***Test Cases***
Create JSON
${data} Evaluate {"name": "Jane", "city": "London"} json
${json_string} Evaluate json.dumps(${data}) json
Log ${json_string}
Working with JSON in API Testing (using RequestsLibrary
):
Sending JSON data in requests:
***Settings***
Library RequestsLibrary
***Test Cases***
Send JSON Request
Create Session my_session https://api.example.com
${data} Evaluate {"key1": "value1", "key2": "value2"} json
Post Request my_session /endpoint json=${data}
Handling JSON responses: The RequestsLibrary
automatically parses JSON responses.
${response} Get Request my_session /endpoint
${name} Set Variable ${response.json()}[name]
Log Name: ${name}
Working with XML:
Parsing XML:
Using Evaluate
and xml.etree.ElementTree
(for simple XML): For basic XML, you can use Python's xml.etree.ElementTree
library.
***Variables***
${xml_string} Set Variable John30
***Test Cases***
Parse XML
${root} Evaluate xml.etree.ElementTree.fromstring('${xml_string}') xml.etree.ElementTree
${name} Evaluate ${root}.find('name').text xml.etree.ElementTree
Log Name: ${name}
Using XML
library (recommended for complex XML): The dedicated XML
library provides more powerful and convenient ways to parse and navigate XML structures. You'll need to install it: pip install robotframework-xml
***Settings***
Library XML
***Variables***
${xml_string} Set Variable John30New York
***Test Cases***
Parse XML
${root} Parse XML ${xml_string}
${name} Get Element Text ${root}/person/name
${city} Get Element Text ${root}/city
Log Name: ${name}
Log City: ${city}
#Get all person elements
@{persons} Get Elements ${root}/person
Log Many @{persons}
#Get attribute of an element
${age} Get Element Attribute ${root}/person/age value
Log Age: ${age}
Creating XML:
Using Evaluate
and xml.etree.ElementTree
: You can construct XML structures using the xml.etree.ElementTree
library.
***Test Cases***
Create XML
${root} Evaluate xml.etree.ElementTree.Element('data') xml.etree.ElementTree
${name} Evaluate xml.etree.ElementTree.SubElement(${root}, 'name') xml.etree.ElementTree
${name}.text Set Variable Jane
${xml_string} Evaluate xml.etree.ElementTree.tostring(${root}, encoding='unicode') xml.etree.ElementTree
Log ${xml_string}
Key Considerations:
Collections
library and Python's built-in json
library are often sufficient. For XML, the XML
library or Python's xml.etree.ElementTree
are commonly used. For API testing, RequestsLibrary
handles JSON well.XML
library's methods.TRY
/EXCEPT
blocks) when parsing or processing JSON and XML data, as these operations can sometimes raise exceptions.Robot Framework, combined with the RequestsLibrary
, is a powerful combination for testing REST APIs. Here's a comprehensive guide on how to do it:
1. Installation:
First, you need to install the RequestsLibrary
.
pip install robotframework-requests
2. Basic API Test :
***Settings***
Library RequestsLibrary
***Variables***
${API_URL} https://api.example.com # Replace with your API URL
***Test Cases***
Get User Data
Create Session my_session ${API_URL}
${response} Get Request my_session /users/1 # Replace with your API endpoint
Should Be Equal As Strings ${response.status_code} 200
${user_data} Set Variable ${response.json()} # Parse JSON response
Log User data: ${user_data}
Should Be Equal ${user_data}[name] John Doe # Example assertion
Explanation:
Create Session
: Establishes a session with your API. This is important for managing cookies and headers across multiple requests.Get Request
: Sends a GET request to the specified endpoint. You can use other methods like Post Request
, Put Request
, Delete Request
, etc., for other HTTP methods.Should Be Equal As Strings
: Verifies the status code of the response. It's good practice to check the status code to ensure the request was successful.${response.json()}
: Parses the JSON response body. The RequestsLibrary
automatically handles JSON parsing.Should Be Equal
: Asserts that a specific field in the JSON response has the expected value.
3. Handling Different HTTP Methods:
* POST :
${data} Create Dictionary name=Jane Doe age=30
${response} Post Request my_session /users json=${data}
* PUT :
${data} Create Dictionary age=31
${response} Put Request my_session /users/1 json=${data}
* DELETE :
${response} Delete Request my_session /users/1
4. Setting Headers :
${headers} Create Dictionary Content-Type=application/json Authorization=Bearer your_token
${response} Get Request my_session /users headers=${headers}
5. Query Parameters :
${params} Create Dictionary limit=10 offset=0
${response} Get Request my_session /users params=${params}
6. Handling XML Responses:
If your API returns XML, you can use the XML
library to parse it, as discussed in the previous response.
7. Assertions:
Use a variety of assertions to validate the API response:
Should Be Equal
: For exact value matching.Should Contain
: To check if a string contains a substring.Should Match
: For pattern matching (regular expressions).Length Should Be
: To check the length of lists or strings.Dictionary Should Contain Key
: To verify that a dictionary contains a specific key.
8. Data-Driven Testing:
You can use Robot Framework's data-driven features to run the same API test with different sets of data.
***Test Cases***
Get User Data
[For] ${user_id} IN 1 2 3
${response} Get Request my_session /users/${user_id}
Should Be Equal As Strings ${response.status_code} 200
9. API Key Management:
Store your API keys in variables or a separate file to avoid hardcoding them in your tests. Use environment variables or Robot Framework's variable files for better security.
10. Best Practices:
Data-driven testing in Robot Framework allows you to execute the same test logic with multiple sets of input data. This is extremely useful for testing various scenarios, boundary conditions, and data combinations without having to write repetitive test cases. Here are the primary ways to achieve data-driven testing in Robot Framework:
1. Using FOR
Loops:
Simplest Approach: For basic data-driven testing, you can use FOR
loops to iterate over a list of values or dictionaries.
***Test Cases***
Data Driven Test with List
@{users} Create List user1 user2 user3
FOR ${user} IN @{users}
Log Testing with user: ${user}
# Perform test steps using ${user}
Data Driven Test with Dictionary
&{user_data} Create Dictionary name=John age=30 city=New York
FOR ${key} ${value} IN &{user_data}
Log ${key}: ${value}
# Perform test steps using ${key} and ${value}
2. Using Test Template
:
More Structured: The Test Template
setting allows you to define a keyword that acts as a template for your test cases. You then provide the data in the ***Test Cases***
section, and Robot Framework automatically creates test cases for each data row.
***Settings***
Test Template Login with User
***Test Cases***
Valid User 1 user1 password123
Valid User 2 user2 another_password
Invalid User invalid_user wrong_password
***Keywords***
Login with User
[Arguments] ${username} ${password}
Input Text username_field ${username}
Input Password password_field ${password}
Click Button login_button
# Assertions based on login result
In this example, the Login with User
keyword is used as a template. Robot Framework will generate three test cases: "Valid User 1," "Valid User 2," and "Invalid User," each with the corresponding data.
3. Using External Data Files:
Most Powerful: For larger datasets, it's best to store the data in external files (CSV, Excel, etc.) and then read it into your tests.
CSV:
***Settings***
Library Collections
***Test Cases***
Data Driven Test from CSV
@{data} Get Lines From File test_data.csv
FOR ${row} IN @{data}
@{values} Split String ${row} ,
${username} Set Variable ${values}[0]
${password} Set Variable ${values}[1]
Log User: ${username}, Password: ${password}
# Perform test steps
Excel: You can use the ExcelLibrary
(install it: pip install robotframework-excellibrary
) to read data from Excel files.
***Settings***
Library ExcelLibrary
***Test Cases***
Data Driven Test from Excel
Open Excel test_data.xlsx
${sheet} Read Excel Sheet Sheet1
FOR ${row} IN @{sheet}
${username} Set Variable ${row}[0]
${password} Set Variable ${row}[1]
Log User: ${username}, Password: ${password}
# Perform test steps
Close Excel
4. Using the DataDriver
Library:
Advanced and Feature-Rich: The DataDriver
library (install it: pip install robotframework-datadriver
) provides a more comprehensive solution for data-driven testing. It allows you to easily read data from various sources (CSV, Excel, JSON, YAML) and create test cases dynamically.
***Settings***
Library DataDriver file=test_data.csv
***Test Template***
Login with User
***Test Cases*** # These will be generated based on the CSV data
***Keywords***
Login with User
[Arguments] ${username} ${password}
# Test steps
Key Considerations:
Test Template
or DataDriver
, Robot Framework will automatically generate test case names. You can customize these names using placeholders from your data.Debugging in Robot Framework is essential to identify issues in test cases. Here are several methods to debug and troubleshoot your scripts effectively:
Set the log level to DEBUG to get detailed logs in the output.
robot --loglevel DEBUG my_test.robot
Robot Framework provides logging functions to print information during test execution.
*** Test Cases ***
Debugging Example
Log This is a normal log message
Log To Console This message appears in the terminal
Log Variable value: ${my_variable} level=DEBUG
Log
→ Adds messages to the log file.Log To Console
→ Prints messages directly in the console.Run Keyword And Continue On Failure
This allows the script to continue even if a step fails, helping you gather more information.
Run Keyword And Continue On Failure Click Element //button[@id='submit']
For UI tests, capture a screenshot on failure.
SeleniumLibrary does this automatically, but you can also use:
Capture Page Screenshot
Pause Execution
(Interactive Debugging)You can pause execution and manually inspect variables.
Pause Execution
Log Variables
)To check variable values at runtime:
Log Variables
Dry-run mode helps check for syntax errors without executing the test.
robot --dryrun my_test.robot
If you are using Python custom libraries, insert Python’s pdb debugger in your code.
import pdb
pdb.set_trace() # This pauses execution for debugging
Breakpoints
for Step-by-Step ExecutionYou can simulate breakpoints using:
Input Text username_field my_username
Sleep 5s # Simulating a wait to manually inspect
Sleep
allows you to check the browser state before proceeding.Debugging Method | Description |
---|---|
--loglevel DEBUG |
Enables detailed logs |
Log & Log To Console |
Prints messages for debugging |
Pause Execution |
Pauses test execution for manual inspection |
Capture Page Screenshot |
Captures screenshots for UI debugging |
Log Variables |
Displays all variables at a point |
--dryrun |
Checks for syntax errors before running tests |
pdb.set_trace() |
Debugs Python libraries step by step |
To ensure cross-browser testing in Robot Framework, you can use SeleniumLibrary and parameterize browser selection dynamically. This allows you to run tests on multiple browsers like Chrome, Firefox, Edge, and Safari.
Before starting, ensure you have the following installed:
pip install robotframework
pip install robotframework-seleniumlibrary
* WebDriver Managers (for automatic driver handling)
pip install webdriver-manager
You can use variables to specify which browser to run tests on.
*** Settings ***
Library SeleniumLibrary
*** Variables ***
${BROWSER} chrome # Default browser
*** Test Cases ***
Test in Multiple Browsers
[Documentation] Run test in different browsers
Open Browser https://www.google.com ${BROWSER}
Sleep 2s
Close Browser
robot -v BROWSER:firefox test.robot
You can define multiple browsers and run the same test for each one.
*** Settings ***
Library SeleniumLibrary
Suite Setup Run Keywords Open Browser https://www.google.com chrome
... AND Open Browser https://www.google.com firefox
Suite Teardown Close All Browsers
*** Test Cases ***
Search Google in Chrome and Firefox
Input Text name=q Robot Framework
Press Keys name=q ENTER
Sleep 2s
Capture Page Screenshot
For faster execution, run tests in parallel using Pabot (Parallel Robot Framework).
pip install robotframework-pabot
pabot --variable BROWSER:chrome --variable BROWSER:firefox test.robot
For scalability, you can run tests on:
*** Variables ***
${REMOTE_URL} http://localhost:4444/wd/hub
${BROWSER} chrome
*** Test Cases ***
Test on Selenium Grid
Open Browser https://www.google.com remote_url=${REMOTE_URL} desired_capabilities=${BROWSER}
Sleep 2s
Close Browser
localhost:4444
.* Use WebDriver Manager to handle driver updates automatically.
* Use Variables (-v BROWSER:firefox
) to switch browsers dynamically.
* Use Pabot for Parallel Testing to reduce execution time.
* Leverage Cloud Platforms for broader browser/OS combinations.
* Use Screenshot Captures to debug UI inconsistencies across browsers.