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 blog post 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:

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.

Specifying documentation format

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

Importing test library

Now you can import test library into your test cases.

And you can search newly created keywords.

Check the test library works successfully.

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:

Copy to Clipboard

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

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.

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

Creating your test cases

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

Check the test library works successfully.

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. ?