DEV Community

TheBadCoder
TheBadCoder

Posted on • Edited on

Robot Framework Quick Start: UI Automation Tutorial

Robot Framework is an automation testing framework developed in python language. Robot framework supports most types of functional testing say User Interface testing(UI), Backend Testing like REST services testing or even Database testing.

In this particular article, we are going to learn quickstart steps to use Robot Framework for UI testing(web browser).

Audience: Any person who wishes to test web applications with automation can refer to this post. Some knowledge of python will be helpful.
Operating System considered is windows.

Folder structure for the project

robot_framework_automation
|__test-project
     |__ code
     |__ data
     |__ test
Enter fullscreen mode Exit fullscreen mode

Code

This folder will have a python file that will have exactly one class with the same name as file name, under which the user can write python functions for some operations which RobotFramework or its libraries do not support.

Data

Data folder should contain all the data files such as CSV or JSON files which could hold data that can be used for test cases for the execution or validation of test after execution.

Test

This folder should consist of all ROBOT files which are nothing but the test scripts that we have written to automate the application

Installation

Pre-requisites

1] Python [for this post we will be using python 3.x]
Find installer at the official website –
URL – https://www.python.org/

An important step during python installation
Tick the checkbox to add the python 3.x to PATH i.e Environment Variable "path"

2] Virtual Environment
Note – open cmd and navigate to test_project folder using the cd command.
Ex. cd /path/to/test_project

pip install pipenv

Installation of the framework using pip package manager

1] Create virtual environment

pipenv shell

This command isolates the project related python/robot framework installation from the other installations on the system, google virtual environments if anyone wants to know more about it.

2] Install robotframework

pipenv install robotframework

3] Install UI library
SeleniumLibrary [Mandatory for UI testing]

pip install robotframework-seleniumlibrary

Writing 1st robot test case

Documentation for Selenium Library for robot framework - https://robotframework.org/SeleniumLibrary/
This will be useful to write the UI test case

Create a file under the test folder with the name of your choice and with extension .robot and the code in the file should be as below.
Ex. basic_test.robot

*** Settings ***
Documentation     Simple example using SeleniumLibrary.
Library           SeleniumLibrary

*** Variables ***
${WEBSITE URL}      https://www.amazon.com/
${BROWSER}          Chrome

*** Test Cases ***
Open Test Website And Close Browser
    Open Test Website In Chrome
    [Teardown]    Close Browser

*** Keywords ***
Open Test Website In Chrome
    Open Browser      ${WEBSITE URL}    ${BROWSER}
    Sleep             2
    Title Should Be   Amazon.com. Spend less. Smile more.
Enter fullscreen mode Exit fullscreen mode

Run the test

1]Open the cmd
2]Navigate to test_project folder
3]Execute the command in cmd

robot test\basic_test.robot

Troubleshooting 1:

So here everything is good in test case but still, you will get failure as a result -

WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

Please read the error message carefully...!!!
So here PATH is nothing but environment variables(system) of windows machine and in that, we have the “path” variable.
In this path variable we need to add a new entry for chromedriver's executable location on our system, but wait do we have it? And why do we need it?

This Chrome driver accepts commands through selenium library which we are using and sends those commands to chrome browser which is being controlled by automation.
In short we can not automate chrome broswer without chrome driver!!

Now where we can get it?
1] We need to download official chromedriver from Google: ChromeDriver
2] Upon download, please extract the zip to some location on your computer.
3] Copy the path to this extracted chrome driver location without the file name.
4] Add this path to the Environment Variable(system) named "path".
[Google environment variables to know more about it and how we can use them]
5]Re-run the test case again from the project location in cmd-

robot test\basic_test.robot

NOW WE SHOULD BE ABLE TO SEE THE MAGIC...!!!

The test case should successfully execute the steps we have written.
1] Open chrome browser and launch URL as mentioned
2] TearDown is a standard process we will discuss it sometime later, but the close browser keyword is self-explanatory that the browser should be closed in the end.
3] Robot framework offers some nice HTML reporting we can find the report in the folder from where we executed the test. In our case it should be in test-project folder with name report.html.

Explanation of the test code

Settings section

The settings section is dedicated to writing down the dependencies of libraries and resources we need to import so that we can use them in the code.
Libraries are those packages we have installed via pip installer like-robotframework-seleniumlibrary.
We write it like-

Type of import    Name Of Package
Library           SeleniumLibrary
Enter fullscreen mode Exit fullscreen mode

Variables Section

This section can be referred to as the global variables section of the particular robot file or the tests in the robot file.

Variable Name     Default Value
${WEBSITE URL}    http://www.programmerslab.com
Here the website URL will be considered as String.
Enter fullscreen mode Exit fullscreen mode

Keywords Section

Keywords can be considered as functions in any programming language.
The purpose of keywords is the same as the functions which can be reused anywhere in the same test file.
Keywords can be used outside of the test file they are written in but the condition is to import it in the settings section as a resource, just like in any other programming language.

Open Browser

This keyword is a standard keyword from Selenium Library which we have imported and it accepts 2 parameters 1st the website URL which we want to launch and 2nd is browser name on which we want to run the test.

Title Should Be

This is again a standard keyword from Selenium Library. It is used to validate the title of the webpage opened in the browser.

Note - Now as you can see we have used standard keywords to form a custom keyword for our use, we can even use these keywords directly into test cases.

Test Cases section

1] First-line in this section is the test case name.
Ex. Open Test Website And Close Browser
2] Every indented line after the test case name constitutes the test code.
Users can declare local variables in this section which can be used for local scope i.e for the test case only.
3] We can use a series of keywords in order to execute the test case.

This concludes the quick start tutorial for the robot framework for UI testing, please comment if you feel there is some gap. We will be happy to help!!

We are going to learn more about UI automation in later posts this was just to get it started.

References:
1] Selenium Library standard keywords documentation https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html
2] Robot framework standard keywords
https://robotframework.org/robotframework/latest/libraries/BuiltIn.html

Top comments (1)

Collapse
 
mohsin708961 profile image
{{7*7}}

Awesome