Run user keyword on a specific remote server

312 views
Skip to first unread message

Carlos Bautista

unread,
Oct 8, 2013, 1:23:56 PM10/8/13
to robotframe...@googlegroups.com
Hi,

It's currently not possible to run a user keyword (high level keyword) on a specific server. Or to run the low level keywords inside that high level keyword on a variable remote server. This is an issue when (1) multiple remote libraries contain keywords with the same name or when (2) a high level keyword needs to run its low level keywords on multiple servers. This would be a useful feature because it would make high level keywords more reusable (like when importing a resource file that has high level keywords). 

For example:

*Setting*
Library Remote my.robot1.server.com:8270 WITH NAME server_1
Library Remote my.robot2.server.com:8270 WITH NAME server_2

*Test Cases*
First Test
server_1.My High Level Keyword
server_2.My High Level Keyword

*Keywords*
My High Level Keyword
Some Low Level Keyword
Another Low Level Keyword

In this example, I would like to run My High Level Keyword and all of its low level keywords on server_1 and server_2 but this cannot be specified using the server_1.My High Level Keyword syntax. I would have to have multiple high level keywords with the servers hard coded in them.

This will also not work:

*Keywords*
My High Level Keyword
[Arguments] ${host}
${host}.Some Low Level Keyword

or

*Keywords*
My High Level Keyword
[Arguments] ${host1} ${host2}
${host1}.Some Low Level Keyword
${host2}.Some Low Level Keyword


Carlos

Kevin O.

unread,
Oct 9, 2013, 10:29:16 AM10/9/13
to robotframe...@googlegroups.com
That is a well-written post.
I agree that there is a need to make using multiple instances of remote libraries easier, but no one has requested it.
I suggest you be the first.

Someone else had a very similar problem, but with the addition of using a library locally as well as on multiple remote hosts. You can read about it here:


I think the best solution is to make a library that is allows you to add and switch between instances. This is what Selenium2Library, SSHLibrary, and SudsLibrary does.

FYI, ${host1}.Some Low Level Keyword may not work, but this does (not a good solution):
Run Keyword    ${host1}.Some Low Level Keyword

At the end of this post is a library that allows you to use multiple instances of remote libraries - it is not production quality, but its a start.
Note there is no checking that remote libraries added even have the same keywords as the first. Keyword names/arguments, etc. are retrieved from the first library instance.

Here are some bits to show how it would be used:

Library           /path/to/MultiRemote.py    http://192.168.104.100:8270/    HostA    WITH NAME    MoreDescriptiveNameThanMultiRemote

In a suite setup:
    Add Remote Library    http://192.168.104.200:8270/    HostB

In the test case,
    Switch Remote Library    HostA
    My High Level Keyword
    Switch Remote Library    HostB
    My High Level Keyword

*Keywords*
My High Level Keyword
    # user keywords do not need to know or care about which host is being used
    Some Low Level Keyword
    Another Low Level Keyword

Cheers,
Kevin Ormbrek

Below is the contents of MultiRemote.py:

from robot.libraries.Remote import Remote
from robot.api import logger
from robot.utils import ConnectionCache


class MultiRemote(object):

    ROBOT_LIBRARY_SCOPE = 'SUITE'

    def __init__(self, uri, alias=None):
        self._cache = ConnectionCache(no_current_msg="No current remote library")
        self.add_remote_library(uri, alias)

    @property
    def current(self):
        return self._cache.current
    
    def get_keyword_names(self):
        names = self.current.get_keyword_names()
        return names + ['add_remote_library', 'switch_remote_library']

    def get_keyword_arguments(self, name):
        if name == 'add_remote_library':
            return ['uri', 'alias=None']
        if name == 'switch_remote_library':
            return ['index_or_alias']
        return self.current.get_keyword_arguments(name)

    def get_keyword_documentation(self, name):
        if name == 'add_remote_library':
            return "Adds a remote library with the given alias.\n\nReturns the index of the current remote library."
        if name == 'switch_remote_library':
            return "Switches to the remote library with the given alias or index."
        return self.current.get_keyword_documentation(name)

    def run_keyword(self, name, args):
        if name == 'add_remote_library':
            return self.add_remote_library(*args)
        if name == 'switch_remote_library':
            return self.switch_remote_library(*args)
        return self.current.run_keyword(name, args)

    def add_remote_library(self, uri, alias=None):
        remote = Remote(uri)
        logger.info("Adding remote library with alias '%s' served from %s" % (alias, uri))
        return self._cache.register(remote, alias)

    def switch_remote_library(self, index_or_alias):
        self._cache.switch(index_or_alias)
        logger.info("Switching to remote library with alias or index %s" % index_or_alias)

Kevin O.

unread,
Oct 31, 2013, 10:30:08 PM10/31/13
to robotframe...@googlegroups.com
How did you solve your issue? Did you try the code I provided?
Reply all
Reply to author
Forward
0 new messages