OOP concepts: Inheritance, Polymorphism

I have perused the examples in the following repos, and it seems Robot Framework does not support inheritance and polymorphism. We can still do it in Python though so is it true that developers will have to write full Python solutions if they use polymorphism and inheritance? Look forward to more insights.

GitHub - robocorp/example-process-invoices-with-amazon-textract: This robot processes randomly generated PDF invoices with Amazon Textract and saves the extracted invoice data in an Excel file.

No need to go full Python. Robot Framework libraries are implemented in Python. Most of the time when you are including a library in Robot Framework files, that included library is written in Python (such as all the built-in libraries and almost all the 3rd party ones, too).

For example, you can include RPA.Robocorp.WorkItems in your Robot Framework script (using Robot Framework syntax).

The implementation for that library can be seen here:

There you will find, for example, abstract classes:

class BaseAdapter(ABC):
    """Abstract base class for work item adapters."""

…and concrete classes that implement those abstract classes:

class RobocorpAdapter(BaseAdapter):
    """Adapter for saving/loading work items from Robocorp Control Room.
class FileAdapter(BaseAdapter):
    """Adapter for mocking work item input queues.

So, you can use OOP concepts in your robot projects inside the Python libraries.

Robot Framework scripts rely on including those libraries. You can still split Robot Framework scripts into multiple files and import libraries in those. The imports are global, so as long as one of your project files has the import, it should be available everywhere.

Another way is to collect the keyword implementations in separate files and then include those as resources in your other files.

Bit of a sidetrack, but usually composition might be a better option than inheritance. Composition is often achieved using dependency injection.

Hopefully this answers at least a part of your question. :sweat_smile:


Thank you for the awesome answer. I’m just beginning to learn in-depths some of the OOP concepts. Glad that you point out the composition over inheritance principle.

And I do have a situation like this: I have class A and class B to represent the properties and behaviors to two different apps. Let’s say they both have a method call log_in(). Since in Robot Framework, I have not seen any example of instantiating an object and then call the method. So when I type in Log in in Robot Framework, which object am I referring to? Do you have any suggestions in this case?

Hi, @htrinh!

When you import a library in your Robot Framework script, Robot Framework parses the method names from Python code. If you have imported two libraries that both have a method with the same name, Robot Framework uses (I think) the first one it finds, but will warn you that you have multiple keywords with the same name.

Sometimes you really do need to import libraries that have name collisions. In those cases, you can choose which keyword to call by prefixing the call with the library name:

*** Settings ***
Library    A
Library    B

** Keywords **
My keyword
    # Will cause a warning.
    Log in
    # Works fine, no warning.
    A.Log in