0
votes

Obviously it is possible to import Python Robot Framework library in some Python script. However, is there a magic way to import Robot Framework resource file in Python script? Resource files are written in RF syntax so there would need to be some dedicated Python module for importing it (translating RF syntax to Python actually). Perhaps something like this exists already or even some RF builtin module could be used, as if I understand correctly, during script execution RF syntax is translated to Python calls.

Update 2018-06-18:

As suggested by A. Kootstra it is possible to import Robot keyword inside Python script using:

from robot.libraries.BuiltIn import BuiltIn
BuiltIn().import_resource('${EXECDIR}/resource.robot') 

However, how can I access any keyword from imported Robot resource inside Python script ? BuiltIn().import_resource does not return any handler to imported lib.

I would call

BuiltIn.call_method

but it needs object instance as first parameter. Imported resource file is also not present in a dict returned by

globals()
1
This question is also answered by the second example in stackoverflow.com/questions/50790809/…A. Kootstra
Thanks for a hint. Indeed, it is possible to import robot resource using robot.libraries.BuiltIn import BuiltIn BuiltIn().import_resource('${EXECDIR}/resource.robot') . However, how can I access any keyword from imported Robot resource inside Python script?Marcin Kowalczyk
I updated the question according to last hintMarcin Kowalczyk

1 Answers

4
votes

BuiltIn() is part of the Robot Framework API which is described in a seperate document on ReadTheDocs. Below is a more comprehensive example that shows you how to do this:

importresource.py

from robot.libraries.BuiltIn import BuiltIn

class importresource(object):

    ROBOT_LIBRARY_VERSION = 1.0

    def __init__(self):
        pass

    def custom_keyword(self):
        BuiltIn().import_resource('${EXECDIR}/resource.robot')
        BuiltIn().run_keyword('Resource Keyword')

resource.robot

*** Keywords ***
Resource Keyword
    Log To Console   \nResource Keyword triggered.

testcase.robot

*** Settings ***
Library    importresource

*** Test Cases ***
TC
    custom keyword

Will result in:

==============================================================================
TC                                                                    
Resource Keyword triggered.
| PASS |
------------------------------------------------------------------------------