Skip to content

Instantly share code, notes, and snippets.

@aaltat
Created March 1, 2019 20:25
Show Gist options
  • Save aaltat/64dffeb571fd2081f6385b32a51b8d07 to your computer and use it in GitHub Desktop.
Save aaltat/64dffeb571fd2081f6385b32a51b8d07 to your computer and use it in GitHub Desktop.
SeleniumLibrary plugin API and keyword documentation
from collections import namedtuple
from robot.utils.importer import Importer
from SeleniumLibrary.base import DynamicCore
class Plugin(DynamicCore):
"""PLugin documentation here.
Decice what goes here
"""
ROBOT_LIBRARY_SCOPE = 'GLOBAL'
def __init__(self, plugins=None):
libraries = []
if plugins:
parsed_plugins = self._string_to_modules(plugins)
for index, lib in enumerate(self._import_modules(parsed_plugins)):
libraries.append(lib(self, *parsed_plugins[index].args,
**parsed_plugins[index].kw_args))
DynamicCore.__init__(self, libraries)
def _string_to_modules(self, plugins):
Plugin = namedtuple('Plugin', 'plugin, args, kw_args')
parsed_plugins = []
for plugin in plugins.split(','):
plugin = plugin.strip()
plugin_and_args = plugin.split(';')
plugin_name = plugin_and_args.pop(0)
kw_args = {}
args = []
for argument in plugin_and_args:
if '=' in argument:
key, value = argument.split('=')
kw_args[key] = value
else:
args.append(argument)
plugin = Plugin(plugin=plugin_name, args=args, kw_args=kw_args)
parsed_plugins.append(plugin)
return parsed_plugins
def _import_modules(self, plugins):
importer = Importer('test library')
return [importer.import_class_or_module(plugin.plugin) for plugin in plugins]
from collections import OrderedDict
from SeleniumLibrary.base import LibraryComponent, keyword
class PluginWithKwArgs(LibraryComponent):
def __init__(self, ctx, **kwargs):
LibraryComponent.__init__(self, ctx)
self.kwargs = kwargs
@keyword
def return_kw_args_as_string(self):
kwargs = OrderedDict(sorted(self.kwargs.items()))
joined_str = 'start:'
for key in kwargs:
joined_str = '%s %s=%s,' % (joined_str, key, kwargs[key])
return joined_str[:-1]
@aaltat
Copy link
Author

aaltat commented Mar 1, 2019

from collections import OrderedDict

from SeleniumLibrary.base import LibraryComponent, keyword


class PluginWithKwArgs():

        def __init__(self, ctx, **kwargs):
            # LibraryComponent.__init__(self, ctx)
            self.kwargs = kwargs

        def get_keyword_names(self):
            return [name for name in dir(self) if hasattr(getattr(self, name), 'robot_name')]

        @keyword
        def return_kw_args_as_string(self):
            kwargs = OrderedDict(sorted(self.kwargs.items()))
            joined_str = 'start:'
            for key in kwargs:
                joined_str = '%s %s=%s,' % (joined_str, key, kwargs[key])
            return joined_str[:-1]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment