Variables are an essential component of Robot Framework and can be utilized in almost any place in test data. Simply said, if we have values that change frequently and are utilized several times in different test scenarios, adding a variable can help. So, if our value changes in the future, we can just update it in one place, and it will be reflected in all the test cases. Test data and locators are the finest use cases for variables.
In Robot Framework, there are four sorts of variables.
1. Scalar (Identifier: $) – The most common way to use variables in Robot Framework test data is using the scalar variable syntax like ${var}. When this syntax is used, the variable name is replaced with its value as-is.
2. List (Identifier: @) – If a variable value is a list or list-like, a list variable like @{EXAMPLE} is used. In this case, the list is expanded, and individual items are passed in as separate arguments.
3. Dictionary (Identifier: &) – A variable containing a Python dictionary or a dictionary-like object can be used as a dictionary variable like &{EXAMPLE}. In practice, this means that the dictionary is expanded and individual items are passed as named arguments to the keyword.
4. Environment (Identifier: %) – Robot Framework allows using environment variables in the test data using the syntax %{ENV_VAR_NAME}. They are limited to string values.
*** Variables ***
${STRING} cute name #Scalar
${INT_AS_STRING} 1 #Scalar
${INT_AS_INT} ${1} #Scalar
${FLOAT} ${3.14} #Scalar
@{LIST} one two three
&{DICTIONARY} string=name int=${1} list=@{LIST}
${ENVIRONMENT} %{PATH}
Let’s write a simple test using all the above identifiers:
*** Settings ***
Documentation To validate the Login Form
Library SeleniumLibrary
Library Collections
Test Teardown Close Browser Session
*** Variables ***
${valid_username} Admin #Scalar
${valid_password} admin123 #Scalar
${url} https://opensource-demo.orangehrmlive.com/web/index.php/auth/login #Scalar
&{VisibleElements} Forgot your password?=css:.orangehrm-login-forgot-header #Dictionary
*** Test Cases ***
Validate Elements on Login Page
Open the Browser with URL
Verify Element on Login Page
Validate Successful Login
Open the Browser with URL
Fill the login form
Verify Dashboard page opens
Verify items in Dashboard page
Verify Environment variable
*** Keywords ***
Open the Browser with URL
Create Webdriver Chrome executable_path=/Vibha_Personal/RobotFramework_Demo/drivers/chromedriver_linux64
Go To ${url}
Maximize Browser Window
Set Selenium Implicit Wait 5
Verify Element on Login Page
Element Should Be Visible ${VisibleElements}[Forgot your password?]
Fill the login form
Input Text css:input[name=username] ${valid_username}
Input Password css:input[name=password] ${valid_password}
Click Button css:.orangehrm-login-button
Verify Dashboard page opens
Element Text Should Be css:.oxd-topbar-header-breadcrumb-module Dashboard
Verify items in Dashboard page
@{expectedList} = Create List Admin PIM Leave Time Recruitment My Info Performance Dashboard Directory Maintenance Buzz #List
${elements} = Get Webelements css:.oxd-main-menu-item--name
@{actualList} = Create List
FOR ${element} IN @{elements}
LOG ${element.text}
Append To List ${actualList} ${element.text}
END
Lists Should Be Equal ${expectedList} ${actualList}
Close Browser Session
Close Browser
1. ${valid_username} Admin and ${valid_password} admin123 are both scalar variables because we have the $ sign. The variables are storing the username and password values.
2. @{expectedList} is a list variable (identified by the sign @) storing 11 elements in a list (or Array).
3. &{VisibleElements} Forgot your password?=css:.orangehrm-login-forgot-header is a Dictionary variable (identified by &) storing two locators in the form of key=value.
Execute the tests
We need the below command to run the Robot Framework script.
robot Variable_Demo.robot
The output of the above program is
View Report and Log
We have the test case passed. The Robot Framework generates log.html, output.xml, and report.html by default.
Let us now see the report and log details.
Report
Right-click on report.html. Select Open In->Browser->Firefox(any browser of your wish).
Log
Robot Framework has multiple log levels that control what is shown in the automatically generated log file. The default Robot Framework log level is INFO.
Right-click on log.html. Select Open In->Browser->Firefox(any browser of your wish).
The list elements are shown below
That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!