Access Telnet object and connections from python library

385 views
Skip to first unread message

Mark Irvine

unread,
May 3, 2011, 1:37:10 PM5/3/11
to robotframework-users
Hello,

I'm trying to do some tricky tasks with telnet connections. I would rather push those into a python library, but use the RF Telnet library to manage connections. What I want to do is:

1. Use the RF telnet library to make the connection and log in, and assign an alias to that connection.
2. then in my python library, I'd like to pass that alias to a function, and then run various commands with that connection.

Is it possible to get a reference to the Telnet object so that I can do this?

So In my RF resource file I have:

============  ===========
Setting       Value   
============  ===========
Library       Telnet  
============  ===========

============  ==================   =========== ================
Keyword       Action                Argument   Argument           
============  ==================   =========== ================
login task    [Arguments]          ${address}   ${alias}              
\             Open Connection      ${address}  ${alias}
\             Login                ${user}     ${pass}                                         
\             Set prompt           \\# 
============  ==================   =========== ================

In my python Library I want to do something like:

from Telnet import *

[somehow get a reference to the Telnet connection object I make when I run the keyword 'login tasks']

telnet.switch_connection('my_alias')
telnet.read()

Thanks,
Mark

Pekka Klärck

unread,
May 4, 2011, 4:32:52 AM5/4/11
to ma...@irvinesonline.org, robotframework-users
2011/5/3 Mark Irvine <ma...@irvinesonline.org>:

> Hello,
> I'm trying to do some tricky tasks with telnet connections. I would rather
> push those into a python library, but use the RF Telnet library to manage
> connections. What I want to do is:
> 1. Use the RF telnet library to make the connection and log in, and assign
> an alias to that connection.
> 2. then in my python library, I'd like to pass that alias to a function, and
> then run various commands with that connection.
> Is it possible to get a reference to the Telnet object so that I can do
> this?

There are several ways to extend an existing library explained in the
User Guide:
http://robotframework.googlecode.com/hg/doc/userguide/RobotFrameworkUserGuide.html?r=2.5.7#extending-existing-test-libraries

If you are creating a new library, the easiest approach would probably
be extending Telnet directly either via inheritance like

from robot.libraries.Telnet import Telnet

class MyCoolLibrary(Telnet):
def my_cool_keyword(self, arg):
self.write(...)

or via composition like

from robot.libraries.Telnet import Telnet

class MyCoolLibrary(object):
def __init__(self):
self._telnet = Telnet(...)
def my_cool_keyword(self, arg):
self._telnet.write(...)

In the former case you get all the keywords from Telnet automatically
so it's probably easier. If your users already use Telnet, and you
just want to add new functionality to it, you might want to get the
active Telnet library instance from Robot via
BuiltIn.get_library_instance method:

from robot.libraries.BuiltIn import BuiltIn

class MyCoolLibrary(object):
@property
def _telnet(self):
return BuiltIn().get_library_instance('Telnet')
def my_cool_keyword(self, arg):
self._telnet.write(...)

All the above approaches are explained in more detail in the User
Guide. Notice that in this last example you cannot assign
`self._telnet = Telnet(...)` in `__init__` due to this limitation in
Robot:
http://code.google.com/p/robotframework/issues/detail?id=646

Cheers,
.peke
--
Agile Tester/Developer/Consultant :: http://eliga.fi
Lead Developer of Robot Framework :: http://robotframework.org

Mark Irvine

unread,
May 4, 2011, 12:03:35 PM5/4/11
to Pekka Klärck, robotframework-users
That's just what I was looking for: BuiltIn().get_library_instance('Telnet')

And does exactly what I need. I was still running on 2.1.3 so I've upgraded to 2.5.7 take advantage of this.

Thanks,
Mark
Reply all
Reply to author
Forward
0 new messages