Getting started with Robot Framework + Selenium for automation testing (1) — Environment setup and first example
Robot Framework is a Python based, keyword-driven framework that can be used for automation process. Together with Selenium library, it can provide a simple, reliable, and convenient approach for web/mobile automation testing.
In order to learn Robot Framework, one needs to have some basic knowledge about Python. But don’t be very scared, since Robot Framework doesn’t require a high level coding skill, just a little bit of knowledge is enough to get started. If you have already gained some insight, you must know how importing of library works. Just like other object oriented programming languages, Python has many built in libraries, and it also allows us to create custom libraries, from which we can use the functions within by simply importing them. For instance, suppose we have a function called “func1()” that resides in library “lib1”, and if we want to use it, we would probably import “lib1” in our editor. Here is some pseudo code below:
“func1()” won’t be recognised if importing fails, as it doesn’t belong to the current scope. Similarly, since it’s written in Python, Robot Framework also works with built-in or custom libraries to achieve the purpose:
Remember what I said just now? Robot Framework doesn’t require too much coding, but the question is: how can we import these libraries into our test script? Or where do we write our test? The answer is --- two GUI tools, one is called wxPython and the other one is called RIDE. wxPython is the cross-platform GUI toolkit for Python, and RIDE is the front end for Robot Framework:
Don’t worry about the content in the above screenshot at the moment, as we will talk about them in detail later. As of now let’s quickly install these components in sequence and have a look at how Robot Framework works based on the above theory.
First, install Python. We can choose either Python 2 or Python 3 and they can be easily downloaded from the official website: https://www.python.org/downloads/:
After installing it, it’s recommended to set up the environment variables based on their locations: Open environment variables -> system variables -> Select “Path” -> add both “path_to_Python” is “path_to_Python_Scripts” (“Scripts” is one of the sub directories for Python). For example, if I install python at “C:\Users\zcirc\AppData\Local\Programs\Python\Python37–32”, then “path_to_Python” will be this path, and “path_to_Python_Scripts” will be “C:\Users\zcirc\AppData\Local\Programs\Python\Python37–32\Scripts”:
Second, install Robot Framework. We can use Python package installer “pip” to install it. Simply open command prompt, and type “pip install Robotframework”. After that, if you are using Python 2, you can check the version by using command “pybot --version”; and if you are using Python 3, then the command should be changed to “robot --version”:
Third, install wxPython and RIDE. Again, this can be achieved by pip with two commands accordingly:“pip install wxpython” and “pip install robotframework-ride”:
During installation of RIDE a dialog will pop up asking if you want to have a desktop icon, it’s recommended to say yes, otherwise you would need to run command “ride.py” to launch RIDE later. You might also have noticed that I also specified RIDE version to be 2.0b1 as this is the latest version when I ‘m writing this tutorial.
Now, all major components are installed successfully, let’s launch RIDE. Sometimes it complains about missing “psutil” library at startup, and if you do encounter this, please install it with command “pip install psutil” manually. This library is important as it’s responsible for running your program.
Once RIDE is open, click “File” -> “New Project”, and observe a dialog pops up, from which we can name our project and choose the format for storing the project. Please enter “RFDemo” and select “TXT” as the format:
In this case the project will be saved into a notepad file “RFDemo.txt”. There are four different formats when creating a project but they all have the same purpose: store the project. You can try them individually if you want. Click “OK” to continue, and observe the editor opens:
There are three tabs at the top: Edit, Text Edit, and Run. Usually we use Edit tab to design program and Text Edit tab to do searching or bulk update. Execution result will show in Run tab.
There is a Settings button in Edit tab, which helps us to setup some basic configurations:
Documentation: Introduction/Purpose of the project;
Setup: Steps that are executed before the test script;
Teardown: Steps that are executed after the test script;
Tags: Annotations used for grouping test scripts;
Template: Reusable components that can be repeated in other scripts.
Again, don’t worry about these settings at the moment, and we will get there by using examples in future.
Settings are followed by the field for importing, and this is where we import libraries! Apart from libraries, we can also import resources and variables. Let’s ignore this field temporarily, as Robot Framework has a few built-in libraries which are already imported by default.
Now right click on project “RFDemo” -> New Test Case to create our first test case, name it “Test”, and click “OK”:
You must have noticed the test editor looks similar to the one for project. One of the major differences is the grid placed right below settings, and these cells are designed to write functions from imported library, or in another word: keywords.
Enter letter “L” into row 1, column1, hit Ctrl+Alt+Space, and you will see a list of candidate keywords that you can use, followed by where they are stored and instructions on how to use them:
The ones that are prefixed by “$” are variables, let’s ignore them at the moment, just focus on the keywords. Like what I said, these keywords are functions, and since we haven’t imported any library, all these functions are stored in the built-in library. Let’s open “path_to_python\ Lib\site-packages\robot\libraries\BuiltIn.py”, and you can find the candidate functions in the above screenshot with words separated by “_”. For instance, “Log To Console” has prototype of “Log_To_Console(param1, param2,…)”:
The function name will have “_” removed when being displayed as keyword on Robot Framework for user friendly purposes.
So let’s select “Log To Console”, and you will see the next cell gets highlighted in red. This means there is one mandatory parameter needed for this keyword:
If you take a closer look at the screenshot for the function body again, you will know that “message” will be the mandatory parameter. Let’s write “This is a RF Demo” as the message:
Now save the project and run the test, observe test passes and relevant information show in Run tab:
Done! We have successfully created our first test case. You may also have noticed that there are some artefacts generated: Output, Log and Report. These 3 files will help you to debug in case of any failure. For instance, the log will have the following layout and you can clearly see the output of your program:
You might be wondering how would I know which keyword I should use if I don’t know “Log To Console” but want to do the same thing. Robot Framework offers very detailed documentation for all different libraries. For instance if you want to check all available keywords in the built-in library, simple visit here.
This demo project let us have a peek at Robot Framework, however, there are still so many concepts that we need to discuss later. Ok, from next section let’s visit them one by one.