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}