How to import custom files created in robot framework in Python file

201 views
Skip to first unread message

JaYa

unread,
Sep 5, 2017, 2:30:10 AM9/5/17
to robotframework-users

What exists:
---------------------

I have created some robot files .txt , which holds all user defined keywords created using robot libraries : Consider test.txt
I have one python files , which holds some functions ( which i am using in robot as python keywords): Consider ext.py

What i want to achieve:
---------------------------------

I want create some python functions (python keyword) in ext.py file, where i want t use the user defined defined keywords which are present in test.txt.


Can we do this ?




Miroslav Zapotocký

unread,
Sep 5, 2017, 2:42:18 AM9/5/17
to robotframework-users
Hi

You can find everything here: http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#extending-robot-framework

But sor example you just need to import your ext.py like a library in RF.

In ext.py:

def hello_world():
   
print "HELLO WORLD!"

in RF:

*** Settings ***
Library         ext.py

*** Test Cases ***
HelloWorld
   
Hello World


Just a simple example for ilustration. It helps me when i started with RF.

JaYa

unread,
Sep 5, 2017, 3:40:45 AM9/5/17
to robotframework-users
Hello,

Thanks for reply!!

Actually i want use the keywords present in .txt file into python file.

Means reverse case.

Tatu Aalto

unread,
Sep 5, 2017, 5:52:33 AM9/5/17
to jaya....@gmail.com, robotframework-users
Ugh

You can, if you really want, by using Run Keyword[1] from BuildIn. But usually, in my opinion, it would be best to write keywords only relying on Python.

-Tatu
Send from my mobile

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.
To post to this group, send email to robotframework-users@googlegroups.com.
Visit this group at https://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

JaYa

unread,
Sep 5, 2017, 7:43:22 AM9/5/17
to robotframe...@googlegroups.com, jaya....@gmail.com


Actually i need to use some of keywords which have written using robot framework library into python scripts.
Which is actually a need, as i need to write multilevel loop structure with multiple list paramters.


It will be very helpful, if you provide how to import the .txt file into python


I have folder structure
Automation
               TestSuite
               TestLib
                           test.txt
               ExtLib
                         ex.py


Please help me, how to import test.txt in ex.py
To post to this group, send email to robotframe...@googlegroups.com.

Bryan Oakley

unread,
Sep 5, 2017, 9:11:44 AM9/5/17
to jaya....@gmail.com, robotframework-users
You cannot import robot keywords into a python file. If you have a python-based keyword that needs to call keywords written in the robot syntax, then you need to use the "Run keywords" keyword. The technique for using built-in keywords from python is shown in the user guide here: http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#using-builtin-library

You can either import the .txt file in your test before calling the python keyword, or you can have your python keyword load it for you using the "Import Resource" keyword, also in the built-in library.

For example:

# ext.py
from robot.libraries.BuiltIn import BuiltIn
def custom_keyword():
    ...
    BuiltIn().load_resource("test.txt")
    BuiltIn().run_keyword("the_keyword", "arg1", "arg2")
    ...



--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.
To post to this group, send email to robotframework-users@googlegroups.com.

JaYa

unread,
Sep 8, 2017, 4:31:33 AM9/8/17
to robotframework-users
I did not find keyword named  load_resource

I have used 
import_resource but facing below issue

BuiltIn().import_resource("test.txt")


File "/usr/local/lib/python2.7/dist-packages/robot/libraries/BuiltIn.py", line 2770, in import_resource    self._namespace.import_resource(path)  File "/usr/local/lib/python2.7/dist-packages/robot/libraries/BuiltIn.py", line 71, in _namespace    return self._get_context().namespace  File "/usr/local/lib/python2.7/dist-packages/robot/libraries/BuiltIn.py", line 66, in _get_context    raise RobotNotRunningError('Cannot access execution context')robot.libraries.BuiltIn.RobotNotRunningError: Cannot access execution context
To post to this group, send email to robotframe...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages