Robot Framework: The Basics
Robot Framework is a test automation framework that is Python-based. This framework supports writing an object-page model in keyword driven methodology.
One of the benefits of choosing Robot Framework over other testing frameworks is its tendency of making it easier for Business people to write their own test-cases without any programming knowledge.
One of the other benefits of choosing it is its community support and it’s log files. It has very descriptive logs including complete debugging capabilities through readable logs. It also has its interface with python where writing custom functions can’t be easier than any other framework. As because data-science usage of python is increasing, the on-boarding of data-scientist on this platform becomes easier.
That all about its benefits lets right a simple test-case in Robot Framework.
Prerequisites:
Let’s go through some prerequisites.
- PyCharm
- Python
- Robot Framework Library
- Robot Framework Selenium Library
Setup:
Installing PyCharm
For using your tool of choice, my personal preference is PyCharm as its community version is easy to use and as it supports Python and multiple other plugins it is beneficial. You can use whichever tool you seem used to i.e. VS Code or Eclipse.
You can download PyCharm from here.
Installing Python
You can install Python Robot Framework can be used with both versions 2.8.x or more latest 3.8.x.
For setup on Ubuntu, you can check out this page.
For setup on Windows, you can check out this page.
For setup on Mac, you can check out this page.
Installing the Robot Framework
Once python is installed you have access to the PIP installer.
pip install robotframework
Installing Robot Framework Selenium Library
As mentioned above you can use the following command to install the robot framework selenium library.
pip install robotframework-seleniumlibrary
Project Setup:
Create a new project in PyCharm by clicking Create New Project:
Name your project whichever name you like we will choose robot-test. Please choose other settings as per your liking. You can also you Virtualenv feature in PyCharm so that you can maintain different versions of Python and Robot Framework.
Create new file from File > New… > File.
Choose the name of your liking but the filename should end with .robot.
Now you start writing your first test.
Writing your first test
First, you need to import your library which you installed on the top. ** **Settings *** on top is used to import libraries, variables and write documentation. For now, we will just import one library.
As the robot framework supports tabular format so differentiate between the objects you use double space, tabs, or a pipe i.e. ‘|’.
*** Settings ***Library SeleniumLibraryDocumentation My First Test
If you want to write multi-line documentation you can simply add ‘…’ before the line and write on as many lines as you like. These three dots are used for the continuation of the same function or comment in a new line. You will see these used further down the line.
*** Settings ***Library SeleniumLibraryDocumentation
... My First Test
... This test will Verify Google
Now everything is imported we will start writing our test-case.
To do that we need to start the section for the test-case as evident above we usually start with the same syntax i.e. ***.
*** Settings ***Library SeleniumLibraryDocumentation
... My First Test
... This test will Verify Google*** Test Cases ***Open Google & Verify Google
Open Browser <https://www.google.com> browser=chrome
${Get_title}= Get Title
Should Be Equal As Strings ${Get_title} Google
Close Browser
The robot framework follows python's indentation rule to start a new block of code. To open a browser we use the keyword Open Browser. This keyword accepts URL and browser as arguments. Get Title is the keyword. which will title the page and assign it to a variable.
A variable in the robot framework is used with ${VARIABLE_NAME}. After that verify if the title contains google. Afterward, we close the browser with the Close Browser keyword.
Writing your Keywords
Even though it would work completely fine without creating keywords we can now write your keyword so that we can reuse them.
We will separate them into two different functions, one to navigate to the website and the second one to verify the website to which we navigated to.
We created Verify Page Contains Google and Verify Page Contains Google keywords. Using them as two steps in a test-case.
Running Tests
Now we finished writing your first test-case we will now run the tests. We use robot command to run your tests.
robot first-test.robot
Once they are running they are marked as PASS in the console if everything works fine. There 3 files generated 3 files after the test has been running.
- log.html
- output.xml
- report.html
One of the best features of why I like to work with the robot framework is the log file which can be found by opening log.html in the browser.