Robot Framework

How to create Variables in Robot Framework 

Variables in Robot Framework

Creating a variable helps when we have values that are constantly changing and are used multiple times in different test cases. Variables are elements that are used to store values that can be referred to by other elements.  

Types of variables available in Robot Framework are as follows: 

  • Scalar 
  • List 
  • Dictionary 
  • Environment 
  • Scalar – In this type, the variable name is replaced with its value as-is.  Syntax: ${variable name} value 
  • List – This type contains a number of values, and can be referred by mentioning the variable name and the index number of the value that we want to pass.  Syntax: @{variable name} value1  value2 …… 
  • Dictionary – This type contains the number of key and value pair and can be referred by specifying the variable name with name of key that we want to use.  Syntax: &{variable name}  K1=value1 K2=value2…… 
  • Environment – Normally this type is a system variable and is limited to string values.  Syntax: %{ENV_VAR_NAME} 

In this blog, we will discuss- 

  • Project setup for variables 
  • How to create scalar variables 
  • How to create a list variable 
  • How to create dictionary variables 
  • How to create environment variables 

Project setup for Variables-  

Firstly, we must install all prerequisite libraries in our project (for more info about prerequisites you can visit- https://www.neovasolutions.com/2022/07/21/robot-framework-automation-using-selenium-and-python/

To ensure that the required libraries are installed, use pip list command, or manually navigate to:

File > settings > project: robot automation> python interpreter 

Project Setup

After verifying the prerequisites, create a file named ‘Variables’  

— Create the file under ‘Testcases’ directory with .robot extension >Enter 

File Name

Given below is an example for a better understanding on how we can create variables in our test. 

Testcase: 

Step1: Visit https://www.saucedemo.com/

Step2: Enter Username 

Step3: Enter Password 

Step4: Click Login button 

Step5: Close the browser 

Code: 

*** Settings ***
Library  SeleniumLibrary
*** Variables ***
${browser}  chrome
${url}  https://www.saucedemo.com/
@{Credentials}    locked_out_user    secret_sauce
&{Data}    username=locked_out_user    password=secret_sauce
*** Test Cases ***
Verify the login functionality
    Start Test
    input text      xpath://input[@id="user-name"]    @{Credentials}[0]
    input text      xpath://input[@id="password"]    &{Data}[password]
    click element      xpath://input[@id="login-button"]
    End Test
    log    This Test is ran by %{USERNAME} on %{os}
*** Keywords ***
Start Test
    open browser    ${url}  ${browser}
    maximize browser window
End Test
    close browser

***Settings*** – To work with robot framework keywords we need to import   Library SeleniumLibrary in the settings section because all the keywords are built in selenium library. Similarly, we can import other libraries, resource files, and variable files as per our project requirement in this section. 

Variables code

Code explanation:

In the above script, ${browser} chrome and ${url} https://www.saucedemo.com/  are both scalar variables identified by ‘$’ sign which are used in the start test user-defined keyword of the keywords section to open the browser with the specified URL by using the keyword in the test cases section. 

The @{Credentials} locked_out_user secret_sauce is a list variable identified by ‘@’ sign that stores 2 values, and we can use any of the values by mentioning the index number. Here in the test cases section ${Credentials}[0] will use the first value of the list – locked_out_user as an input for the username field. 

&{Data} username=locked_out_user password=secret_sauce is a dictionary variable, identified by the ‘&’ sign that stores values for username and password in the form of key=value. Here in the test cases section ${Data}[password] specifies the key of the value that is required, so secret_sauce is the value that will be used as an input for the password field. 

%{USERNAME} is an environment variable that provides the username of the system, here in this test case we used a command named log in order to print the name of the user through whom the execution has been done.  

Lastly, we ended the session by creating a keyword in the keywords section named End Test that will close the browser by using it in the test case section. 

Execution: 

To execute the test case we need to use the command below: 

Execution Command

After execution, we should get the result as a pass. 

Pass result

Reports: 

After every execution, robot framework creates detailed HTML reports. 

We get three files – log.html, output.xml, and report.html. 

Log.html: 

Variables Log

Report.html: 

Variables Report

Read our next blog on “How to handle Waits (Implicit and Explicit)”.