Robot Framework Variables

Variables are elements 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 is a variable that 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.

Click on the First Test Case , and you will see the should be equal keyword in the tabular column. Right-click on the test suite and then select the new scalar option

creating-new-a-scalar

Once you select the new scalar, the below page will appear on the UI, enter the variable name inside the curly braces ${var}. I gave the value as, because we are using the should be equal keyword and 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 one scalar variable

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 button 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, to get the value, the list item is passed as the argument to the list variable. The individual list items are passed in arguments separately

The syntax for the list variable is

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

Let us see how to create a List Variable

Go the Ride editor, and here I have already created another test case called the second test case, so right-click on it, once it open, in the tabular column, press ctrl+alt+space and select the should not be equal as strings And then right-click on the test suite 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

Now, go back to the second test case and pass the arguments with an index value, as shown below.

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

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

list-variable-output

Demo Log :

demo-log-of-list-variable

Test execution log is as shown below

test-execution-log-of-list-variable

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}
&{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 a 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 page will open, 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 button and then select the Third Test Case and then click on the start button.

output-of-dictionary-variable

Once you open the log report, then you will see the below report

demo-log-of-dictionary-variables

The Test Execution log

test-execution-log-for-dictionary

Data-Driven Test case

Supported File Formats for Robot Framework

The Robot framework test data can be defined in two different formats

Plain Text Format in Robot Framework

The plain text format is the base for all supported robot framework data formats. It can be edit by using the normal text editor and IDE's. Test Data can be parsed line by line. In single-line arguments and keywords are separated by a separator.

The most commonly used separator is space separator

Space separator : Two or more spaces acts as a separator between the two different data items. The minimum spaces should be two.

Let us see how to write the test cases in plain text format. Open the Ride editor and then click on any test case, here I am opening the First Test case.

You can see the three options Edit, Text Edit, and Run, click on the Text Edit to write the test case in the plain text format.

text-editor-for-pzlin-text

Create another test case in the plain text format as the Fourth test case and selected the keyword as Should Be Equal and pass the arguments as 4 and 4 by giving the two spaces between them.

created-fourth-test-case0in-the-plain-text

We entered the test case in the text editor, and the same will be reflected in the Editor as well.

plain-text-reflecting-in-the-editor

Keyword Rewind Functionality

Tab Separated Format(TSV)

The files in the TSV format are typically edited in the spreadsheet programs because the syntax is so simple, and they are easy to generate programmatically. They are pretty easy to edit by using the normal editor, and also they work well in version control.

In the case of Plain text format, we will use the spaces to separate the arguments, but in the case of TSV, we use tabs to separate the arguments.

We can create and edit the TSV files in any spreadsheet program, such as Microsoft Excel. The TSV file containing non-ASCII characters must be saved using UTF-8 encoding.

The files in the TSV format are commonly saved by using the .tsv extension. But starting from the framework 3.1, the --extension is used explicitly to tell that these files should be parsed. We can also save the TSV files with .robot extension, but this requires the files to be fully compatible with the plain text syntax.

The supported text formats are:

reStructured Text format: it is easy to read plain text markup syntax that is commonly used for documentation of python projects.

The reStructured documents are most often compiled to HTML, but still, other output formats are supported.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions