Extend robotframework using the robotframework python lib core
Robotframework can save you a lot of time and money in your efforts to create and maintain automated tests, with it’s keyword driven syntax and rich echo system, you can have easier access to many test automation tools
In this article I’ll be showing you how you can add more tools and functionalities to robotframework with costumed libraries.
What is robotframework?
Robotframework is a generic, open-source acceptance testing and Robotic Process Automation (RPA) framework.
Developed by Nokia Networks in 2005 as part of it’s creators, Pekka Klärck, masters thesis, it is now maintained by the robotframework foundation.
It’s written in the python programming language and it’s main feature is the keyword-driven testing implementation with a tabular syntax.
Combined with a rich eco-system and skillful contributes from all over the world, robotframework offers huge benefits to it’s users:
It can save automation developers hours of coding and debugging.
Owing to the rich eco-system, automation developers have free and immediate access to plenty of libraries, solving common automation problems.
To name a few:
- SeleniumLibrary — The most commonly used library, provides easy accesses to the selenium api to write web testing automation.
- AppiumLibrary — Mobile automation library based on appium
- RESTInstance — Easy contract testing to restful protocols.
- DataDriver — Library for quick data-driven testing, spare you from code duplication by generating tests based on data.
- Faker — Access to the python faker library, allows you to generate fake data for your tests.
- Pabot — for test parallelization.
It can be explained easily to non-coders
The keyword driven approach and tabular syntax makes the test suites easier to explain.
*** Settings ***
Documentation A test suite with a single test for valid login.
...
... This test has a workflow that is created using keywords in
... the imported resource file.
Resource resource.txt
*** Test Cases ***
Valid Login
Open Browser To Login Page
Input Username demo
Input Password mode
Submit Credentials
Welcome Page Should Be Open
[Teardown] Close Browser
this test case is very easy to share with product managers, sales personnel and even costumers, basically any stakeholder can easily understand the test cases without getting into the technical details.
Comprehensive reports
Your tests are worthless without final reports that other stakeholders can read and understand
here’s what a robotframework html reports look like:
it can be easily navigated through the different test cases, suites and tags and drilled into the logs.
Easy coding setup
Robotframework integrates easily to visual studio code with many extensions in the extensions market place.
My favorite one is the robotframework language server protocol (lsp) which provides auto completion, debugging and more.
The robotframework eco-system offers auto formatters such as robotframework-tidy and static code analyzers like robocop.
Easy to extend
Robotframework can be extended easily with python libraries or listeners to support more functionality.
In addition to that robotframework offers a whole set of tool belt, known as the robotframework python core lib, with which you can write libraries for a more stateful kind of extension.
Here’s an example of a stateless python extension:
file structure:
-test.robot
-extensions
- __init__.py
- my_library.py
extensions/my_library.py:
def my_foo_keyword_stateless(message: str) -> None:
print(message)
test.robot:
*** Settings ***
Library extensions/my_library.py*** Test Cases ***
My Test
My Foo Keyword Stateless hello world
prerequisites:
install robotframework from the pypi warehouse:
pip install robotframework==4.0.3
Lets run the test:
robot -d results test.robot
Output:
C:\Users\user\Desktop\robot-demo>robot -d results test.robot
==============================================================================
Test
==============================================================================
My Test | PASS |
------------------------------------------------------------------------------
Test | PASS |
1 test, 1 passed, 0 failed
==============================================================================
Output: C:\Users\user\Desktop\robot-demo\results\output.xml
Log: C:\Users\user\Desktop\robot-demo\results\log.html
Report: C:\Users\user\Desktop\robot-demo\results\report.htmlC:\Users\user\Desktop\robot-demo>
Now let’s look at a stateful library using the robotframework core lib
file structure:
- test.robot
- GreetingLibrary
- __init__.py
- greeting_library.py
- keywords
- __init__.py
- greeting_keywords.py
test.robot:
The robot file imports the Greeting Library and runs 2 test cases:
*** Settings ***
Library GreetingLibrary
*** Test Cases ***
Test 1
Do Your GreetingsTest 2
Do Your Greetings
GreetingLibrary\__init__.py:
from .greeting_library import GreetingLibrary
GreetingLibrary\greeting_library.py:
The main GreetingLibrary subclasses the DynamicCore Class provided by the robotlibcore module, which handles it’s interaction with the robotframework environment.
By instantiating the keyword classes (in this case it’s Greeting) as class parameters, they are only going to be instantiated once when the library class is instantiated, so the keyword classes can manage state effectively.
from typing import Any, List
from robotlibcore import DynamicCore
from .keywords import Greetingclass GreetingLibrary(DynamicCore):libraries: List[Any] = [Greeting()]def __init__(self):
DynamicCore.__init__(self, GreetingLibrary.libraries)
GreetingLibrary\keywords\__init__.py:
from .greeting_keywords import Greeting
GreetingLibrary\keywords\greeting_keywords.py:
When Greeting class is instantiated, it generates a fake name and sets the counter to 1.
The counter gets incremented with every call to the do_your_greetings method and this method is decorated with the keyword decorator which signals to the robotframeworks corelib that this should be externalized as a keyword.
from faker import Faker
from robotlibcore import keyword
from robot.libraries.BuiltIn import loggerclass Greeting:
def __init__(self)-> None:
fake = Faker()
self._who = fake.name()
self._cntr = 1
@keyword
def do_your_greetings(self)-> None:
greeting_message = f'I am please to greet you {self._who}, counter={self._cntr}'
logger.console(greeting_message)
logger.info(greeting_message)
self._cntr += 1
prerequisites:
install robotframework, pythonlibcore and faker from the pypi warehouse:
pip install robotframework==4.0.3 robotframework-pythonlibcore==3.0.0 Faker==8.10.0
Lets run the test:
robot -d results -P . test.robot
Output:
C:\Users\user\Desktop\robotframework_lib>robot -d results -P . test.robot
==============================================================================
Test
==============================================================================
Test 1 I am please to greet you Mrs. Melissa Santana, counter=1
Test 1 | PASS |
------------------------------------------------------------------------------
Test 2 I am please to greet you Mrs. Melissa Santana, counter=2
Test 2 | PASS |
------------------------------------------------------------------------------
Test | PASS |
2 tests, 2 passed, 0 failed
==============================================================================
Output: C:\Users\user\Desktop\robotframework_lib\results\output.xml
Log: C:\Users\user\Desktop\robotframework_lib\results\log.html
Report: C:\Users\user\Desktop\robotframework_lib\results\report.html
The two tests got executed with the state (the name of the person to greet and the counter) preserved.
Conclusion:
Robotframework is an awesome test automation and RPA environment.
It’s can save you and your organization time and money.
Using the offered core lib you can extend robotframeworks functionality to meet any future needs.