Introduction To Robot Framework
Introduction To Robot Framework
Prepared by:
Alaa Atitallah
Content
Chapter 1: A Brief Overview of Robot Framework …………………………………………………………………………………………………………………………. 1
#Test Cases Section
#Settings Section
#Challenge
#In Review…
#Quick Recap
#Settings - Resource
#Inline Variables
Chapter 1: A Brief Overview of Robot Framework
Test drivers are helpful in implementing Acceptance Test Driven Development (ATDD) and Behavior Driven
Development (BDD).
− The section is defined by the heading with three asterisks, a space, the word “Test Cases”, a space, and
three asterisks — *** Test Cases ***.
− Spacing and positioning are very important as they tell Robot Framework how to interpret the test file.
− The name of the test case here is “Create an Invoice”.
− Test case names are specified by being left justified or all the way to the left in a test cases section.
The next 7 lines here are the actions that make up the test case. For the most part, this section is easily
readable.
1
Chapter 1: A Brief Overview of Robot Framework
• We create dummy invoices.
• We navigate to the add invoice page.
• We get the invoice details and fill them out.
• Submit an invoice form.
• We check that the page should contain the invoice, and
• Open the invoice.
I've intentionally made this test suite a bit complicated so that you can get a sense of some of the features
and inner workings of Robot Framework. Remember, this is just an overview of Robot Framework to give you a
sense of how it works.
#Settings Section
You'll notice that there's a section at the top called “Settings” — *** Settings ***
Robot Framework test suites are defined in text files. You'll notice we're using libraries to find keywords that
we'll use throughout the test case. The libraries here include SeleniumLibrary, OperatingSystem, and
Collections.
You'll also see that we use custom resources we've defined in other text files like resources.robot, invoice-
details-page.robot, and others.
Resource files and libraries provide Robot Framework a way to share behaviors and data, keywords, and
variables between test suites.
Finally, you'll notice we've used a Suite Setup and Suite Teardown to call keywords or do actions. These run
before and after the test suite.
This test suite is in the file name called “tour-of.robot”. I'm giving you a tour of Robot Framework, so I felt this
was a good name.
This test suite and all of our tests during this course are running against an application I host called the Invoices
Application.
2
Chapter 1: A Brief Overview of Robot Framework
It's an invoice manager. You can add invoices, delete them, and modify them.
It's not a good application. It has defects and I use this application in order to show off tools like Robot
Framework and help you learn.
Now that the test is run and passed, let's look at the reports.
Many plugins exist for continuous integration systems like Jenkins, Bamboo, and others to make running,
reporting, and aggregating reports easy.
3
Chapter 1: A Brief Overview of Robot Framework
Note, the background is green when the test suite passes. And even if 1 test case fails, it turns red.
4
Chapter 1: A Brief Overview of Robot Framework
You can see if they passed or if they failed and you can see what exactly the problem was and what the
keyword did.
It's a powerful tool with an active open-source community. Testers with no automation experience can
automate with it.
Developers can use it to give access to features and logic long before a UI is available.
It's available to work with many different languages. It's easily readable by business users and works well with
BDD and ATDD.
Robot Framework can work for most types of automated testing, not just UI testing. Many IDEs have support
for Robot Framework like VS Code, IntelliJ, and Atom.
I'm looking forward to working with you to learn more about how to use robot framework.
Quiz
1. Which of the following can Robot Framework NOT do:
• Test APIs
• Work with many different programming languages
• Automatically test your applications.
• Test UIs
3. Human readable Test Drivers like Robot Framework can aid in communication between for all the following
except:
• Developers
• Stakeholders
• Testers
• Human Resources
5
Chapter 2: Project Setup
You'll need to do some setup in order that we can work together to learn Robot Framework.
I'm doing this in windows. If you're on a different OS, you'll need to work through set up for your own system
differently.
After downloading or cloning the repository, you'll notice several exercises in the directory. They're not
sequentially numbered.
In the Project Setup section, you have the steps to use at the command line to determine what you already
have installed.
6
Chapter 2: Project Setup
You'll notice in the Install Tool section, we'll be installing 5 tools — Python, pip, Robot Framework,
SeleniumLibrary and WebDriver manager.
You'll also notice some Optional Tools if you want to install an Integrated Development Environment (IDE). I'm
using VS Code.
In the README file, click Install Python, then download and install. We're looking for version 3.
I'll be using version 3.10, because that's what's available right now for me.
After downloading the install packet make sure to click _Add Python to PATH. A_fter that, the default Install
Now option should be fine.
7
Chapter 2: Project Setup
When you click on that in the README file, you see the instructions which are little more than typing pip
install robotframework at the command line.
You should see something after a bunch of output that says, "Successfully installed Robot Framework." The
version available to me right now is 4.1, but by the time this video comes out, you should see version 5.
All of these examples are set up to work with Robot Framework, version 5.
You can check that you have the right version by typing robot --version, as I did.
Throughout this course, you'll use Selenium to drive the web browser and aid in our web testing automation.
Robot Framework can allow you to use Selenium without knowing how to code.
We also need to install specific drivers for browsers so that Selenium can work with those browsers. If all that
sounds complicated, I'm sorry. Just ignore it and do these installs.
Once again, we'll use pip to install. Click on the link in the README and see that we use pip install --
upgrade robotframework-seleniumlibrary.
And then when that's done, we’ll install WebDriver manager by typing pip install webdrivermanager.
Finally, we'll install the Chrome driver. You should have Chrome installed on your system; this is the Chrome
driver that selenium will use.
When all this is done, you're installed and ready to go. Congratulations
2. The exercises for this course are designed to work with which major version of Python?
• 3.0
• 8.0
• 1.0
• 2.0
8
Chapter 3: Running tests
We'll be using the 001-DemoOfRobotFramework directory. Each exercise in this repository has its own
directory.
You'll notice within the _001-DemoOfRobotFramework _directory, there's a tour-of.robot file. This is the one
we'll be using first.
Notice I'm using DOS. We'll be using the command line quite a bit with Robot Framework. If you need a cheat
sheet for DOS or for Linux, take a look at the beaufortfairmont.com website and download one.
I use robot and then the name of the file — robot tour-of-robot.
Since this run with the SeleniumLibrary, try not to interact with the keyboard or the mouse until the test
finishes.
You may notice here that we're interacting with an application on the web that my folks have created.
9
Chapter 3: Running tests
It's an invoice manager. It manages invoices. There's really not a lot to it.
After the test runs, notice we see that it passed in the output, and we can also see several lines of output.
**I'm copying the path to the report on my local machine and pasting it into a new tab on my browser. **
The report is going to come up green or red depending on whether the test case passed or failed.
10
Chapter 3: Running tests
You can click on the links and drill down into a specific test case and look at the steps that were executed
there.
This is one of the great benefits of Robot Framework. It has very clear output in a nice browser-based form.
11
Chapter 4: Keywords
There are 2 main ways to use keywords in your script — one is by a library; the other is by creating it yourself.
In each of the exercises I'm going to have a challenge for you, and that challenge is going to be set up with a
file in the exercise.
The robot file in this case is example-settings.robot. The answer, in case you want to cheat, is in the example-
settings.answer.robot file. But I know you guys don't want to cheat.
A keyword is a piece of functionality in a test case. We talked about test cases briefly earlier.
It's denoted by having 3 asterisks, a space, the word "Settings", space, and then 3 asterisks again. It's left
justified on the 0th column.
A Robot Framework test file is really a test suite. It contains 1 or more tests.
The name of a test case is defined at the top of a test case, and it's left justified.
Spacing in Robot Framework is crucial. Make sure you understand spacing within Robot Framework.
Test cases, once again, are named and defined by being left justified.
12
Chapter 4: Keywords
• We make a comment, and the comment is, "This is my first RobotFramework test case!"
• And then, we open a browser, we point it to a URL, and we tell it to use the Chrome browser.
How did I know that? Well, in general keywords will start a line.
Keywords are always defined by having 2 or more spaces between the keyword (in this case Comment) and an
argument (in this case the String, "This is my first RobotFramework test case!").
In this case, we're going to make a comment when the test case runs saying, "This is my first RobotFramework
test case!"
The second line here uses a different keyword called Open Browser.
Open Browser is going to open the browser. The arguments are the URL, and the type of browser to use.
Where did these come from? Well, that's the challenge I want you to figure out in this test case. So, I'm not going
to give you the answer right here.
Instead, what I'd like to do is look at another example of keywords and help you understand where keywords
come from.
Take a look at the 007-BasicsOfKeywords directory and open up the file called extra-large-test.robot.
13
Chapter 4: Keywords
Input Text invoice ${invoiceId}
The steps, in this case, almost all come from libraries. In fact, most of them come from the SeleniumLibrary.
14
Chapter 4: Keywords
• Page Should Contain Element we're looking for a specific HTML element on the page.
• Click Link and Input Text are also SeleniumLibrary keywords to help us navigate an HTML
webpage.
SeleniumLibrary Keywords
I mentioned the Settings section. In it, we can use a library to pull in keywords to make them available to us to
use. A number of libraries have already been created for you for Robot Framework. You can use them with very
little work.
We've already installed the SeleniumLibrary on this machine when you did the setup steps. But I want to show
you what the SeleniumLibrary documentation looks like, and where you can go to find it.
robotframework.org/seleniumlibrary is where you can find the documentation for the SeleniumLibrary.
Luckily, there's a search space here at the top and we can look for specific keywords, like for instance, Open
Browser.
The document is in the format that nearly all Robot Framework documents are in. It looks a little bit plain, but
there's a lot of utility here. You can find out what you need to know about this library from this document.
You'll notice several things. We have a table of contents, and we have some information that is general about
this library and how to use it.
We can search for Open Browser in the search box, pull it up in the keyword section on the left, and when we
click on it, we get the Open Browser keyword documentation on the right.
15
Chapter 4: Keywords
Notice we have the arguments to use for Open Browser and we also have documentation just below there
about how to use this keyword and all the specifics of it.
This is the definition of the keyword. It's the documentation from it, and we know that the keyword comes from
the SeleniumLibrary. We now know how to use it.
We see the Generate Random String keyword comes from a different library. It comes from the String library,
which we import here on line 4.
You'll notice Set Suite Variable keyword, which comes from a built-in library for Robot Framework. You
don't see that in the settings section because BuiltIn comes along with every test. You don't have to use the
Library keyword.
16
Chapter 4: Keywords
Here's the documentation on the BuiltIn library and a few of the keywords.
You'll also
notice [OperatingSystem](https://robotframework.org/robotframework/latest/libraries/OperatingSys
tem.html) which gives you the opportunity to work with the operating system, do file manipulations, work with
directories, log files.
[Collections](https://robotframework.org/robotframework/latest/libraries/Collections.html) is a
library that we'll use from to time to manage our data. We have lists, we have dictionaries and things like that
to manage data within Robot Framework.
So, keywords are a piece of text that'll allow you to do multiple things in Robot Framework.
17
Chapter 4: Keywords
You can find it by going to the command line and going into the proper directory, but let's also go in and run
this test case.
If you remember, you run a test case by typing robot and then the name of the file — robot example-
settings.robot.
You should have the same experience I have here, which is a failing test case.
If you were focused, you already know how to make Robot Framework find the Open Browser keyword. I taught
you one way so far to use keywords. Use that way to make sure that this test case works.
For those of you who have already answered this on your own and are checking on how it works, you'll notice
the answer is, we put the "SeleniumLibrary" in the Settings section.
*** Settings ***
Library SeleniumLibrary
Let's now run the answer, and you can see it pass. And it goes to the page we expect, and we see a nice green
pass in the output. If you got that right, good work.
We want to do one more exercise now or one more challenge while we're here.
18
Chapter 4: Keywords
Let's go into a new directory. We’re going to go into 007-BasicsOfKeywords and we’re going to look again at
extra-large-test.robot.
This is a good way to look at some of the keywords that can be used to see how they're used within a long test
case.
What I'd like to do now is teach you another way to create keywords.
So, the first way we used was to use the keyword Library in the settings section to pull in already created
libraries with their keywords. Now we're going to do things a bit differently.
We'll create a keyword section to start. Notice I've already got one in this file — *** Keywords ***.
Again, it's asterisk, asterisk, asterisk, space, "Keywords", space, asterisk, asterisk, asterisk.
One thing you could do here is to create a keyword. I'm going to create one called, "My Keyword"
I'm going to tab over 4 spaces underneath and give it something to do. In this case I'm just going to log
something to the console. I'm going to use the keyword Log to Console. It's one that I know, one that I've used
before. You can look it up in the BuiltIn library if you'd like.
And then I'm going to log the text, "This is a keyword I created".
*** Keywords ***
My Keyword
Log to Console This is a keyword I created
You'll notice there are at least 2 spaces between the keyword and the argument, and I can use this keyword
that I've created wherever I want.
All I have to do to use it is to tab over or use at least 2 spaces, I've got 4 here on line 12, and then type, "My
Keyword."
19
Chapter 4: Keywords
I'm going to comment out the rest of the test case just for us to be able to run this 1 keyword with no other
things going on here to see very quickly what it does.
It should output to the screen, "This is a keyword I created". So, I should see this in my output when I run this
test.
Let's see what happens. I run the keyword by running robot extra-large-test.robot.
I can see "This is a keyword I created" in the output there. That's exactly what Log to Console should do,
and our keyword worked.
It says it passed, but as a good test automation engineer, I'm always skeptical.
So, I'm going to go and look into our logs and see if I can see the output there. It should be there.
We know the keyword ran, and we can see right here where the keyword ran.
20
Chapter 4: Keywords
Down at the very bottom it says, "My Keyword" and it shows us that it wrote the words, "This is a keyword I
created".
#Challenge
Now what I'd like you to do is take a look at the README file for the 007-BasicsOfKeywords directory.
21
Chapter 4: Keywords
In the robot file I'd like you to create a keyword called, "Navigate To Home Page".
And what it is you're going to look in the keyword-basics.robot file and you're going to create a keyword in
here called, "Navigate To Home Page".
You're going to replace the steps in the test, probably the ones that I've highlighted, by calling a keyword just
like we just did. You'll need to create your own keyword section and define the "Navigate To Home Page"
keyword in it.
I created a keyword called "Navigate To Home Page". I put it in a newly created Keywords section. And inside
the keyword I called the Open Browser keyword to open the browser. And that's it.
All I did was copy from line 7 into my keyword section, and then I called the keyword or executed it on line 7,
where I've typed, "Navigate To Home Page".
Now we're calling our own keyword instead of Open Browser on line 7; and we still call Open Browser inside
the keyword we created. I'm just showing you how to create a keyword.
22
Chapter 4: Keywords
23
Chapter 4: Keywords
*** Settings ***
My Test Case
${nickName}= Set Variable Paul
Print Profile ${nickName} Merrill Trainer dpaulmerrill
${nickName}= Change Nickname ${nickName} Davie
Print Profile ${nickName} Merrill Trainer dpaulmerrill
Change Nickname
[Arguments] ${original} ${new}
[Return] ${new}
Print Profile
[Arguments] ${firstName} ${lastName} ${title} ${twitterHandle}
Log to Console \n__Profile__
Log to Console First Name: "${firstName}"
Log to Console Last Name: "${lastName}"
Log to Console Title: "${title}"
Log to Console Twitter Handle: "${twitterHandle}"
You'll notice we have 2 test cases.
One is called, "My Addition Test," the other is called, "My Test Case."
In the "My Test Case" you'll see that we use a keyword called, Print Profile which is defined down in the
Keywords section.
All it does is print out a profile of a particular person. We pass in arguments, which are the first name, the last
name, and a title, and a Twitter handle.
And it should log to the console, which is our command prompt, with my log keyword (Log to Console).
24
Chapter 4: Keywords
We pass in 2 and 3 and we check to make sure that the answer is 5, or the sum. Add is created here inside
Keywords as well.
We use the BuiltIn keyword called, Evaluate to add the first value to the second value.
Now let's run these test cases and see how they work.
Remember, I have to change into the proper directory, 032, and I'll run the test using our robot command robot
example-keywords.robot.
One of my keywords printed out profiles. It was used within the "My Test Case" test case.
The first time we ran "My Test Case", the name was "Paul Merrill". My title was "Trainer". And my Twitter handle
was "@DPaulMerrill".
The second time we called within the first test case, or rather the second time the Print Profile keyword
was called within "My Test Case", we printed this out, "Davie", "Merrill", "Trainer", "@DPaulMerrill," and "My Test
Case".
There was no output from it, but we can see that both "My Test Case", as well as "My Addition Test" passed.
Looking back at those, remember "My Test Case" is defined here. The entire test case is the section from line
8 down to line 12.
25
Chapter 4: Keywords
#In Review…
There are 2 main ways to create and use keywords.
• Keywords can come from a library, which may be supplied to you, in this case, we used SeleniumLibrary
and OperatingSystem library.
• Or you can create them on your own inside the test case using the Keywords section.
− A keyword is defined by having 2 or more spaces around it.
− A keyword is a piece of functionality that we can create or use to do something special.
− Many keyword libraries are already developed for Robot Framework, so make sure to use them as much
as possible in lieu of creating your own.
4. What is a keyword?
• The opposite of a lockword
• A space in memory that stores values
• Like a password
• A piece of functionality that can be used in a test case
26
Chapter 5: Test Cases
We've mentioned that there is a Test Cases section in a test suite file. We mentioned that each section within
a test case file or a test suite file is denoted by 3 asterisks, a space, the name of the section, a space, and 3
asterisks.
It's also left justified meaning it's all the way to the left. There are no spaces in between these.
Robot Framework only knows a section includes Test Cases if the section heading is set up exactly like I show
here.
The same is true for the Settings section and the other sections. One of those was the Keywords section we
talked about in the last chapter.
I wanted to use this simple example to talk about test cases and exactly what's going on. You'll notice in other
test files we have other sections involved.
The flow for how Robot Framework works is easily shown here.
We don't have a Variables section here, but if we did, it might look something like this.
27
Chapter 5: Test Cases
The Variables section is the first thing that happens within the flow of a Robot Framework script.
The variables are loaded. They're created or kept in mind to be used by the program. But the variables have
to be defined so that the Settings section can use them as well.
The Keywords section doesn't actually get run until a keyword defined in the Keywords section is used within
that test case.
Test cases are run from top to bottom, meaning the first test case at the top of the Test Cases section runs
first.
The steps in the test case are run from top to bottom.
This example is about learning to write a test case and adding steps to it.
28
Chapter 5: Test Cases
The challenge that we're going to have here is to verify that the text “Invoice Manager” is on the first page of
the application.
We've shown this application before, but this is what it looks like.
All this test case is going to do when you're done with it is to verify that “Invoice Manager” is on this page.
Let's go just make sure in a manual way that it's actually there. And yep, we can see that it's here. That's the
text we're looking for on this page.
All this test will do is open up the browser, go to the URL and look for “Invoice Manager” on that page.
You'll notice in my test case that line 7 is green and it's green because this line is a comment.
It starts with the hash or pound sign (#). The pound sign is used to say, “don't execute anything after this line
or anything on this line”.
Comments are used in programming to make sure that we can add contextual information to our code.
You can also stub out pseudo code for what's supposed to happen, which is what I'm doing here. I'm saying,
"Hey, verify that the text ‘Invoice Manager’ is on the front page." How you do that is up to you.
So once again, you know that you can cheat and go look at the first-test-case.answer.robot file, but I'll
encourage you not to.
Now, pause the video and go verify that the text “Invoice Manager” is on the front page.
We've got an Open Browser call and then the only thing that I really added of value was Page Should Contain
and then “Invoice Manager”.
*** Settings ***
Library SeleniumLibrary
29
Chapter 5: Test Cases
Check invoice manager page
Open Browser http://inv.beaufortfairmont.com/ chrome
Comment Checking that we're on the invoice manager page
Page Should Contain Invoice Manager
Page Should Contain is a keyword that we found in the SeleniumLibrary.
If you want to know how to find specific keywords, I'm not really sure how to tell you.
It's kind of like when they taught you how to look things up to the library, to look in the card catalog and
brainstorm with keyword creation. You've got to kind of brainstorm a little bit here to figure out what it is you
should be looking for.
30
Chapter 5: Test Cases
#Quick Recap
So now you've been able to create a test case and run it. You should understand how a test case runs, that it
runs from top to bottom,
You should understand which sections can be in a Test Case file — Settings, Test Cases, Keywords and Variables.
And you know the order of these as well, in terms of how they execute and when they execute.
31
Chapter 5: Test Cases
5. The first thing to happen when a test suite file is run is:
• The keywords section is executed
• The variables section is loaded
• Test cases are defined
• The settings section is executed
32
Chapter 6: Settings
I'm going to go into my 001-DemoOfRobotFramework directory, and look at the tour-of.robot file, because it's
got a good example of a nice, big Settings section.
*** Settings ***
Library SeleniumLibrary
Library OperatingSystem
Library Collections
Resource resources.robot
Resource invoice-details-page.robot
Resource navigation.robot
Resource system.robot
Resource data.robot
Suite Setup Run Keywords Initialize Test Data Configure Selenium Navigate To Homepage
Suite Teardown Exit Selenium
#Settings - Library
We've already talked about the Library keyword and how we can use preexisting libraries full of keywords in
our test cases. The Library keyword can pull in preexisting keywords.
Your developers can make libraries for Robot Framework. You can even pull in Python packages and use them
here.
Any publicly exposed method or function in Python can be exposed as a keyword by pulling it in with the
Library keyword.
#Settings - Resource
Resources are files that you build.
You may have files that have keywords in them. You may have some that have variables in them.
33
Chapter 6: Settings
There are lots of different ways to use the Resource keyword, but it helps you keep your keywords in certain
groups.
• For instance, in this case, we've grouped together all navigation keywords into a file called
navigation.robot.
• We've basically got a Page Object in the invoice-details-page.robot file.
• We've got system keywords in the system.robot file.
• And we've got our data that we use in the data.robot file.
There are a lot of other cool things you can do with the Settings section.
Make sure to take a look robotframework.org and the documentation there to learn more about different
keywords you can use in the Settings section.
**The Suite Setup allows you to run specific keywords prior to the entire suite running. **
Here, I've got a keyword called Run Keywords allowing me to run more than one keyword sequentially.
Suite Setup Run Keywords Initialize Test Data Configure Selenium Navigate To Homepage
The Initialize Test Data is one of the custom keywords in one of the resources files.
Navigate To Homepage is the third keyword I call with run keywords in the suite setup section.
We run these in order from left to right prior to this suite or this file running.
**After all the test cases are done, we do something called Exit Selenium using this Suite Teardown. **
This is a little bit different in terms of how control flow works within Robot Framework.
We previously said that control flow works by executing the test case with the Variables section first, the
Settings section second, the Test Cases section third.
• Suite Setup gets run prior to all test cases in this suite.
• Suite Teardown runs after all test cases in this suite.
But both of these keywords are in this Settings section. I know that's a little confusing.
Take a look at the documentation in the BuiltIn library for different explanations that might work better
for you.
The Settings section is basically something you can think of as your configuration, your steps before the test
case, your pre-conditions.
34
Chapter 6: Settings
35
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 ***
# 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}.
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.
36
Chapter 7: Variables
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”.
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 ***
37
Chapter 7: Variables
# 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
38
Chapter 7: Variables
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”.
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.”
Excellent.
39
Chapter 7: Variables
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.
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.
40