Robot Framework Variables

Variables are used to store values, that can be referred to by other elements. Variables can also be used in test cases and user-defined keywords.

Example: {Var1}=Bag

There are three types of variables in the robot framework

Web Elements of Robot Framework

Scalar Variable in Robot Framework

A Scalar variable holds a single value at a time, and it is a single component that assumes the range of numbers.

The Scalar variable is used to store the Strings, Objects, List, etc..

The syntax for the scalar variable is

${variablename}

Let us know how to create a scalar variable and make use of it.

In the Features Of Ride article, we have created a First Test Case, let us create a scalar variable by using the below command open the Ride Editor

ride.py

Click on the project you have created, or if you want to create a new project, refer to the article Creating a New Project. Click on the Test Suite, which you have created, and then, you can see the test cases.

Right-click on the test suite(Demo1) and then select the New Scalar

creating-new-a-scalar

Once you select the new scalar, the below popup will appear on the UI, enter the variable name inside the curly braces ${var}. I gave the value as 4 and we can provide any comment describing why we have this variable, I am passing the comment as This is my first scalar variable and then click ok.

craeting-new-scalar-variable-var1

If the scalar variable has created successfully, it will be displayed below the test suite.

scalar-var-displayed-below-test-suite

I am creating another scalar variable ${var2}

second-scalar-variable

Now go to the First Test Case and instead of values 4 and 4, I am going to give as ${var1} and ${var2}

passing-the-var-name-instead-of-values

Click on the Run tab on the top of the editor and then select the First Test Case and then click on the Start button.

click-on-run-select-first-test-case-click-on-start

The output is

The-output-of-the-first-test-case

Click on the log report, and the log report is as shown below

test-statistics-of-fisrt-test-case

Test Execution log is as shown below

test-execution-log-of-first-test-case

Suite Demo and test case is as shown below

suite-demo1-and-test-case

Locators in Selenium

List Variables in Robot Framework

The List variable will have an array of values, The values in the list are enclosed with the square brackets and separated by a comma.

The syntax for the list variable is

@{variablename}
@{user}=["Jaanu", "Jhanvi"]

Let us see how to create a List Variable

Right-click on the test suite(Demo1) and select the new list variable.

select-new-list-variable

Fill the required parts, and here I am giving the name as a user and select the column number as one and then enter two values as jaanu and jhanvi and then click, ok.

craeting-list-variable

You can see list variable displayed below the test suite

list-variable-below-test-suite

Create a test case called the second test case and add an assertion to compare the arguments with the index value. Here we are using assertion Should Not Be Equal As Strings

passing-the-arguments-to-the-list-variable

Now, click on the Run tab, and then select the second test case and click on the start button.

list-variable-output

Basics of CSS Selector

Dictionary Variables in Robot Framework

The dictionary variables are similar to the List variables wherein we pass the index as an argument. However, in the case of the dictionary variable, we can store the details in the key-value pairs.

The syntax for the Dictionary variable is:

&{variablename}=['key1': 'value1' 'key2': 'value2',.......]
&{user}=['name' : 'Rakul', 'age' : '21']

Let us create a dictionary variable before that create a new test case to explain the dictionary variable.

third-test-case

Create the keyword as Should Be Equal As String in the Third test case

entering-the-keyword-in-third-test-case

Right-click on the Demo suite and then select the New Dictionary Variables

new-dict-variable

Once the dictionary popup opens, enter the dictionary name and then the individual items in the format of key=value format.

user-details-dictionary

Now, go back to the Third test case and enter the arguments as shown below

keywords-for-dictionary

Click on the Run tab and then select the Third Test Case and then click on the start button.

output-of-dictionary-variable

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions