0% found this document useful (0 votes)
103 views

Introduction To Robot Framework

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

Introduction To Robot Framework

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Introduction to Robot Framework

Prepared by:

Alaa Atitallah
Content
Chapter 1: A Brief Overview of Robot Framework …………………………………………………………………………………………………………………………. 1
#Test Cases Section

#Settings Section

#Running the Suite

Chapter2: Project Setup ……………………………………………………………………………………………………………………….………………………………………………………………………………. 6


#Install Python and Pip

#Installing Robot Framework

#Installing SeleniumLibrary and WebDrivers

Chapter 3: Running tests ……………………………………………………………………….…………………………………………….……………………………………………………………………………. 9


Chapter 4: Keywords …………………………………………………………………………………………………………………………………………………………………..……………………………………………. 12
#Keywords Created in Libraries

#Robot Standard Library Keywords

#Creating Custom Keywords

#Challenge

#Example Code - keyword-basics.robot

#Example Code - keyword-basics.answer.robot

#Another Learning Example…

#In Review…

Chapter 5: Test cases ………………………………………………………………………………………………………………………………………………………………..…………………………………………. 27


#Robot Test Flow

#Quick Recap

Chapter 6: Settings ……………………………………………………..………………………………………………………………………………………………………………..…………………………………………. 33


#Settings - Library

#Settings - Resource

#Settings - Suite Setup and Suite Teardown

Chapter 7: Variables …………………………………………………………………………………………………………………………………………………………………..……………………………………………. 36


#Section Variables

#Inline Variables
Chapter 1: A Brief Overview of Robot Framework

What’s Robot Framework?


Robot framework is a test driver. It allows you to easily create and read automated scripts. Much like
Cucumber, human readable scripts enable better communication between stakeholders, developers, and
testers.

Test drivers are helpful in implementing Acceptance Test Driven Development (ATDD) and Behavior Driven
Development (BDD).

Let's walk through an example:

#Test Cases Section


Tests in robot framework are defined in a test cases section of a test file.

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

#Running the Suite


I run the suite using the command robot and the file name of the test suite I want to run — robot tour-
of.robot.

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.

As I mentioned, this test uses SeleniumLibrary to drive the web browser.

Now that the test is run and passed, let's look at the reports.

These reports are automatically generated.

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.

**Let’s look a bit deeper at the logs. **

The logs show us every keyword used in the test.

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

2. Robot Framework allows you to

• Avoid all manual testing


• Easily create and read test scripts
• Ignore your product owner
• Automatically test your application

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.

The exercises and materials in this course are available on GitHab at


https://github.com/alaaatitallah/Robot_Framework_Project.

After downloading or cloning the repository, you'll notice several exercises in the directory. They're not
sequentially numbered.

Now, take a look at the README file in the 000-ProjectSetup directory.

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.

#Install Python and Pip


Next, we need to install Python. Python is the programming language Robot Framework is written in.

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.

When you're done, click Close.

#Installing Robot Framework


Now we'll install Robot
Framework using pip.

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.

#Installing SeleniumLibrary and WebDrivers


Finally, we need to install SeleniumLibrary and some WebDrivers.

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 need to install SeleniumLibrary in order to do this.

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.

To install it, we type webdrivermanager chrome.

Finally, confirm everything is installed properly by typing chromedriver --version.

When all this is done, you're installed and ready to go. Congratulations

1. Which of the following is required to be installed for Robot Framework to work?


• Python
• Selenium
• Git
• An IDE

2. The exercises for this course are designed to work with which major version of Python?
• 3.0
• 8.0
• 1.0
• 2.0

3. Use Git to:


• Start Robot Framework
• Create tests
• Clone or download the exercises for this course from gitlab.

8
Chapter 3: Running tests

As I mentioned, make sure to download or clone the repository.

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.

The command for running Robot Framework tests is robot.

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.

We see the Output, the Log, and the Report.

The report is the one you're going to want to look at first.

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

In this case, it's green because it passed.

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.

Any step that fails will show up as red.

This is one of the great benefits of Robot Framework. It has very clear output in a nice browser-based form.

This has been how to run a Robot test.

1. The command to run tests in Robot Framework is


• test
• run
• robot
• cd

2. What is generated after a test suite runs?


• A report and logs
• A working application
• A pull request
• A defect report

3. Which tool is used to execute Robot Framework tests?


• Selenium
• Jira
• Chrome
• Command line

11
Chapter 4: Keywords

In this chapter, we're going to learn a few things.

• The 2 places keywords can come from.


• We're going to learn what a keyword is.
• We're going to learn how to identify the correct syntax for a keyword.
• We're going to use a keyword in a test script.
• We’ll find keywords in keyword documents.
• You'll also create your own keyword.

There are 2 main ways to use keywords in your script — one is by a library; the other is by creating it yourself.

We're going to talk about using those created in libraries first.

#Keywords Created in Libraries


Go with me into chapter 004-Keywords. In this directory you'll find a file called example-settings.robot.

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.

So, what is a keyword?

A keyword is a piece of functionality in a test case. We talked about test cases briefly earlier.

#Example Code - example-settings.robot


*** Settings ***
#Library #?

*** Test Cases ***


Create an Invoice
Comment This is my first RobotFramework test case!
Open Browser http://inv.beaufortfairmont.com:8082/ chrome
We know that we've got a Settings section.

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.

We notice we have a Test Cases section in this file as well.

A Robot Framework test file is really a test suite. It contains 1 or more tests.

The Test Cases section holds our test cases.

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

This test case is called "Create An Invoice".

There are only 2 steps to this test case:

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

The keywords here are Comment and Open 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!").

An argument is a piece of information we give a keyword to tell it to do something special.

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.

Example Code - extra-large-test.robot


*** Settings ***
Library SeleniumLibrary
Library RequestsLibrary
Library String
Library Collections

Suite Setup Run Keywords


Suite Teardown Run Keywords Close Browser

*** Test Cases ***


Create an Invoice
Open Browser http://inv.beaufortfairmont.com chrome
${invoiceId}= Generate Random String 10 [LETTERS]
Set Suite Variable ${invoiceId}
Set Selenium Speed 0.5 Seconds
Click Link Add Invoice
Page Should Contain Element invoiceNo_add

13
Chapter 4: Keywords
Input Text invoice ${invoiceId}

Fill in invoice information

Click Button createButton


Create Session invoice-manager http://inv.beaufortfairmont.com:8082
${resp}= Get On Session invoice-manager /invoices/${invoiceId}
Should Be Equal As Strings ${resp.status_code} 200
Dictionary Should Contain Value ${resp.json()} ${invoiceId}
${resp}= Delete On Session invoice-manager /invoices/${invoiceId}
Should Be Equal As Strings ${resp.status_code} 200
${resp}= Get On Session invoice-manager /invoices/${invoiceId}
expected_status=any
Should Be Equal As Strings ${resp.status_code} 404

*** Keywords ***


Fill in invoice information
Input Text company my example company
Input Text type plumbing
Input Text price 34.00
Input Text dueDate 2018-10-31
Input Text comment Unclogged Drain
Select From List By Value selectStatus Past Due
This is a really long test case; it does a lot of steps.

The steps, in this case, almost all come from libraries. In fact, most of them come from the SeleniumLibrary.

If you're familiar with Selenium, these keywords probably look familiar.

• Open Browser, we saw earlier.


• Click Link sounds very familiar to Selenium users.

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.

But where are we getting the functionality for these keywords?

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.

What you want to do here is look at the keyword documentation.

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.

As you scroll down, you'll notice even more helpful material.

Remember, we were looking for Open Browser.

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.

Here we are in Open .

Notice we pass in the URL, which happens to be the address inv.beaufortfairmont.com.

And we also pass in the Chrome browser as an argument.

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.

#Robot Standard Library Keywords


You'll notice a lot of other ones here in our code example.

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.

You'll notice things like


the [Process](https://robotframework.org/robotframework/latest/libraries/Process.html) library.

The [String](https://robotframework.org/robotframework/latest/libraries/String.html) library,


which I mentioned earlier. We use "Generate Random String to generate the invoice number for the invoice
in this example.

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

Let's go back to 004-Keywords directory and look at the example-settings.robot file.

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.

This test case fails because we haven't completed it yet.

It basically says, "No keyword with name open browser found".

That's because we haven't told the script where to find it yet.

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

*** Test Cases ***


Create an Invoice
Comment This is my first RobotFramework test case!
Open Browser http://inv.beaufortfairmont.com/ chrome
That's really all you need.

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.

#Creating Custom Keywords


We're going to create our own keyword.

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.

So, I'm going to use it right here in the beginning.

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.

It ran very quickly. That's a good sign.

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

#Example Code - keyword-basics.robot


*** Settings ***
Library SeleniumLibrary

*** Test Cases ***


Check invoice manager page
Comment We're learning how to create a custom keyword.
Open Browser http://inv.beaufortfairmont.com/ chrome
Page Should Contain Invoice Manager
I'll give you a hint; the steps in this are already in the test case.

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.

Now, pause the video and give this exercise a try.

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.

#Example Code - keyword-basics.answer.robot


*** Settings ***
Library SeleniumLibrary

*** Test Cases ***


Check invoice manager page
Comment We've learned how to create a custom keyword!
Navigate To Home Page
Page Should Contain Invoice Manager

*** Keywords ***


Navigate To Home Page
Open Browser http://inv.beaufortfairmont.com chrome
If we look back really quickly at how we started, you'll notice we were already calling Open Browser.

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

Let me show you that this works.

And there we go.

#Another Learning Example…


Let's take a look at another example down in directory 032-ParametersForKeywords. Let's look at example-
arguments.robot.

23
Chapter 4: Keywords
*** Settings ***

*** Test Cases ***


My Addition Test
${sum}= Add 2 3
Should be equal as integers 5 ${sum}

My Test Case
${nickName}= Set Variable Paul
Print Profile ${nickName} Merrill Trainer dpaulmerrill
${nickName}= Change Nickname ${nickName} Davie
Print Profile ${nickName} Merrill Trainer dpaulmerrill

*** Keywords ***


Add
[Arguments] ${value1} ${value2}
${value}= Evaluate ${value1} + ${value2}
[Return] ${value}

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

The second test case just does addition.

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.

And there we go.

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

The other test case here was addition.

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.

The other test case was from line 4 down to line 6.

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.

1. What are the two places keywords come from?


• The command line and GitLab
• Proprietary files and the Robot Framework Database
• Libraries and custom keywords you create
• Python and Java

2. How many spaces must surround keywords?


• At least one
• Two or more
• Two tabs-worth
• It doesn't matter

3. The information given to a keyword to do specialized work is called


• Strings
• Arguments
• Literals
• Variables

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 talked a little bit about 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.

As I mentioned before, spacing is very important in Robot Framework.

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.

Another we haven't talked about yet is called the Variables section.

#Robot Test Flow


In the Test Cases I have in front of me, from exercise directory 005-TestCases, the file first-test-case.robot,
you'll notice I only have 2 sections.
*** Settings ***
Library SeleniumLibrary

*** Test Cases ***


Check invoice manager page
Open Browser http://inv.beaufortfairmont.com/ chrome
#Verify that the text "Invoice Manager" is on the front page

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.

The first thing to happen is that the Variables section is loaded.

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.

1. First the Variables section happens,


2. Second, the Settings section, and
3. Third, the **_Test Cases _** section happens.

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 next one runs second, and so on.

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.

Like I've mentioned before, each of


these exercises has a README file.

28
Chapter 5: Test Cases

It's also got a challenge.

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.

It's a pretty simple invoicing application.

− The “Invoices” page is here.


− The “Add invoice” page is here. That allows you to add an invoice.

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.

That's your challenge; you'll do those steps here.

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.

We'll do the same with our Robot Framework tests.

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

*** Test Cases ***

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.

Remember we looked up the SeleniumLibrary to find keywords in it.

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.

Page should contain, browser should contain, should be in…

30
Chapter 5: Test Cases

The word “should” is usually associated with


some of the keywords, which assert
something, or make sure that something
works or does a validation or verification
point.

So “should” would be in there for those.

It's going to be a little tricky to learn the


language of each particular library, but you'll
be able to figure it out with a little bit of time.

Worst case, read through the keywords until


you see something that looks like it could work and then try it, see what happens.

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

1. Test cases are defined in which section of a test suite file?


• The Variables section
• The Keywords section
• The Test Cases section
• The Settings section

2. The Test Cases section is denoted with:


• ### Test Cases ###
• *** Test Cases ***
• At least 2 spaces surrounding "test cases"
• Test Cases *

3. The test case name is


• Indented
• Always upper case
• Left justified
• Always lower case

4. The steps in a test case


• Must be indented with at least 2 spaces
• Are Left Justified
• Must be indented with at least 4 spaces
• Must be indented with a tab

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

Let's talk a bit about the Settings section.

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

*** Test Cases ***


Create An Invoice
${invoice}= Get Dummy Invoice demo
Navigate To Add Invoice
Fill Out Invoice Details ${invoice}
Submit Invoice Form
${invoice_id}= Get Invoice Id ${invoice}
Page Should Contain ${invoice_id}
Open Invoice ${invoice_id}

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

#Settings - Suite Setup and Suite Teardown


Some of my favorite keywords are Suite Setup, Suite Teardown, Test Setup, and Test Teardown.

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

Configure Selenium is another one of these that I've created.

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.

So, before any test cases, we do these 3 steps.

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

This breaks that rhythm; it breaks that paradigm.

• 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

1. Which of the following is the settings section NOT used for:


• Loading resource files
• Loading libraries
• Generating test case reports
• Setting up preconditions

2. The Suite Setup keyword allows you to


• Run a keyword before all the test cases are executed.
• Run a keyword before each of the test cases is executed.
• Run a keyword after all the test cases are executed.
• Run a keyword after each of the test cases is executed.

3. In which order are the sections of a test suite run?

• Settings, Test Cases, Variables


• Test Cases, Settings, Variables
• Variables, Test Cases, Settings
• Variables, Settings, Test Cases

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 ***

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

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

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.

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

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.

39
Chapter 7: Variables

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.

40

You might also like