How to write and use custom Robot Framework Python RPA libraries
Quick recap on Robot Framework libraries and keywords
Robot Framework works in a modular way: you can easily add more keywords to your robots by adding libraries. Robot Framework includes a set of standard libraries, and you can get more libraries byinstalling additional packages.
For example, by adding the rpaframework package, you get all the keywords included in it.
Once you have added a library to your script, you can use the keywords that the library includes. For example, in this script, we are adding the RPA.Browser.Selenium
library, and we are using the Open Available Browser
and Capture Page Screenshot
keywords that it provides:
Sometimes your use case might have some specific requirements that you feel cannot be achieved with readily available libraries. Or, you might be more comfortable coding in a "lower-level" language than Robot Framework. If that's the case, a great way to still get all the Robot Framework (and the Robocorp ecosystem) benefits but get more control over the code is to add your custom libraries and keywords using Python!
Custom Robot Framework Python library examples
These examples use the default robot structure.
The simplest custom Robot Framework Python library: a single Python function
In our example use case, based on RobotSpareBin Industries Inc., our favorite fictional company, we have some sales representatives that have a weekly sales goal. We want to show each sales representative a feedback message based on their actual sales results for the week as part of a software robot.
Sounds like an interesting use case? It happens to be the story of our Beginners' course. Go and take it if you haven't already! Completing the course will grant you the Robocorp Level I certificate!
The feedback message depends on a bit of calculation, so we decide that the best approach is to have the logic in a custom Python library, which we choose to call PerformanceFeedback
. So:
- We add a file called
PerformanceFeedback.py
in the root of our package. - We add this code to it:
NOTE: You can also decide to create a different directory in your package for your library, and put your library Python file there. For this to work, you will need to add the subdirectory to the
PYTHONPATH
section of yourrobot.yaml
file.
This function expects two arguments: the actual sales result for the week and a sales target, which is different for each sales representative. We make sure that the function is operating on numbers, then we compare the sales result and the sales target, and we return a feedback message.
- Now, we can import and use the library in our
.robot
file, like in this simple example:
- We added the name of the library in the
*** Settings ***
section. - Our library contains a function called
get_sales_performance_feedback
: this gives us a keyword calledGet Sales Performance Feedback
. - The
Get Sales Performance Feedback
keyword requires two arguments like the function that it comes from, so we can call it with the sales results and targets for our two hypothetical sales representatives.
Executing our robot will now produce this log output:
We now have a basic Python library that we can use in our robot script. We can add as many new functions to the library as we need and use them as keywords.
Use a class as your custom library
You can also organize the code in your library in a class. In this case, the class and the filename of the library have to match, and the class methods become available as keywords.
As an example, to get a keyword called Get Current Date
, you could add this code in a file called ExampleLibrary.py
:
Remember: the file name and the class name must match. Otherwise, your library won't work! 🤯
Using rpaframework
in your custom Python library
Having your custom library does not mean that you have to code everything from scratch. It is very easy to import other libraries and packages and integrate them into your code. For example, you can use the RPA Framework both in Robot Framework and in Python directly.
As an example, consider the Orders
library that we created as part of the Web store order robot:
Here we are creating a new class called Orders
, with one method called get_orders
, which accepts an argument excel
, containing the path to an Excel file. We are importing the Files
and Tables
classes from the RPA.Tables
and RPA.Excel.Files
modules, and using their methods in our Python code. This code resides in a file called Orders.py
in the libraries
subdirectory in our robot.
Adding the Orders
library to our .robot
code, we are then able to use the Get Orders
keyword in our own keyword like this:
Summary
Libraries add new functionalities to your robot scripts. You can add existing libraries using available Python packages such as rpaframework
or create your own using Python code. You need to follow a set of simple conventions so that your Python methods are recognized as Robot Framework keywords. You can use rpaframework
either in Robot Framework or import its modules into your custom Python libraries.
Learn more about Robot Framework and Python
Robot Framework User Guide - Learn how to use custom Robot Framework Python libraries
Robot Framework learning resources
- What is Robot Framework?
- Basic concepts
- Basic syntax
- What are keywords?
- What are tasks?
- How to use variables?
- How to use for loops?
- How to create a while loop?
- How to do conditional IF / ELSE IF / ELSE execution?
- Robot Framework cheat sheet and quick reference
Python learning resources
Python robot examples
- Web scraper Python robot example
- A simple robot written in Python
- Employee Training Reminders With Excel Files And Python
- Working With HTML Tables
- Convert PDF Files To PNG Images Using Python