Transcripted Summary



Chapter 7, Variables.

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 2 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 the 010-Variables directory and open up the variablesSectionExample.robot file.


*** Settings ***

*** Test Cases ***
Using Variable  
  # Log to Console 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 to Console 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 again with *** Variables ***.

Inside, 3 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 — ${nameofvariable}.
  • When assigning variables in a variable section, you must have at least 2 spaces in between the variable name and the value you want to assign that variable — ${nameofvariable} valueofvariable

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

When this 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 2 things.

  • It logs 2 statements to the console. The 2 statements it logs are exactly the same.
  • The difference between these 2 lines on 7 and 10 are that we're using a variable in the second log message and not in the first.

In the first message we're using a literal String to Log to 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 same thing, but the variable is what we use instead of the literal String. We use the variable “name”, the variable “color”, and the variable “team” in order to output “Paul”, “teal”, and “Jaguars”.

Let's run this.



Notice we get 2 log messages on the screen, they're each exactly the same.

# Inline Variables

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

In the same directory, you have a file called inlineExample.robot, it should look familiar.


*** Settings ***

*** Test Cases ***
Using Variables

  # 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.

  # Now we're declaring 3 Variable 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}.

  # 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  Orange
  ${team}=  Set Variable  Bengals

  # 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}.

  # 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}.

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 in the same way we did before, except we're going to do it inline.

You may notice that we're not using a Variables section or at least you don't see one at this point.

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

Inside the “Using Variables” test case on lines 11 through 13, you'll see 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 — ${name}= Set Variable Paul

In this case, the variable name is “name”. We have at least 2 spaces following the equal sign. We use the Set Variable keyword (which you can look up easily in the BuiltIn library). And we have at least 2 spaces following the keyword Set Variable and prior to the value we want to set, “Paul”.

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

Then we Log to Console and it should look exactly the same as the String 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.

We'll use a person named “Lisa”. We'll say her favorite color is “orange”, and we'll call her favorite team the “Bengals”.

What do you expect next on line 24 of the output to the console?

That's right. It should be: “My name is Lisa. My favorite color is orange. My favorite NFL team is the Bengals”.

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” and setting the team to “Dolphins”.

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

Let's run this test and see if it works.

Excellent.



We see the output here we expected.

You have 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 work differently than you might expect.

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 here if you need it.



Resources



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