ROBOT_LIBRARY_SCOPE - confusion

1,335 views
Skip to first unread message

BS

unread,
Nov 5, 2008, 5:53:00 AM11/5/08
to robotframework-users
Hi!

I'm a bit confuse because of this issue.

I'm writing a wrapper for SSHLibrary in Python and here is how I
handle with the library:

import SSHLibrary
sshlibrary = SSHLibrary.SSHLibrary()

class SSHWrapper:
def something(self):
pass


So I create an instance of SSHLibrary because it's the only way how I
can use this library inside my library. Am I right?

Well, that library has ROBOT_LIBRARY_SCOPE = 'GLOBAL' so I thought
that after importing it in my library and importing my library in
testcase, I will see keywords from SSHLibrary as well (in a testcase).
But I don't. What I misunderstood in this point?

Also, perhaps my wrapper should has similar scope set, right?

thanks for your help!

BS

unread,
Nov 5, 2008, 9:06:25 AM11/5/08
to robotframework-users
On 5 Lis, 11:53, BS <xterr...@o2.pl> wrote:

> import SSHLibrary
> sshlibrary = SSHLibrary.SSHLibrary()
>
> class SSHWrapper:
>     def something(self):
>         pass

erkm...

import SSHLibrary

class SSHWrapper:
def __init__(self):
self._ssh = SSHLibrary.SSHLibrary()
def something(self):
pass

anything else?

Janne Härkönen

unread,
Nov 5, 2008, 9:44:20 AM11/5/08
to xter...@o2.pl, robotframework-users
On Wed, Nov 5, 2008 at 12:53 PM, BS <xter...@o2.pl> wrote:
>
> Hi!
>
> I'm a bit confuse because of this issue.
>
> I'm writing a wrapper for SSHLibrary in Python and here is how I
> handle with the library:
>
> import SSHLibrary
> sshlibrary = SSHLibrary.SSHLibrary()
>
> class SSHWrapper:
> def something(self):
> pass
>
>
> So I create an instance of SSHLibrary because it's the only way how I
> can use this library inside my library. Am I right?
>
> Well, that library has ROBOT_LIBRARY_SCOPE = 'GLOBAL' so I thought
> that after importing it in my library and importing my library in
> testcase, I will see keywords from SSHLibrary as well (in a testcase).
> But I don't. What I misunderstood in this point?
>
Hello,

the correct way to implement a test library that extends existing
library is to use inheritance:

from SSHLibrary import SSHLibrary

class MySSHLib(SSHLibrary):

def __init__(self, some, params):
SSHLibrary.__init__(self, maybe, other, params)

def some_new_keyword(self, *args):
.....

This way, you get all the keywords from the SSHLibrary directly.
To override a keyword, just implement a method with same name in your own class.
You need to define the __init__ only if you want to things in addition
to SSHLibrary __init__.
If your own class does not have __init__, the SSHLibary.__init__ is
called automatically.


Hopefully this helps,

__janne

BS

unread,
Nov 6, 2008, 4:14:50 AM11/6/08
to robotframework-users
On 5 Lis, 15:44, "Janne Härkönen" <janne.t.harko...@gmail.com> wrote:

> the correct way to implement a test library that extends existing
> library is to use inheritance:

Oh! Yeah! This way is the best one! Thanks for your support.

Mark Hudson

unread,
Jan 11, 2013, 8:08:59 PM1/11/13
to robotframe...@googlegroups.com
How about if I have a lib that is both included by test.txt scripts, as well as by pybot --listener path/to/lib.py at runtime.
It seems that there are two different instances, no matter what I set the ROBOT_LIBRARY_SCOPE value.
How to have the lib-calls share data with the listener instance?

Kevin O.

unread,
Jan 12, 2013, 8:52:16 PM1/12/13
to robotframe...@googlegroups.com
this is one way to do it by getting the instance at test start and putting it in an attribute.

from robot.libraries.BuiltIn import BuiltIn

def currlib():
    return BuiltIn().get_library_instance("SSHLibrary")

class MyListener(object):

    ROBOT_LISTENER_API_VERSION = 2   

    def start_test(self,name,attrs): 
        self.sshlib = currlib()
        ...

Kevin

Pekka Klärck

unread,
Jan 14, 2013, 3:39:30 PM1/14/13
to mar...@gmail.com, robotframe...@googlegroups.com
2013/1/12 Mark Hudson <mar...@gmail.com>:
> How about if I have a lib that is both included by test.txt scripts, as well
> as by pybot --listener path/to/lib.py at runtime.
> It seems that there are two different instances, no matter what I set the
> ROBOT_LIBRARY_SCOPE value.

Listeners always get a separate instance regardless test library
scopes. I agree that being able to use libraries as listeners directly
would sometimes be useful.

> How to have the lib-calls share data with the listener instance?

Kevin already showed how to use BuiltIn.get_library_instance(). An
alternative solution is having some global data in the module that
contains your listener and library.

Cheers,
.peke
--
Agile Tester/Developer/Consultant :: http://eliga.fi
Lead Developer of Robot Framework :: http://robotframework.org
Reply all
Reply to author
Forward
0 new messages