Python API
RobotLogListener
- class RPA.RobotLogListener.RobotLogListener
RobotLogListener is a library for controlling logging during a Robot Framework execution using the listener API.
About keyword parameters
Parameters names and keywords for keywords Mute Run On Failure and Register Protected Keywords do not need to be full names of keywords, ie. all keywords matching even partially will be affected. Run Keyword would match all BuiltIn library keywords (17 keywords in RF 3.2.1) and of course all Run Keyword named keywords in any resource and/or library file which are imported would be matched also.
Mute Run On Failure
This keyword is to be used specifically with RPA.Browser.Selenium library, which extends SeleniumLibrary. Normally most of the SeleniumLibrary keywords execute run_on_failure behaviour, which can be set at library initialization. By default this behaviour is running Capture Page Screenshot keyword on failure.
In the example task Check the official website below the keyword Run Keyword is muted and when it runs the keyword Element Should Be Visible then those failures do not create page screenshots into log file.
It is also possible to change default failure behaviour by giving parameter optional_keyword_to_run for Mute Run On Failure, see task Check the official website with error log. This optional keyword would be then executed on failure. Due to the underlying SeleniumLibrary implementation this keyword can’t have arguments.
Example of using Mute Run On Failure without and with optional keyword to run.
*** Settings *** Library RPA.Browser.Selenium Library RPA.RobotLogListener Task Setup Set Task Variable ${TRIES} 1 Task Teardown Close All Browsers *** Tasks *** Check the official website Mute Run On Failure Run Keyword Open Available Browser https://www.robocorp.com Check for visible element Capture Page Screenshot Check the official website with error log Mute Run On Failure Run Keyword optional_keyword_to_run=Log tries Open Available Browser https://www.robocorp.com Check for visible element Capture Page Screenshot *** Keywords *** Check for visible element FOR ${idx} IN RANGE 1 20 Set Task Variable ${TRIES} ${idx} ${status} Run Keyword And Return Status Element Should Be Visible id:xyz Exit For Loop If '${status}' == 'PASS' Sleep 2s END Log tries Log Checked element visibility ${TRIES} times
Register Protected Keywords
This keyword is used to totally disable logging for named keywords. In the example below the keyword This will not output is protected and it will not be logging into Robot Framework log files.
Robot Framework
*** Settings *** Library RPA.RobotLogListener *** Tasks *** Protecting keywords This will not output # will output because called before register Register Protected Keywords This will not output This will not output # is now registered This will output *** Keywords *** This will not output Log 1 This will output Log 2
Python
from robot.libraries.BuiltIn import BuiltIn, RobotNotRunningError from RPA.RobotLogListener import RobotLogListener try: BuiltIn().import_library("RPA.RobotLogListener") except RobotNotRunningError: pass class CustomLibrary: def __init__(self): listener = RobotLogListener() listener.register_protected_keywords( ["CustomLibrary.special_keyword"] ) def special_keyword(self): print('will not be written to log') return 'not shown in the log'
- INFO_LEVEL_KEYWORDS = []
- KEYWORDS_TO_MUTE = []
- KEYWORDS_TO_PROTECT = ['rpa.robocloud.secrets.', 'rpa.robocorp.vault.', 'rpa.mfa.get_oauth_token', 'rpa.mfa.refresh_oauth_token']
- ROBOT_LIBRARY_DOC_FORMAT = 'REST'
- ROBOT_LIBRARY_SCOPE = 'GLOBAL'
- ROBOT_LISTENER_API_VERSION = 2
- end_keyword(name, attributes)
Listener method for keyword end.
- Parameters
name – keyword name
attributes – keyword attributes
If name exists in the protected keywords list then log level is restored back to level it was before settings to NONE.
- mute_run_on_failure(keywords: Optional[Union[str, List]] = None, optional_keyword_to_run: Optional[str] = None) None
Set keywords which should not execute SeleniumLibrary default behaviour of running keyword on failure.
- Parameters
keywords – list of keywords to mute
optional_keyword_to_run – name of the keyword to execute if keyword defined by keywords fail
Keyword names do not need to be full names of keywords, ie. all keywords matching even partially will be affected. Run Keyword would match all BuiltIn library keywords (17 keywords in RF 3.2.1) and of course all Run Keyword named keywords in any resource and/or library file which are imported would be matched also.
By default SeleniumLibrary executes Capture Page Screenshot on failure.
If optional_keyword_to_run is not given then nothing is done on failure, but this can be set to override SeleniumLibrary default behaviour for a set of keywords.
- only_info_level(names: Optional[Union[str, List]] = None)
Register keywords that are allowed only INFO level logging
- Parameters
names – list of keywords to protect
- register_protected_keywords(names: Optional[Union[str, List]] = None) None
Register keywords that are not going to be logged into Robot Framework logs.
- Parameters
names – list of keywords to protect
- start_keyword(name, attributes)
Listener method for keyword start.
- Parameters
name – keyword name
attributes – keyword attributes
If name exists in the protected keywords list then log level is temporarily set to NONE.