Skip to content

Commit b430137

Browse files
committed
Implemented support for @Keyword decorator
get_keyword_names populates the mapping from an 'embedded argument name' to a 'function name'. Assumed is that get_keyword_names is always called before run_keyword. Tags passed to the decorator are added at the end of the keyword documentation using Tags: tag1, tag2 syntax. The test does not check if tag1 and tag2 have been added to the keyword yet. issue: #35
1 parent de85cf8 commit b430137

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

src/robotremoteserver.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,16 +190,27 @@ class StaticRemoteLibrary(object):
190190

191191
def __init__(self, library):
192192
self._library = library
193+
self._robot_name_index = {}
193194

194195
def get_keyword_names(self):
195-
return [name for name, value in inspect.getmembers(self._library)
196-
if name[0] != '_' and is_function_or_method(value)]
196+
names = []
197+
for name, value in inspect.getmembers(self._library):
198+
if is_function_or_method(value):
199+
if getattr(value, 'robot_name', None) not in (None, ''):
200+
names.append(value.robot_name)
201+
self._robot_name_index[value.robot_name] = name
202+
else:
203+
if name[0] != '_':
204+
names.append(name)
205+
return names
197206

198207
def run_keyword(self, name, args, kwargs=None):
199208
kw = self._get_keyword(name)
200209
return KeywordRunner(kw).run_keyword(args, kwargs)
201210

202211
def _get_keyword(self, name):
212+
if name in self._robot_name_index:
213+
name = self._robot_name_index[name]
203214
kw = getattr(self._library, name, None)
204215
return kw if is_function_or_method(kw) else None
205216

@@ -227,7 +238,11 @@ def get_keyword_documentation(self, name):
227238
return inspect.getdoc(self._library) or ''
228239
if name == '__init__' and inspect.ismodule(self._library):
229240
return ''
230-
return inspect.getdoc(self._get_keyword(name)) or ''
241+
keyword = self._get_keyword(name)
242+
doc = inspect.getdoc(keyword) or ''
243+
if len(getattr(keyword, 'robot_tags')):
244+
doc += "\nTags: %s\n" % ', '.join(keyword.robot_tags)
245+
return doc
231246

232247

233248
class HybridRemoteLibrary(StaticRemoteLibrary):

test/atest/keyword_decorator.robot

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
*** Settings ***
2+
Resource resource.robot
3+
Suite Setup Start And Import Remote Library keyword_decorator.py
4+
Suite Teardown Stop Remote Library
5+
6+
*** Test Cases ***
7+
Keyword with 2 arguments
8+
Add 7 Copies Of Coffee To Cart
9+
10+
When embedded name is empty keyword is still callable
11+
Embedded name empty
12+
13+
Tags added with keyword decorator
14+
login admin

test/libs/keyword_decorator.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from robot.api.deco import keyword
2+
3+
class Arguments(object):
4+
5+
@keyword('Add ${quantity:\d+} Copies Of ${item} To Cart')
6+
def add_copies_to_cart(self, quantity, item):
7+
pass
8+
9+
@keyword('')
10+
def embedded_name_empty(self):
11+
pass
12+
13+
@keyword(tags=['tag1', 'tag2'])
14+
def login(username, password):
15+
'''
16+
This is keyword documentation'''
17+
18+
if __name__ == '__main__':
19+
import sys
20+
from robotremoteserver import RobotRemoteServer
21+
22+
RobotRemoteServer(Arguments(), '127.0.0.1', *sys.argv[1:])

0 commit comments

Comments
 (0)