I have a following example keyword in my custom Robot Framework library that uses Robot Framework's BuiltIn library to call another keyword inside the test sequence using parameters:
# MyLibrary.py
from robot.libraries.BuiltIn import BuiltIn
class MyLibrary(object):
def run_a_keyword(self, keywordName):
builtinLib = BuiltIn().get_library_instance("BuiltIn")
parameters = ['hi', 'second param']
# Runs the keyword with the given parameters
builtinLib.run_keyword(keywordName, *parameters)
I would be running for example a following simplified test to check that the keyword works property by testing the errors as well:
*** Settings ***
Library MyLibrary.py
*** Test Cases ***
Test Case
Run A Keyword My Keyword
Run Keyword And Expect Error Keyword 'Another Keyword' expected 3 arguments, got 2. Run A Keyword Another Keyword
*** Keywords ***
My Keyword
[Arguments] ${arg1} ${arg2}
Log To Console Keyword Executed
Another Keyword
[Arguments] ${arg1} ${arg2} ${arg3}
Log To Console Keyword Executed
I would expect that this test case would pass, but the test case fails in the second step with Run Keyword and Expect Error despite I had stated that the error was expected?
My workaround for this was to catch the exception thrown by builtInLib's call inside my keyword and re-throw it, after this the test sequence with Run Keyword And Expect Error works properly:
def run_a_keyword(self, keywordName):
builtinLib = BuiltIn().get_library_instance("BuiltIn")
parameters = ['hi', 'second param']
try:
builtinLib.run_keyword(keywordName, *parameters)
except Exception as err:
raise Exception(err.message)
However, I need a listener to pass the errors to a service:
class MyListener(object):
ROBOT_LISTENER_API_VERSION = 2
ROBOT_LIBRARY_SCOPE = 'GLOBAL'
def __init__(self):
self.ROBOT_LIBRARY_LISTENER = self
def log_message(self, message):
level = message['level']
if level == 'FAIL':
send_the_error_to_somewhere(message['message'])
The log_message is called twice (when the built-in function is called and when I raise the new exception). This causes that the same error would be handled and logged twice which is not what I want.
So: How can I Run Keyword And Expect Error with a keyword that calls a built in function and still handle the error only once?