Test Automation using Robot Framework
What exactly is a test automation framework?
Below are the characteristic and components of a good automation framework:
- The first thing to consider is a language like Python, Java, etc. that we can use to write our tests. It gives us the ability to create scripts.
- The reusable page or entity objects.
- Supporting libraries that give us the ability to test web or API or database or mobile applications.
- An abstraction layer that gives us the ability to write test cases in a natural language so that it can also be understood by business analysts.
- A test runner that has selective test running capabilities like the ability to tag our test cases and run them by tags.
- Test suites or test setup and teardown mechanism which gives us the ability to clean up and also not repeat setup and teardown script.
- A very important part of it is well-structured reports that have results summary with aggregation, timestamps, the ability to drill down into the test case to see what was executed, the ability to search our results, and automatically embedded screenshots if there is a failure.
- The ability to schedule our tests daily or weekly or monthly.
- The ability to integrate with a continuous integration system so that the tests can be run automatically after the build and deployment on a server.
- The support for parallel execution so we can run tests on different computers at the same time to save time.
- Finally, we want to have some sense of convention to bring consistency in the Team.
Why Robot Framework
The simple answer to the ‘Why’ is that Robot Framework does all of these things right out of the box so that we do not require to build test automation frameworks from scratch. What’s even better is that it’s open-source and free. Also, it can be installed, up and running and ready to use all these features in no time and with minimal effort. And it’s definitely the fastest and easiest to learn test automation framework.
High-level architecture
Figure 1: Test Automation Framework architecture
An Overview of Robot Framework Libraries
Figure 2: Robot Framework Libraries
Robot Framework scripts are mostly keyword-driven i.e we can type in some simple, plain English words to invoke pre-written logic in some pre-existing libraries that have been made available to us. This gives us the ability to create our scripts in a Natural Language, essentially Business Domain vernacular, and the end result is simpler, more readable test scripts. The above diagram makes it easier to visualize this. Inside of our chosen IDE, we will create a script file, which is simply a text file with a dot robot extension. And those script files will reference workflow files which are lower-level keywords, also text .robot files and those workflows themselves would be making calls to the various libraries. So, we can see the libraries we have access to include Built-In libraries that come with Robot Framework itself and also external libraries that can be installed by using pip3 install and then the name of the library.
Installation Steps on Mac
- Install Python and PIP (add to your Path)
- Use PIP to Install Robot Framework
- Use PIP to install Selenium Library
- Select and install desired browser Versions
- Install Selenium WebDriver for each browser
- Install the PyCharm IDE and Intellibot Plugin
- Create the base directory for the script
- Install Python and PIP (add to your Path)
$ brew install python
- Homebrew installs pip pointing to the Homebrew’d Python 3 for us
2. Use PIP to Install Robot Framework
$ pip3 install robotframework
3. Use PIP to install Selenium Library
$ pip3 install robotframework-seleniumlibrary
4. Install Selenium WebDriver for each browser
- E,g Install chrome webdriver for chrome
$ brew cask install chromedriver
6. Install the PyCharm IDE and Intellibot Plugin
- Open Pycharm and Pycharm > Preferences
Figure 3: Pycharm preferences
- Select Plugin and search and install IntelliBot @SeleniumLibrary Patched
Figure 4: IntelliBot @SeleniumLibrary Patched
6. Organizing Project files
Figure 6: Script layers
We can organize our project files by using a layered script approach. As represented in the above figure the script file contains test cases, the keywords files contain lower level keywords that are created to help the script do its work, and the page object files are a way to encapsulate locators and functionality to represent different pages in the system under test. So the script falls under a test directory, the keywords and page objects together fall under resources, If we happen to get into creating our own libraries using Python we could also consider creating a libraries directory to hold those. If instead, we had multiple products and tests we could consider an approach, where inside our test directory we have got a product one directory which might hold feature one and feature two and so on, which would be shorter scripts for specific areas of our product. And of course, this assumes there would be a product two directory and a product three directory and so on. And then inside the resources directory would have a product one directory and a product two directory and inside those directories, those would hold our corresponding keyword files for feature one, feature two, and so on. And of course a single page objects directory for each product, the reason why we might take this approach is so that we could use one common keywords file for all of the different products, so maybe for things like suite setup and suite teardown, which includes things like opening up the browser, closing browsers and such, if we don’t want to have to repeat those keywords for multiple products, we could have this one common keywords file that serves each of our multiple products. And in results, we would have a product one directory and a product two directory that would hold the results for those products respectively.
Figure 5: Project structure
Figure 7: Test script
Figure 8: Execution output
Figure 9: Results. Html
Figure 10: Log Html
Conclusion
To summarize, Robot Framework is a feature-rich framework that can assist us in design and development mature end to end automation solutions with ease. As the scripts are keywords based hence its readable and it becomes easy for different stakeholders to collaborate in the products quality engineering.
A quick view of the robot framework features:
- Simple Tabular Syntax
- Built-In keywords from the Selenium Library (open browser, close browser, maximize browser, etc.) and also user defined keyword.
- Supports a lot of external libraries like SeleniumLibrary, Database Library, FTP Library and http library.
- Possible to write test cases that work as cross-platform.
- Data-driven Test Cases
- Separate Test Data Editor
- Clear Reports
- Detailed logs
- Generic test libraries
- Support for parallel execution of test scripts
- Text editor support: Eclipse, Emacs, Vim, Visual studio code
- It can be containerized and integrated with a CI tool like Jenkins
References :
PS: If you liked this, click the 👏 below. I notice each one and I’m grateful for every one of them.
For more musings about programming, follow me so you’ll get notified when I write new posts.