How to customize your Robot Framework libraries

How to customize your Robot Framework libraries

ROBOT FRAMEWORK is a generic open source test automation framework for acceptance testing, acceptance test-driven development (ATDD) and robotic process automation (RPA).

It has easy-to-use tabular test data syntax and it utilizes the keyword-driven testing approach. You can build the automated script by integrating all necessary built-in, third-party keywords and users can create new higher-level keywords from existing ones using the same syntax that is used for creating test cases.

But in some cases, you need to create customized keywords to enhance your testing libraries. The core Robot Framework is implemented using Python. However, your testing libraries may code in Java or C#. 

This article will explain how to customize your Robot Framework libraries with the real examples.

Customize Python library

The simplest approach is having a module or a class in Python with methods which map directly to keyword names. Keywords also take the same arguments as the methods implementing them. Keywords report failures with exceptions, log by writing to standard output and can return values using the return statement.

Providing name and arguments to test libraries

Give a name to your test library so that you can import it using the same name in your test case. Then define the methods and arguments like this:

No alt text provided for this image

Setting test library scope

Robot Framework attempts to keep test cases independent from each other that means each test case creates new instances of test libraries. However, sometimes test cases need to share a common library. The test libraries scope can be controlled by setting a class attribute ROBOT_LIBRARY_SCOPE. This attribute must be a string and it can have the following three values:

TEST CASE: A new instance is created for every test case.

TEST SUITE: A new instance is created for every test suite.

GLOBAL: Only one instance is created during the whole test execution.

No alt text provided for this image

Specifying documentation format

You can specify the documentation format in the source code using ROBOT_LIBRARY_DOC_FORMAT attribute.

No alt text provided for this image

Importing test library

Now you can import test library into your test cases.

No alt text provided for this image


And you can search newly created keywords.

No alt text provided for this image

Check the test library works successfully.

No alt text provided for this image

Customize C# library

Python for .NET (pythonnet) is the main package that gives Python programmers nearly seamless integration with the .NET 4.0+ Common Language Runtime (CLR) on Windows. 

Preparing C# library

In Example below I’m using a fake shopping cart solution. The solution is simple:

No alt text provided for this image

Here are some main logics to add the book to cart and calculate total price.

No alt text provided for this image

Creating your Python test library

Creating a Python module or class and importing your C# libraries using pythonnet. Import .Net models in your Python project. 

No alt text provided for this image

Now you can play with your C# libraries using Python language to re-design the keyword.

No alt text provided for this image

Creating your test cases

Import test library into your test project and start creating the test cases. 

No alt text provided for this image

Check the test library works successfully.

No alt text provided for this image

CONCLUSION

Although you can use the above methods to expand your test libraries. Reduce implementing of duplicate code to use existing libraries as much as possible.

However, improper implementation will also reduce the scripts execution efficiency, making it difficult to debug and analysis problems.

Tell us about your opinions and problems. 😊

To view or add a comment, sign in

More articles by Ash Sheng

  • How to capture HTTP requests using Selenium

    Why is this needed? We all know that Selenium is the most widely used open-source tool for automating testing suite for…

    1 Comment
  • How to do & automate front-end performance testing

    Why is this needed? When talking about performance testing, most folk think of stress and load testing, which involves…

    3 Comments

Explore content categories