Robot Framework Tutorial
Robot Framework Tutorial
Robot Framework Tutorial
i
Robot
Audience
This tutorial is designed for software programmers/testers, who want to learn the basics
of Robot Framework automation testing in simple and easy ways. This tutorial will give
you enough understanding on various functionalities of Robot Framework with suitable
examples.
Prerequisites
Before proceeding with this tutorial, you should have a basic understanding of testing
concepts.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at contact@tutorialspoint.com
i
Robot
Table of Contents
About the Tutorial ............................................................................................................................................ i
Audience ........................................................................................................................................................... i
Prerequisites ..................................................................................................................................................... i
Conclusion ....................................................................................................................................................... 3
Conclusion ..................................................................................................................................................... 16
Conclusion ..................................................................................................................................................... 24
Conclusion ..................................................................................................................................................... 39
ii
Robot
Conclusion ..................................................................................................................................................... 57
Conclusion ..................................................................................................................................................... 67
Conclusion ..................................................................................................................................................... 87
Conclusion ..................................................................................................................................................... 97
16. Robot Framework — Working With Setup And Teardown .................................................................... 171
18. Robot Framework — Working With External Database libraries .......................................................... 179
19. Robot Framework — Testing Login Page Using Robot Framework ........................................................ 191
iv
1. Robot Framework — Overview Robot
Robot Framework is an open source test automation framework for acceptance testing
and acceptance test-driven development. It follows different test case styles – keyword-
driven, behaviour-driven and data-driven for writing test cases. Robot Framework provides
support for external libraries, tools which are open source and can be used for automation.
The most popular library used is Selenium Library used for web development & UI testing.
Test cases are written using keyword style in a tabular format. You can use any text editor
or Robot Integrated Development Environment (RIDE) for writing test cases.
Robot framework works fine on all the Operating Systems available. The framework is built
on Python and runs on Jython (JVM) and IronPython (.NET).
Keywords
Robot framework comes with built-in keywords available with robot framework, keywords
available from the libraries like Selenium Library (open browser, close browser, maximize
browser, etc.). We can also create user-defined keywords, which are a combination of
other user-defined keywords or built-in or library keywords. We can also pass arguments
to those keywords, which make the user-defined keywords like functions that can be
reused.
Variables
Robot framework supports variables – scalar, list and dict. Variables in robot framework
are easy to use and are of great help while writing complex test cases.
Libraries
Robot framework has support for a lot of external libraries like SeleniumLibrary, Database
Library, FTP Library and http library. SeleniumLibrary is mostly used as it helps to interact
with the browsers and helps with web application and UI testing. Robot framework also
has its own built-in libraries for strings, date, numbers etc.
Resources
Robot framework also allows the import of robot files with keywords externally to be used
with test cases. Resources are very easy to use and are of great help when we need to
use some keywords already written for other test projects.
1
Robot
RIDE
This editor available with Robot framework helps in writing and running test cases. The
editor is very easy to install and use. RIDE makes life easy for writing test cases by
providing framework specific code completion, syntax highlighting, etc. Creation of project,
test suite, test case, keywords, variables, importing library, executing, tagging the test
case is easily done in the editor. Robot framework also provides plugins for eclipse,
sublime, Textmate, Pycharm that has support for robot test cases.
It is very easy to install and helps in creating and executing test cases. Any new
comer can easily understand and does not need any high level knowledge of testing
to get started with robot framework.
It is a good support for external libraries. Most used is Selenium Library, which is
easy to install and use in robot framework.
2
Robot
Conclusion
Robot Framework is an open source test automation framework for acceptance testing and
acceptance test-driven development. The test cases in Robot Framework are based on
keywords written in tabular format, which makes it clear and readable, and conveys the
right information about the intention of the test case. For example, to open browser, the
keyword used is “Open Browser”.
3
2. Robot Framework — Environment Setup Robot
Robot framework is built using python. In this chapter, we will learn how to set up Robot
Framework. To work with Robot Framework, we need to install the following:
● Python
● pip
● Robot Framework
Install Python
To install python, go to python official site: https://www.python.org/downloads/ and
download the latest version or the prior version of python as per your operating system
(Windows, Linux/Unix, Mac, and OS X) you are going to use.
4
Robot
Before you download python, it is recommended you check your system if python is
already present by running the following command in the command line:
Windows Installation
python --version
If we get the version of python as output then, we have python installed in our system.
Otherwise, you will get a display as shown above.
Here, we will download python version 2.7 as it is compatible to the windows 8 we are
using right now. Once downloaded, install python on your system by double-clicking on
.exe python download. Follow the installation steps to install Python on your system. Once
installed, to make python available globally, we need to add the path to environment
variables in windows as follows:
5
Robot
6
Robot
Click on Environment Variables button highlighted above and it will show you the screen
as follows:
Get the path where python is installed and add the same to Variable value at the end as
shown above.
7
Robot
Once this is done, you can check if python is installed from any path or directory as shown
below:
8
3. Robot — UNIX and Linux Installation Robot
Let us now see a few simple steps to install Python on Unix/Linux machine. Open a Web
browser and go to https://www.python.org/downloads/.
● Follow the link to download zipped source code available for Unix/Linux.
● make
● make install
Install PIP
Now, we will check for the next step, which is pip installation for python. PIP is a package
manager to install modules for python.
PIP gets installed along with python and you can check the same in command line as
follows:
Command
pip --version
9
Robot
Here we are still not getting the version for pip. We need to add the pip path to
Environment variables so that we can use it globally. PIP will be installed in Scripts folder
of python as shown below:
10
Robot
Go back to environment variables and add the path of pip to the variables list. Add
C:\Python27\SCripts to environment variables as follows:
Now open your command line and check the version of pip installed:
Command
pip install robotframework
11
Robot
Once the installation is done, you can check the version of robot framework installed as
shown below:
Command
robot --version
Install wxPython
We need wxPython for Robot Framework Ride, which is an IDE for Robot Framework.
12
Robot
For windows to get the required download for wxPython, go to the following URL:
http://sourceforge.net/projects/wxpython/files/wxPython/2.8.12.1/
And, download 32 or 64-bit wxpython for windows as per your Windows Operating system.
13
Robot
Once the installation is done, it opens the command line and auto runs some commands
as shown below:
wxPython is now installed.This module is required for the RIDE Ide to be used for Robot
Framework which is the next step.
On Linux, you should be able to install wxPython with your package manager. For
example, on Debian based systems such as Ubuntu running sudo apt-get install python-
wxgtk2.8 ought to be enough.
On OS X, you should use wxPython binaries found from the wxPython download page.
wxPython2.8 only has 32 bit build available, so Python must be run in 32-bit mode also.
This can be done globally by running:
Install Ride
Ride is Robot Framework IDE. We can use pip to install it as shown below.
Command
pip install robotframework-ride
14
Robot
Once the installation is done, open the command prompt and type the following command
to open the Ride-IDE.
Command
ride.py
15
Robot
So we are done with the installation of Robot Framework and can get started working with
it.
Conclusion
We now know how to install python, pip, robot framework and also get RIDE installed to
work with test cases in robot framework.
16
Robot
17
4. Robot Framework — Introduction to Ride Robot
Ride is a testing editor for Robot Framework. Further, we will write test cases in Ride. To
start Ride, we need to run the command shown below.
Command
ride.py
18
Robot
The above command will open the IDE as shown in the following screenshot:
In this chapter, we will walk through the editor to see what options and features are
available in the IDE. The options and features will help us in testing our project.
19
Robot
The following screen will appear when you click New Project.
Enter the name of the project. Created Path is the path where the project will get saved.
You can change the location if required. The project can be saved as File or directory. You
can also save the project in format like ROBOT, TXT, TSV or HTML. In this tutorial, we are
going to use the format ROBOT and how to write and execute test-cases.
Now, we will add a project as a file the way it is shown below. The project is named Testing
and the following screen sppears after the project is created.
The name of the project is shown on the left side and on the right side we can see three
tabs Edit, TextEdit and Run.
Edit has a lot of options on the UI as shown above. In this section, we can add data
required to run our test cases. We can import Library, Resource, Variables, Add scalar,
Add list, Add dict and Add Metadata.
The details added in the Edit section will be seen in the next tab, Text Edit. You can write
the code here in text edit section.
20
Robot
If there is any change added in Textedit, it will be seen in the Edit section. Therefore, both
the tabs Edit and TextEdit are dependent on each other and the changes done will be seen
on both.
Once the test cases are ready, we can we use the third tab Run to execute them.
21
Robot
The Run UI is as shown above. It allows to run the test case and comes with options like
start, stop, pause continue, next test case, step over, etc. You can also create Report, Log
for the test cases you are executing.
Right-click on the project created and click on new test case as shown below:
22
Robot
Enter the name of the test case and click OK. We have saved the test case as TC0. The
following screen appears once the test case is saved.
The test case has options like Documentation, setup, teardown, tags, timeout and
Template. They have an edit button across it; upon clicking the button a screen appears
wherein, you can enter the details for each option. We will discuss the various parameters
of these details in our subsequent chapters.
The test cases can be written in tabular format as shown below. Robot framework test
cases are keyword based and we can write the test-cases using built-in keywords or
keywords imported from the library. We can also create user-defined keywords, variables,
etc. in robot framework.
There are shortcuts available in the navigation bar to run/stop test case as shown below:
23
Robot
The search keyword option can be used as shown in the screenshot below:
To get the list of keywords available with robot framework, simple press ctrl+space in the
tabular format as shown below and it will display all the keywords available:
In case, you cannot remember the keyword, this will help you get the details. We have
the details available across each keyword. The details also show how to use the related
keyword. In our next chapter, we will learn how to create our first test case in ride.
Conclusion
In this chapter, we have seen the features available with RIDE. We also learnt how to
create test cases and execute them.
24
5. Robot Framework — First Test Case Using Robot
Ride
Open Ride from command prompt or you can create a shortcut of ride on your desktop.
From Desktop
Go to the path where ride is installed; for windows, it is C:\Python27\Scripts.
25
Robot
You will now see an icon of ride on your desktop. You can click on it to open the ride editor.
26
Robot
Let us start with our first test case in ride. Open the editor and click on File -> New Project.
Parent Directory is the path where the project will be saved. You can change the path if
required. I have created a folder called robotframework and will save all the files in that
folder.
28
Robot
There are 3 tabs shown for the test case created – Edit, Text Edit and Run.
The Edit tab comes with two formats – Settings and Tabular. We will discuss the two
formats in our subsequent sections.
29
Robot
Documentation
You can add details about your test case so that it becomes easy for future reference.
Tags
This is used for tagging test cases – to include, exclude specific test cases. You can also
specify if any of the test cases is critical.
30
Robot
Timeout
This is used to set a timeout on the test case. We will keep it empty for now.
Template
This will have the keywords to be used for the test case. It is mostly used for data driven
test case. The high-level user-defined keyword is specified in the template and test cases
are used to pass data to the keyword.
In the tabular format, we will write our first test case and execute the same to see the
output.
In this test case, we are just going to add some logs and see the output of it. Consider the
following screenshot to understand this:
31
Robot
Based on the keywords specified in Edit, we can get the code in Text Edit as shown below:
You may also write the test case in the Text Edit and the same will reflect in the tabular
format. Now let us Run the test case and see the output.
32
Robot
Click on start and here are is the output of the test case:
Our test case has executed successfully and the details are as shown above. It gives the
status as PASS.
We can also see the details of the test case execution in Report and Log as highlighted
below.
33
Robot
In Report, it gives the details like the start time, end time, path to the log file, status of
the test case, etc.
Click on Log at the top right corner in report or from the Run screen.
34
Robot
The Log file gives the details of the test execution and the details of keywords we gave for
the test case.
In the report and the log file, we get green color for the status.
35
Robot
Let us now make some changes that will lead to the failure of the test case fail and see
the output.
36
Robot
In the above test case, the Log keyword is wrong. We will run the test case and see the
output:
We see that the test case has failed. I have highlighted the error that it tells about the test
case.
37
Robot
38
Robot
From Log
When the test case fails, the color is changed to Red as shown above.
Conclusion
In this chapter, we covered a simple test case and the results seen during execution are
shown. The reports and logs show the details of test case execution.
39
6. Robot framework — Writing and Executing Robot
Test Cases
In this chapter, we will learn how to write and execute test cases. We would cover the
following areas in this chapter:
Project Setup
Importing Libraries
Write test case in tabular format
Using Tags for Executing Test Case
Use Resource Files for Test Case
Project Setup
Run the command ride.py to start RIDE IDE.
Command
ride.py
40
Robot
Upon clicking New Project, the screen will appear as shown below:
New Project shows the type as file or directory. By default, File is selected. We will click
on Directory to create test suite, which can have many test suites in that directory. Each
suite will have test-cases.
41
Robot
The Parent-Directory is the path where the WritingTestCases directory will be created.
Click OK to save the test suite directory.
Right-click on the directory created and click on New Suite. You can also create sub
directories with test suites in that.
42
Robot
For now, we will start with Test Suite creation as shown below:
43
Robot
Now you can add test case to the suite. Right-click on the Test suite created as shown
below:
Click New Test Case. It will display the screen to add name of the test case as shown
below:
Click OK to save the test case. We have the project setup ready.
44
Robot
Importing Libraries
Robot Framework has its own built-in library, which need not be imported. But we need to
interact with the browsers, databases, etc. To interact, we need to import the libraries.
The list of external libraries supported by robot framework are listed on robot framework
official site as shown below:
For working with browsers and web application, we are going to import Selenium Library.
The installation is discussed in the chapter Working with Browsers using Selenium
Library.
45
Robot
To import a library, we need to click main project. To the right, the setting will display the
Add Import option as shown below:
Click Library and enter the name of the library as shown below:
46
Robot
We need to repeat the same step and add library for the test suite created. Click on the
test suite created and import the library as shown below:
When you click on the test case on the left side, it will display the tabular format where
you can enter the keywords. Now, you can use the built-in keywords and the keywords
available from the selenium library.
47
Robot
Library SeleniumLibrary
TC1
Close Browser
48
Robot
We will add one more test case: TC2 in the same project.
TC2
${a} Set Variable Hi
Log ${a}
${b} Set Variable If ${number}>0 Yes No
Log ${b}
49
Robot
We can add multiple test cases under the test suite created. Click Run to execute the test
cases. The execution will take place based on the number of test cases added:
50
Robot
Click Ok to save the tag. Add the tag name in Run as shown below:
We have selected option -> Only run tests with these tags and added tag name in it.
Now, it will run only those test cases that have tag names. You can give any name and
group the test cases based on tag name and run the same. You can also use tag to skip
the test case.
51
Robot
Test case TC1 that we have created uses the following keywords:
Open Browser
Maximize Browser Window
Close Browser
We will use a user-defined keyword for the above test case. The user-defined keyword will
be available in the robot file which will be used as a resource.
We will create a file in the same directory and write our keyword as follows:
Please note details of keywords, i.e., how to create user-defined keywords are explained
in Robot Framework - Working with Keywords chapter.
The file contains various options such as Settings, Variables, and Keywords. Please note,
we cannot write test case inside the file to be used as resource. We will upload the above
file as resource for the test suite as shown below.
52
Robot
Select the test suite. On the left side, click on resource option as shown below:
Click on Resource and it will ask the path to import robot file:
Mention the path where the file is stored as shown above and click OK to add resource. It
will be displayed as shown below:
53
Robot
Now, we will change test case TC1 which has keywords as shown below:
We will add the user-defined keyword to TC1 from the resource file, i.e., Test Browser
keyword:
54
Robot
55
Robot
We have both test cases being passed. Let us now see the report and log details.
Report
56
Robot
Log
Conclusion
This chapter gives details on how to write test case, execute it, how to tag a test-case,
use resources, etc.
57
Robot
58
7. Robot framework — Keyword and Data Robot
The workflow of a test-case can be tested using keyword or data driven style. In case you
want to test the workflow with different inputs, the same can be done using data driven
test cases. We will work on an example to go through the following test case approaches:
59
Robot
The name given to the project is keywordstyle. Click OK to save the project. In this project,
we will create a user keyword as shown below.
Right-click on the name of the project and click on New User Keyword as shown below:
60
Robot
Enter the name of the keyword and the arguments it will take. Here we will give name of
the keyword as Display Message. The role of Keyword Display Message is, when it is called,
it will log a message. So we need to give an argument to it. Therefore, in the above
example the argument will be a scalar variable ${msg}.
Click OK to save the user keyword. Now we need to write the action the keywords need to
do. So, it will have tabular format as shown below where we can give the Library keywords
or built-in keywords available with Robot Framework.
Here, we will use a simple Log keyword available with Robot Framework as shown below:
61
Robot
To get more keywords available with Robot framework, press ctrl + space bar in the table
column as shown below:
So the keyword we want to use with our testcase is ready. The name of the user keyword
is Display Message and it takes one argument called ${msg}.
Let us now use this keyword in simple keyword driven style test-case. To do that we need
to create test case. Right-click on the name of the project created. Now, click New Test
Case:
62
Robot
We are done with the project setup and now will write test cases for the keyword driven
style.
In the test case, we have used the user-defined keyword Display Message in the tabular
format as shown below:
We have used the keyword we have created as shown above and passed the value Hello
World.
63
Robot
We will execute the test case TC1 and check the output:
In the above example, we have written a simple test-case which logs message and the
test case is executed with output Hello World. We can see the output Hello World printed
in the log. The test case is also passed here.
To work with data driven style, we need to create template. Template will take the name
of the high level keyword, which is a user-defined keyword like the one we created at the
start called Display Message. The arguments to that template will be sent in the form of
test-cases. We can pass different values to that template keyword. The data driven
approach is mostly used when you want to test the scenario with different data to it.
Once the test case is saved. Click on the test case and the display will be as follows:
64
Robot
Click on Edit button for Template and add the user-defined keyword.
Enter the user keyword for the template and click OK to save the template.
65
Robot
Display Message keyword takes one argument called ${msg}. This is a scalar variable.
The details passed in this test case will act as arguments to the user-defined keyword
Display Message.
In TC2, we have added Template Display Message (user-defined keyword). We have given
messages in the tabular format.
66
Robot
We can see Run executes both the Test Cases. The output shown for TC1 is Hello World.
This was the message we had given to the User Keyword Display Message.
For TC2, we used Display Message as a Template. We passed My First Test Case and
Testing Template as values in TC2. As the user keyword Display Message uses internally
Log Keyword, it displays the message in the log as shown above.
Conclusion
We have used keyword style and data driven style in this chapter and seen the working of
both. Data Driven style takes high-level user-defined keyword as a template and all the
test cases act as values to the template.
67
8. Robot — Working With Browsers Using Robot
Selenium Library
In this chapter, we will learn how to work with browsers using Robot Framework and
Selenium Library in ride.
The name given is BrowserTestCases. Click OK to save the project. Right-click on the name
of the project created and click on New Test Case:
68
Robot
69
Robot
We are done with the project setup. Now, we will write test cases for the browser.
70
Robot
Go to http://robotframework.org/
71
Robot
Select External option from above and it will list you all the libraries available to be used.
72
Robot
Click SeleniumLibrary.
73
Robot
For Installation of seleniumlibrary, we can use the command from the github and install it
using pip.
Command
pip install --upgrade robotframework-seleniumlibrary
74
Robot
Selenium library gets installed inside the lib folder in python as follows:
Once the installation is done, we have to import the library in Ride as shown in the below
steps.
75
Robot
Click on your project on the left side and use Library from Add Import:
Upon clicking Library, a screen will appear wherein you need to enter the library name:
76
Robot
The name given has to match with the name of the folder installed in site-packages. In
case the names do not match, the library name will be in red as shown below:
77
Robot
Library import in red is as good as the library does not exist inside python. Now, we have
completed selenium library import.
78
Robot
79
Robot
Here we have a list of the various drivers available for browsers. For Chrome, click Google
Chrome Driver and download the latest driver as per you operating system.
80
Robot
Click on the latest release. It will display the downloads as per the operating system –
windows, linux and mac.
Download the version as per your operating system from the above list. It downloads the
zip file. Once the file downloads, unzip it and copy the .exe driver file to python folder.
81
Robot
Now we are done installing the driver for chrome. We can get started with writing test
case that will open browser and close browser.
Go back to ride and enter the keywords for opening the browser.
Ride helps you with keywords to be used with its built-in tool. Enter the command and
press ctrl+spacebar. You will get all the details of the command as shown below:
It gives the details of the command and also examples on how to use it. In the test case,
we will open the site https://www.tutorialspoint.com/ in chrome and the test case details
will be as follows:
82
Robot
The test case has passed; we can see the site is opened in chrome browser.
83
Robot
Close browser
Here are the details of the report and log for above test cases executed.
Report
84
Robot
Log
85
Robot
86
Robot
Conclusion
We have seen how to install Selenium library and the browser drivers to work with
browsers in Robot framework. Using the selenium library keywords, we can open any given
link in the browsers and interact with it. The details of the test-case execution are available
in the form of reports and logs, which give the time taken for execution.
87
Robot
88
9. Robot Framework — Working With Textbox Robot
For testing, it becomes important to understand how to interact with the browser and
locate the html elements. It is very easy to work with input fields with robot framework.
In this chapter, we will learn how to work with textbox using Selenium Library. To work
with input field – textbox, we need the locator, which is the main unique identifier for that
textbox and it can be id, name, class, etc.
Click New Project and enter Name of your project as shown below.
89
Robot
The name given for the project is Textbox. Click OK to save the project. Right-click on the
name of the project created and click on New Test Case:
Name your test case and click OK to save it. We are now done with the project setup.
Further, we will write test cases for the textbox. Since we need Selenium library, we need
to import the same in our project.
90
Robot
Click on your project on the left side and use Library from Add Import.
Upon clicking Library, a screen will appear where you need to enter the library name:
91
Robot
The name given has to match with the name of the folder installed in site-packages.
92
Robot
In case the names do not match, the library name will show in red as in the following
screenshot:
To work with textbox, we need a locator. A locator is the identifier for the textbox like id,
name, class, etc. For example, if you are using the -
93
Robot
Now, we will add the details of the test case for textbox in ride. Here are the keywords
entered for textbox test case:
Open Browser: The keyword opens the browser for the given URL and the browser
specified.
Input Text: This keyword works on the input type and will look for the locator
name:search on the site https://www.tutorialspoint.com/ and angularjs is the value
we want to type in the textbox.
94
Robot
Upon clicking the Search icon, a screen will appear as shown in the following screenshot:
95
Robot
Report
Log
96
Robot
Conclusion
We have seen how to interact with the textbox using selenium library in robot
framework.Using the keywords available with robot framework and the library imported
we can locate the textbox and enter data and test the same.
97
Robot
98
10. Robot Framework — Working With Radio Robot
Button
For testing, it becomes important to understand how to interact with the browser and
locate the html elements. It is very easy to work with input fields with robot framework.
In this chapter, we will learn how to work with radio button using Selenium Library. To
work with radio button, we need the locator – the main unique identifier for that radio
button.
Click New Project and enter Name of your project as shown in the screenshot below.
99
Robot
100
Robot
Right-click on the name of the project created and click on New Test Case:
Give name to the test case and click OK to save it. We are done with the project setup and
now will write test cases for the radio button. Since we need Selenium library, we need to
import the same in our project.
101
Robot
Click on your project on the left side and use Library from Add Import.
Upon clicking Library, a screen will appear where you need to enter the library name:
102
Robot
103
Robot
The name given has to match with the name of the folder installed in site-packages. If the
name does not match, it will be in red as shown below:
For radio button, name is the locator. In the above example, the name is gender. We also
need the value so that we can select the radio button we want. The values in the above
example are Male and Female.
Now, we will create a test-page with radio button and open the same in the browser. Now,
select the value of the radio button. The test case details will be as follows:
104
Robot
While writing the keyword for test cases, press Ctrl + Spacebar. You will get the details of
the command.Details of Radio button:
For the radio button, the arguments are group name and value. Here are the details of the
test case for Radio button selection:
105
Robot
<html>
<head>
<title>Radio Button</title>
</head>
<body>
<form name="myform" method="POST">
<b>Select Gender?</b>
<div><br/>
<input type="radio" name="gender" value="male" checked> Male<br/>
<input type="radio" name="gender" value="female"> Female<br/>
</div>
</form>
</body>
</html>
In the above form, we are planning to select female, which is a radio button. The name
and value are given in the test case. Now, we will execute the test case and check the
selection of the checkbox for the above form.
106
Robot
Let us execute the test case and see the display in the browser:
107
Robot
Report Details
Log Details
108
Robot
Conclusion
We have seen how to select value of radio button by giving the group name of the radio
button to the test case. Using the keywords available with robot framework and the library
imported, we can locate the radio button and select the value of the radio button. We do
get the details of the test-case executed using robot framework logs and report.
109
11. Robot Framework — Working With Checkbox Robot
For testing, it becomes important to understand how to interact with the browser and
locate the html elements. It is very easy to work with input fields with robot framework.
In this chapter, we will learn how to work with checkbox using Selenium Library. To work
with checkbox, we need the locator, which is the main unique identifier for that checkbox.
The locator can be id, name, class, etc.
Click on New Project and enter Name of your project as shown in the screenshot below.
The name given for the project is Checkbox. Click OK to save the project.
110
Robot
Right-click on the name of the project created and click New Test Case:
Give name to the test case and click OK. We are done with the project setup. Now we will
write test cases for checkbox. Since we need Selenium library, we need to import the same
in our project.
111
Robot
Click on your project on the left side and use Library from Add Import.
Now, click Library. A screen will appear where you need to enter the library name:
112
Robot
113
Robot
The name given has to match with the name of the folder installed in site-packages. If the
names do not match, the library name will show in red:
For checkbox, we have the name as the locator. In the above example, the name is
option1. We also need the value so that we can select the same. Car holds the value in
the above example.
Now, we will create a test page with checkbox. Open the checkbox in the browser and
select the value.
114
Robot
While writing the keyword for test cases, press Ctrl + Spacebar. It gives all the details of
the command.Details of checkbox.
So, arguments is the locator for the checkbox. Here are the details of the test case for
Checkbox selection:
115
Robot
checkbox.html
<html>
<head>
<title>Checkbox Test Page</title>
</head>
<body>
<form name="myform" method="POST">
<b>How would you like to travel?</b>
<div><br>
<input type="checkbox" name="option1" value="Car"> Car<br>
<input type="checkbox" name="option2" value="Bus"> Bus<br>
<input type="checkbox" name="option3" value="Train"> Train<br>
<input type="checkbox" name="option4" value="Air"> Airways<br>
<br>
</div>
</form>
</body>
</html>
In the above form, we are planning to select Car, which is a checkbox. The details are
given in the test case. Now, we will execute the test case and check the selection of the
checkbox for the above form.
116
Robot
117
Robot
Details of Report
Details of Log
118
Robot
Conclusion
In this chapter, we learnt how we can select a checkbox by giving the locator of the
checkbox. The log and Reports give the details of the execution of the test case along with
the time spent for each test case.
119
12. Robot Framework — Working With Robot
Dropdown
In this chapter, we will learn how to work with dropdown using Selenium Library.
120
Robot
Right-click on the name of the project created and click New Test Case:
We are done with the project setup. Now, we will write test cases for the dropdown. Since
we need Selenium library, we need to import the same in our project.
121
Robot
Click on your project on the left side and use Library from Add Import as shown below:
Now, click Library. A screen will appear where you need to enter the library name:
122
Robot
The name given has to match with the name of the folder installed in site-packages.
123
Robot
In case the name does not match, the library name will show in red:
Library import in red is as good as the library does not exists inside python.So now we are
done with selenium library import.
<select name="carbrand">
<option value="">Select car brand..</option>
<option value="audi">AUDI</option>
<option value="bmw">BMW</option>
<option value="chevrolet">CHEVROLET</option>
<option value="datsun">DATSUN</option>
</select>
124
Robot
For dropdown, name is the locator. In the above example, the name is carbrand. We also
need the value so that we can select the same. The values in the above example are –
audi, bmw, chevrolet and datsun.
Now, we will create a test page with dropdown, open the same in the browser and select
the value from the dropdown.
While writing the keyword for test cases in RIDE, press Ctrl + Spacebar. This gives all the
details of the command.
We will work on an example to show working for all the cases mentioned above.
In our test page, we will create 3 dropdowns and will use above test cases to select the
dropdown by index, label and value.
dropdown.html
<html>
<head>
<title>Dropdown</title>
</head>
<body>
<form name="myform" method="POST">
<div>
Dropdown By Index:
<select name="months">
<option value="">Select Months.</option>
<option value="Jan">January</option>
<option value="Feb">February</option>
<option value="Mar">March</option>
<option value="Apr">April</option>
<option value="May">May</option>
<option value="Jun">June</option>
<option value="Jul">July</option>
125
Robot
<option value="Aug">August</option>
<option value="Sept">September</option>
<option value="Oct">October</option>
<option value="Nov">November</option>
<option value="Dec">December</option>
</select>
</div>
<br/>
<br/>
<div>
Dropdown By Label:
<select name="days">
<option value="">Select Day..</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
126
Robot
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
</div>
<br/>
<br/>
<div>
Dropdown By Value:
<select name="year">
<option value="">Select year..</option>
<option value="0">2000</option>
<option value="1">2001</option>
<option value="2">2002</option>
<option value="3">2003</option>
<option value="4">2004</option>
<option value="5">2005</option>
<option value="6">2006</option>
<option value="7">2007</option>
<option value="8">2008</option>
<option value="9">2009</option>
<option value="10">2010</option>
<option value="11">2011</option>
<option value="12">2012</option>
<option value="13">2013</option>
<option value="14">2014</option>
<option value="15">2015</option>
<option value="16">2016</option>
<option value="17">2017</option>
<option value="18">2018</option>
</select>
</div>
</form>
127
Robot
</body>
</html>
For index, we need to pass the locator of that dropdown – name or id and the index of the
element that needs to be selected.
Now, we want to select month as May so the index to be given in the test case is 5.
If you want to select a day, you can choose one from the dropdown.
<select name="year">
<option value="">Select year..</option>
<option value="0">2000</option>
<option value="1">2001</option>
<option value="2">2002</option>
<option value="3">2003</option>
<option value="4">2004</option>
<option value="5">2005</option>
<option value="6">2006</option>
<option value="7">2007</option>
<option value="8">2008</option>
<option value="9">2009</option>
<option value="10">2010</option>
<option value="11">2011</option>
129
Robot
<option value="12">2012</option>
<option value="13">2013</option>
<option value="14">2014</option>
<option value="15">2015</option>
<option value="16">2016</option>
<option value="17">2017</option>
<option value="18">2018</option>
</select>
If you want to select any year, take the value corresponding to the year and add the same
in test case. For example, if you want to select year 2017 the value is 17.
After execution, here is the selection done for dropdowns based on the test case:
130
Robot
Execution Details
Report Details
131
Robot
Log Details
Conclusion
We have seen how to work with dropdown by value, index and label. We can refer to logs
and reports to get the details of the test case executed.
132
13. Robot Framework — Working With Keywords Robot
In Robot Framework, test cases are constructed in test case tables using keywords. In this
chapter, we will cover the details on keywords used in Robot Framework. There are 2 types
of keywords used in Robot:
Library Keywords
User Defined Keywords
Library Keywords
Library Keywords are keywords that come from the library we import in Robot Framework.
We will now take a look at the Selenium library, which helps us interact with the browser.
We will discuss some of the important keywords associated with selenium library.
The details relating to the installation of Selenium library is discussed in chapter “Working
with Browsers using Selenium Library ”. Open ride using ride.py from the command
line.
Click on New Project and give name to your project. The name given to the project is
LibraryKeywords.
133
Robot
Right-click on the name of the project created and click on New Test Case:
We are done with the project setup. Now, we will write test cases to show the working of
library keywords. Since we need Selenium library, we need to import the same in our
project.
134
Robot
Upon clicking Library, a screen will appear where you need to enter the library name:
135
Robot
The name given has to match with the name of the folder installed in site-packages.
Now will create test case in the project created and use a few important keywords.
Click on your test case created TC1 and in the tabular form enter the keywords to open
the browser and enter data inside the form opened.
Open Browser
136
Robot
To get more details of this keyword, while typing the keyword press ctrl + spacebar. It will
show the details of the library keyword entered.
Here is an example for Open Browser, and if any help required for that keyword you can
make use of ctrl + spacebar while typing the keyword.
Similarly, we have Library keywords to work with Input, Radio, Text, etc
137
Robot
We will execute the test case we entered to open the browser with URL:
https://www.tutorialspoint.com and enter details in the input text.
We have executed the test case. You can see the textbox has all the details we gave in
the test case.
138
Robot
User-defined Keywords
User-defined keywords can be created to perform a particular action in the test case or it
can also be created using the library keywords and built-in keywords in robot framework.
We will work on an example and see how we can create keywords for our test case.
We will use the same project that we created above and create user-defined keywords in
that and use in the test case.
To create keyword in Ride, right-click on your project and click on New User Keyword as
shown below:
139
Robot
Enter the Name of the keyword and click OK. The screen also shows Arguments. We will
discuss what arguments have to do with Keywords in a subsequent section.
We have given the name BrowserDetails to the keyword. Click OK to save it. The keyword
BrowserDetails is created.
To test the URL in the browser, we repeatedly have to enter open browser, maximize
browser keywords.
Now, we will create a user-defined keyword that will have open browser and maximize
browser details. The keyword created will be used in our test case.
140
Robot
Now, we will use the keyword created in the test case as shown below.
Test case
Considering the above test case, we are going to use the user-defined keyword
BrowserDetails.
141
Robot
The name of the keyword is BrowserDetails. We can use this keyword in other test cases
created under the project. The keyword contains the URL of the browser hardcoded. If we
want to use the keyword in another test case with a different URL, it will not be possible.
We can use arguments to help us with the hardcoded parameters. We will go back to the
keyword created and make use of arguments.
142
Robot
If there is more than 1 argument, you can separate them using pipe (|). We will now use
the argument in the Keyword specified as follows:
143
Robot
Go back to your test case. Now, you need to pass the value which is the URL to be used
for the test case.
In the test case, when you type the user-defined keyword and press Ctrl + Spacebar, it
gives the details of the keyword along with the arguments.
144
Robot
The test case now will have the URL to be passed as argument.
145
Robot
The Keyword and the arguments passed to the user-defined keyword are working fine.
We have changed the argument to Input Text to the id available from google site. To get
the id or name or class of the input field, you can inspect and check in the browser.
Let us run the above test case and see the output.
146
Robot
Upon successful execution, the above test case generates the following output:
Conclusion
In this chapter, we have seen how to get help for built-in keywords. We have also seen
how to create user-defined keywords, which can be a combination of library keywords and
built-in keywords.
147
14. Robot Framework — Working With Variables Robot
In this chapter, we will discuss how to create and use variables in Robot Framework.
Variables are used to hold a value, which can be used in test cases, user-defined keywords,
etc.
● Scalar Variable
● List Variable
● Dictionary Variable
We will understand the working of each of this variable with the help of test cases in Ride.
Scalar Variable
Scalar variables will be replaced with the value they are assigned. The syntax for scalar
variable is as follows:
${variablename}
We can use scalar variable to store strings, objects, lists, etc. We will first create a simple
test case and make use of scalar variable in it.
Open RIDE using ride.py in the command line and create a new project.
148
Robot
Right-click on the name of the project created and click on New Test Case:
149
Robot
We are done with the project setup and now will write test cases for the scalar variables
to be used in our test case. Since we need Selenium library, we need to import the same
in our project.
Click on your project on the left side and use Library from Add Import:
150
Robot
Upon clicking Library, a screen will appear where you need to enter the library name:
The name given has to match with the name of the folder installed in site-packages.
151
Robot
If the name does not match, the library name will be shown in red:
152
Robot
To create scalar variable, right-click on your project and click on New Scalar as shown
below:
Clicking on New Scalar will open the following screen to create the variable and the value
we need to replace with when the variable in used inside test cases.
153
Robot
Here we need to enter the name of the variable inside the curly braces as shown in the
screen below:
We added the comment as shown above. Click OK to save the scalar variable. The details
of the variable are added as shown below:
154
Robot
Let us now use the scalar variable created inside our test case.
In the above test case, we have to replace the URL with the variable we just created
above.
Now, we will run the test case to see if it is taking the URL from the variable. Below is the
output that we get when we run it. The URL http://localhost/robotframework/login.html is
picked up from the scalar variable we created.
155
Robot
Execution Details
The advantage of using variables is that you can change the value for that variable and it
will be reflected in all test cases. You can use the variables in many test cases that you
create under that project. Hardcoding of values can be a serious problem when you want
to change something, you will have to go to individual test case and change the values for
it. Having variables in one place gives us the flexibility to test the way we want with
different values to the variables.
Now, we will look into the next type of variable called the List variable.
156
Robot
List Variable
List variable will have an array of values. To get the value, the list item is passed as the
argument to the list variable.
Syntax
@{variablename}
Suppose we have values A, B. To refer the values, we need to pass the list item as follows:
@{variablename}[0] // A
@{variablename}[1] // B
To add list variable, right-click on the project and click New List Variable.
157
Robot
Upon clicking New List Variable, a screen appears where we can enter the values:
The Name is given as @{} followed by Value. It also has 4 Columns selected. Right now,
we will use just Column 1 and create the list variable, which will have values, email id and
password as follows:
The name of the list variable is @{LOGIN_DETAILS} and the values given are
admin@gmail.com and admin, which has email id and password for the login page.
Click OK to save the list variable. The variable is listed below the project as shown here:
158
Robot
Now, we will add the list variable inside the test cases as shown below.
Here, we have hardcoded values for the Input Text and Password. Now, we will change it
to use the list variable.
159
Robot
Now, we will execute the test case to see if it is taking the values from the list variable:
It has taken the email id and password from the list variable as shown above in the test
screen.
The following screenshot shows the execution details for the same:
Dictionary Variable
Dictionary Variable is similar to list variable wherein we pass the index as an argument;
however, in case of dictionary variable, we can store the details – key value form. It
becomes easier to refer when used in the test case instead of using the index as 0, 1, etc.
Syntax
&{Variablename}
160
Robot
Suppose we are storing the values as key1=A, key2=B. It will be referred in the test case
as :
&{Variablename}[key1] // A
&{Variablename}[key2] // B
161
Robot
Upon clicking New Dictionary Variable, a screen will appear as shown below:
The Name by default in the screen is &{} and it has Value and Columns option.
We will enter the Name and the Values to be used in the test case.
Click OK to save the variable. The variable will be listed under the project and also in the
settings as follows:
162
Robot
163
Robot
We have seen the Edit and Run Tab so far. In case of TextEdit, we have the details of the
test case written. We can also add variables required in TextEdit.
164
Robot
Test case
We have used scalar variable and dictionary variable in the above test case. Here is the
code so far in TextEdit; this is based on the test case written:
The variables used are highlighted in Red. We can also create variables we want directly
in TextEdit as shown below:
We have added a scalar variable called ${new_url} and the value given is
https://www.tutorialspoint.com/.
Click Apply Changes button on the top left corner and the variable will be seen under the
project as shown below:
165
Robot
Similarly, other variables – list and dictionary variables can be created directly inside
TextEdit tab whenever required.
Conclusion
We have seen how to create and use variables. There are three types of variables
supported in robot framework – scalar, list and dictionary. We discussed in detail the
working of all these variables.
166
15. Robot Framework – Working With Command Robot
Line
In this chapter, we will learn how to make use of the command line to run test cases.
To begin with, let us open the command prompt and go to the folder where your test cases
are saved. We have created test cases and saved in the folder robotframework in C
Drive.
robot -T nameoftestcase.robot
167
Robot
We will run one of the test created from the folder as shown below:
The output, log and report paths are displayed at the end as shown above.
168
Robot
Report
Log
169
Robot
Conclusion
We can use command line to execute robot test cases. The details of the test case pass or
fail are displayed in the command line along with log and report URLs.
170
16. Robot Framework — Working With Setup And Robot
Teardown
In this chapter, we will understand two important concepts of testing world — setup and
teardown.
Setup
This is a set of keywords or instruction to be executed before the start of test suite or test
case execution.
Teardown
This is a set of keywords or instruction to be executed after the start of test suite or test
case execution.
We will work on a project setup, where will use both setup and teardown. The opening and
closing of browser are the common steps in test cases.
Now, we will add keyword open browser in the setup and close browser in teardown.
Open Ride using ride.py command from command line and create a new project.
171
Robot
Now we need to import the SeleniumLibrary to use the keywords related to browser and
interact with the pages.
172
Robot
In the above screenshot, the Settings section has Setup and Teardown options. For Setup,
click Edit to enter the keyword.
173
Robot
Click OK to save the Setup. We have entered the Keyword Open browser and also added
the arguments as shown above.
174
Robot
We have only Input Text in the test case. The opening and closing of the browser is done
from Setup and Teardown Settings.
Conclusion
Setup and teardown play a crucial role in the testing world. We have seen how to use
setup and teardown in our test cases and also how they are executed.
175
17. Robot Framework — Working with Built-In Robot
Library
In this chapter, we will cover some of the important built-in keywords, which come with
the Robot Framework. We can use these keywords along with External libraries for writing
test case. We also have the built-in library available with Robot framework by default. It
is mostly used for verifications (for example – Should Be Equal, Should Contain),
conversions (convert to integer, log to console, etc.).
We will work on a simple test case and will make use of built-in library in that.
Here are the test cases used for comparing number, string, concatenate, etc. We have
used simple keywords in the test cases below. The keywords are shown in tabular format
here:
Following is the test code for above test cases from text edit:
176
Robot
When you write your keywords in tabular format, press ctrl + spacebar. It gives the list of
built-in keywords available with Robot Framework.
177
Robot
It gives the details of each keyword with example in the corresponding window. When we
click on the corresponding window, it will open separately as shown below:
Conclusion
We have seen keywords related to string, numbers, log messages, which are available
with robot framework by default. The same can be used along with external library and
also can be used to create user-defined keyword to work with test-cases.
178
18. Robot Framework — Working With External Robot
Database libraries
We have seen how we can work with Selenium Library. The detailed installation/importing
of Selenium Library is discussed in chapter “Working with Browsers using Selenium
Library”.
In this chapter, we will discuss database library and how to connect and test database
using Robot Framework.
179
Robot
We will now take a look at the external library in this chapter. Upon clicking External, the
following screen appears:
It shows the list of external libraries supported by Robot Framework. Here, we will focus
more on the Database Library (Python). The same has been highlighted in the screenshot
above.
180
Robot
Upon clicking the Database Library (Python), you will be redirected to the screen where
the instruction for installation are listed as shown in the following screenshot:
We can install the database library using pip and the command is:
181
Robot
Once the installation is done, the next step is to import the library inside the project and
use it with test cases.
182
Robot
183
Robot
Enter the Name of the Library as DatabaseLibrary as shown below and click OK.
184
Robot
We are going to work with MySQL Database. To work with MySQL, we need to install the
module.
Command
pip install pymysql
185
Robot
We are going to use the existing database called customers available with us.
186
Robot
We have a table called customer, which has data distributed in 6 rows. Now will write test-
case which will connect to MySQL database customers and fetch the data from customer
table.
Before we start, we will create scalar variables which will hold the data for dbname, dbuser,
dbpasswd, dbhost, dbport and queryresult to store data, etc. Here are the variables
created with values:
187
Robot
We have connected to the database, checked if table customer exists in database, got the
query executed and logged the details of the query.
188
Robot
The results from the table are shown for the queryResults.
Log Details
189
Robot
Details of TC1
Conclusion
We have seen how to import database library, and the installation of it. We now know how
to connect to MySQL database in Robot Framework and test the tables.
190
19. Robot Framework — Testing Login Page Using Robot
Robot Framework
- Import Libraries
We will use all of the above features and use it to test login page in this chapter. We have
a login page which takes in email-id and password. When you enter correct email id and
password, you will be redirected to a welcome page. If the user enters invalid email id or
password, the page will get redirected to error page.
HTML Code
<html>
<head>
<title>Login Page</title>
</head>
<body>
<script type="text/javascript">
function wsSubmit() {
191
Robot
The following screen appears when either the email-id or the password is not valid:
HTML Code
<html>
<head>
<title>Login Failed</title>
</head>
<body>
<div id="loginfailed">
<h1>Login Failed</h1>
</div>
192
Robot
</body>
</html>
The following screen appears when both the email id and password are valid:
HTML Code
<html>
<head>
<title>Login Successful</title>
</head>
<body>
<div id="loginfailed">
<h1>Login Successful</h1>
</div>
</body>
</html>
Now we are going to write test cases for the above test page. To start with it, we will first
run the command to open Ride.
Command
ride.py
193
Robot
Once done, we will get started with the project setup as shown below:
We will save the type of the project as Directory. The name given to the project is testlogin.
194
Robot
Click OK to save the test suite. We need to import the Selenium Library since we will be
working with the browser.
195
Robot
Import Library in the main project and also to the test suite created.
Click Library as in the above screenshot. Upon clicking Library, the following screen will
appear.
196
Robot
Once the library is saved for the project, it will display the library in the settings:
197
Robot
Now in the main Project, we will create a setup and teardown. We would like to open the
login page in Chrome browser and maximize the window. In teardown, we will close the
browser.
For setup, we will create a user-defined keyword called Open Login Page. This keyword
will take 2 arguments, login page URL and browser name.
Now, we need 2 scalar variables that will help us store the values – url and the browser
name.
${loginpage}
${browser}
198
Robot
Now, we will add the setup and teardown for the main project.
Click on the project on the left side. In settings, click Suite Setup.
199
Robot
We have created setup that is using user keyword Open Login Page with arguments
${loginpage} and ${browser}.
Now, we have to create the user-defined keyword Open Login Page, which is done as
follows:
200
Robot
Now we need to enter the library keywords, which will open the URL.
201
Robot
For Suite teardown, we are directly using library keyword, which will close the browser.
Click OK to save the suite teardown.
202
Robot
Let us now create a setup for the test suite – Test Setup. This setup needs to get executed
first.
For the Test Setup, we have created User defined Keyword called Login Page Display,
which will take the argument as ${loginpage} as in the above screenshot.
203
Robot
204
Robot
Right-click on the test suite and click New User Keyword as shown below:
205
Robot
Let us now enter the keyword we need for the user keyword Login Page Display.
Here we want to go to the loginpage and check if the title of the page matches with the
value given.
Now, we will add template to the test suite and create data driven test cases.
206
Robot
To create template, click on the suite and on right side click Edit for Test Template.
Before we create the Test Login keyword, we need some scalar variables. The scalar
variables will have the details of the email-id, password, successpage, failurepage, etc.
207
Robot
We have created email, password, successpage and failurepage scalar variables as shown
in the above screenshot.
Now, we will create Test Login User defined Keyword. Right-click on the test suite and
click on New User Keyword.
208
Robot
The following screenshot shows the keywords entered for Test Login:
Enter Email, Enter Password, Submit Details and Login Should Fail are User Defined
Keywords, which are defined as follows:
209
Robot
Enter Email
210
Robot
Enter Password
211
Robot
Submit Details
212
Robot
Now, we will write test cases, which will take different email id and password details to
the template created.
213
Robot
The email is passed with values abcd@gmail.com and ${password} is the password stored
in the variable.
214
Robot
Invalid Password
Empty Email Id
Empty Password
215
Robot
Now, we are done with the test cases and can run the same.
Go to the Run tab and click Start to execute the test cases.
216
Robot
217
Robot
Conclusion
We have seen here how to test a login page with different inputs, which will validate if the
login is working fine or not. The details of how the execution takes place is given in the
log section.
218