Transcripted Summary

Variables are a way of storing information. They have a name and they have a value.

In this section, we're going to talk about how Robot Framework uses variables and how you can use variables in your Robot Framework tests.

There are two types of variables in Robot Framework. The first is called Section Variables.


# Section Variables

Section variables are defined within a variables section in a test.

Take a look in exercise-09 which has a file called variablesSectionExample.robot.


*** Settings ***

*** Test Cases ***
Using Variable  
  # Log a message, with a literal string. No Variable are used here.
  Log to Console   My name is Paul. My favorite color is teal. My favorite NFL team is the Jaguars.

  # Log the same message, but this time we use the variables we set up in the variables section.
  Log to Console   My name is ${name}. My favorite color is ${color}. My favorite NFL team is the ${team}.

*** Variables ***
${name}  Paul
${color}  teal
${team}   Jaguars

Inside this file, we have Settings, which contains nothing; one test case in the Test Cases section.

Then we have a new block that we haven't seen before — *** Variables *** — Variables is denoted with asterisk, asterisk, asterisk, space, “Variables”, space, asterisk, asterisk, asterisk.

Inside three variables are defined. The first is called “name”. The second is “color”. The third is “team”.

The value of the name variable is “Paul”. The value of the color variable is set to “teal” and the value of the team variable is set to “Jaguars”.

  • A variable is denoted by having a dollar sign, open curly brace, the name you want to give the variable, and a closed curly brace.

  • When assigning variables in a variable section, you must have at least two spaces in between the variable name and the value you want to assign that variable.


As we mentioned before, when a test suite file is run in Robot, the Variables section is the first section to be run.

When that section is run, the values in that section are assigned to their variables and they're made usable to the Settings section, the Test Cases section and the Keywords section.

In this test case, we're going to find out how to use variables. The test case we're looking at is called “Using Variable”.

It does only two things.

  • It logs two statements to the console. The two statements it logs are exactly the same.

  • The difference is that we're using a variable in the second log message and not in the first.

In the first message, we use an actual String to log to the console — “My name is Paul. My favorite color teal. My favorite NFL team is the Jaguars”.

In the second Log to Console, we're going to log out the very same thing, but the variable we use in this first case name will be replaced with the value we assigned in the variable section, “Paul”. The variable color will be replaced with the value, “teal”; and the variable team will be replaced with the value, “Jaguars”.

Let's run this.



Notice we get two log messages on the screen. They are each exactly the same.


# Inline Variables

Now let's look at a different way of using variables. Let's use them Inline.

In exercise-09, you have a file called inlineExample.robot. This should look fairly familiar.


*** Settings ***

*** Test Cases ***
Using Variable

  # Log a message, with a literal string. No Variable are used here.
  Log to Console   My name is Paul. My favorite color is teal. My favorite NFL team is the Jaguars.  console=true

  # Now we're declaring three Variables called name, color, and team. We also set them to values.
  ${name}=  Set Variable  Paul
  ${color}=  Set Variable  teal
  ${team}=  Set Variable  Jaguars

  # Log the same message, but this time we use the Variable we set up in the lines above.
  Log to Console   My name is ${name}. My favorite color is ${color}. My favorite NFL team is the ${team}.  console=true

  # Now I'd like to log the same message, but for a different person named Lisa. She has different preferences than Paul
  ${name}=  Set Variable  Lisa
  ${color}=  Set Variable  Blue
  ${team}=  Set Variable  Bills

  # Log the same message, but this time we use the Variable we set up in the lines above.
  Log to Console   My name is ${name}. My favorite color is ${color}. My favorite NFL team is the ${team}.  console=true

  # Now I'd like to log the same message, but for another person named Fred. He has different preferences than Paul and Lisa
  ${name}=  Set Variable  Fred
  ${color}=  Set Variable  Green
  ${team}=  Set Variable  Dolphins

  # Log the same message, but this time we use the Variable we set up in the lines above.
  Log to Console   My name is ${name}. My favorite color is ${color}. My favorite NFL team is the ${team}.  console=true

On line 8, we do a Log to Console which looks exactly the same as the test case we used earlier. My name is Paul. My favorite color is teal. My favorite NFL team is the Jaguars.

Now we're going to declare 3 variables with the same value as we did before, but we're going to do it in a different way. You may notice that we're not using a variable section, or at least you don't see one at this point.


Instead, we're going to set the values of these variables inline, meaning in the test case.

So, inside the “Using Variable” test case on lines 11 through 13, we'll set the variable name using the Set Variable keyword and a special syntax. We have an equal sign immediately following the name of the variable we want to create and use.

In this case the variable name is “name”. We have at least 2 spaces following that equal sign.

We use the Set Variable keyword (which you can look up very easily in the BuiltIn library) and we have at least two spaces following the keyword Set Variable and prior to the value we want to set, “Paul”.

We do the same thing with the “color”, setting it to “teal”; and the “team” setting it to “Jaguars”.

Then we log to the console and it should be exactly the same String as what we saw before — “My name is Paul. My favorite color is teal. My favorite NFL team is the Jaguars”.


Now let's log the same message but for different values.

Let's use a person whose name is “Lisa”, let's use a color called “blue” and a variable for a team called “Bills”. What do you expect the next line on line 24 to output to the console?

That's right. It should be, “My name is Lisa. My favorite color is blue. My favorite NFL team is the Bills”.

Finally, let's change the values one more time inline within the test by setting the name variable using the equal sign and the set variable method to “Fred”, setting the variable color to “green”, setting the team to “Dolphins”.

Then we log to the screen: “My name is Fred. My favorite color is green. My favorite NFL team is the Dolphins”.

Let's run this test and see it work.



You've got a number of other test cases in this exercise that you can look at to see how variable sections and inline variables work together and differently.

Take a look at those and then work on the challenge in the README.



The challenge is to use variables in place of literals in the “variables.robot” test suite. Use a variable block or section if you like.

Remember the word “literal” just means an actual String.

So, in that first example that we looked at, we used the word “Paul” instead of the variable “name”. The word “Paul” is a literal, so look for literals in the “variables.robot” test suite and use variables in place of those literals.

The answer is there if you need it.



Resources



© 2021 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR