You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Clean-up semi-public API to register "run keyword variants" and to disable variable resolving in arguments #2190
Robot has a mechanism to disable resolving variables in arguments using robot.libraries.BuiltIn.register_run_keyword method. This also makes the method so called "run keyword variant" which slightly changes the logic how the keyword is executed. This whole system is a mess that needs to be cleaned up. At least the following things need to be considered:
Separate mechanism to disable resolving variables from arguments. Could possibly be part of robot.api.deco.keyword. Should then also have an API to resolve arguments if needed.
Investigate could the whole "run keyword variant" system be removed from the public API or, preferably, altogether.
If we cannot remove "run keyword variant" system, we need to deprecate/remove the functionality to register keywords by using function/method. That system doesn't work in Python 3 currently and trying to fix it is not worth the effort.
changed the title [-]Clean-up semi public mechanism to register "run keyword variants" and disable variable resolving in arguments[/-][+]Clean-up semi-public API to register "run keyword variants" and to disable variable resolving in arguments[/+]on Dec 15, 2015
In our own code base we have numerous situations where we've created custom control structures. This feature's pretty important to keeping our logs clean looking and making complex behaviors simple to use. One example is a custom if statement that evaluates a javascript expression against the given page instead of a python expression:
def js_run_keyword_if(self, *args):
"""If the first value in *args* evaluates to true in javascript, execute's the second value as a keyword
*keyword*, with the following values up to the first instance of "ELSE" or "ELSE IF." If *js_test* returns
false and there is an "ELSE" or "ELSE IF" in *args*, it verifies the js_test.
@param args: A list of values in a RobotFramework style ELSE/IF format, where the conditionals are javascript booleans
@type args: ListType
@return: The result of executing one of the keywords, or nothing if none of the keywords is executed.
"""
(else_ifs, elses) = self._split_else_ifs(list(args))
for else_if in else_ifs:
assert len(else_if) > 1, "Each else if statement must have a condition and result, %s does not." % str(else_if)
conditional = else_if[0]
action = else_if[1:]
js = "return %s;" % conditional
conditional_result = self._current_browser().execute_script(js)
if conditional_result:
return robot.libraries.BuiltIn.BuiltIn().run_keyword(*action)
if elses:
return robot.libraries.BuiltIn.BuiltIn().run_keyword(*elses)
Then to make it so it only evaluated variables when it should is at the bottom of the file:
Without register_run_keyword it would be pretty hard to make this at all clean. It would either involve doing it wholly in RF, or writing a separate python keyword for each case I needed it in.
@fkberthold: Thanks for showing a use case where this functionality is used. It makes it explicit that this stuff is used also outside RF standard libraries and it cannot be outright removed. I hope we can make the API better in the future, though.
Keywords registered to Robot's current "run keyword register" have some special features explained below.
These keywords won't be stopped by timeouts. The reason is that we only want to forcefully stop the lowest level keywords if a timeout occurs and these "run keyword variants" often execute other keywords. The reason only the lowest level keywords should be stopped is that otherwise we may end up with corrupted output.xml. This functionality is valid also with custom keywords that run other keywords internally.
These keywords can optionally tell that only some of the arguments are actually resolved when the keyword is called. Resolving most importantly means replacing variables, but it also takes care of handling syntax like \n. This functionality has two usages.
If the keyword runs other keywords using BuiltIn.run_keyword, arguments passed to the lower level keyword will be resolved before it is called. If the higher level keyword is passing arguments it received further, they will be resolved twice which can have undesired results. Thus the higher level keyword should tell Robot to not resolve its arguments. It would make sense to expose this functionality to custom libraries that use BuiltIn.run_keyword, but I actually believe it would be even better to have a better API for keywords to execute other keywords. Instead of using BuiltIn.run_keyword, they should be able to use some dedicated API where arguments are passed as-is and won't be resolved again. Then arguments passed to the higher level keyword could generally be resolved and this functionality wouldn't need to be exposed publicly at all. I'll submit a separate issue about such a better keyword running API.
Not resolving some of the arguments makes it possible to use non-existing variables in arguments if they are not actually used. For example, it is possible to use Run Keyword If False Log ${non-existing-variable} without problems. This functionality was added ages ago (Arguments to keywords executed with 'Run Keyword' variants should be resolved later #67). It was useful back then, but now that we have native IF/ELSE support I don't think it has any real usage. At least I don't think there's need to expose this kind of functionality publicly.
If there is a conflict with a standard library keyword and a custom keyword, typically you get a warning telling that there's a conflict, that the custom keyword is used instead, and that you should use format like LibrName.Keyword Name to explicitly select which keyword to use to avoid the warning. If the custom keyword in question has been registered to be a "run keyword variant", it is used without a warning. The use case was to allow more powerful variants of Run Keyword If when we hadn't yet added ELSE and ELSE IF support to it. There's absolutely no real use case for this functionality anymore. The whole functionality should be removed and even if it isn't there's no need to expose it publicly.
As you can see, the only really relevant features that the "run keyword variants" have are avoiding timeouts that can corrupt output.xml and avoiding resolving arguments twice when keywords use BuiltIn.run_keyword. If we have a better API for executing keywords, the only useful feature is disabling timeouts.
If we'd only need to care about timeouts, it would simplify API design related to this feature a lot. Most likely it would be enough to add a new optional argument to the @keyword decorator that can be used for disabling timeouts. It could work, for example, like @keyword(disable_timeouts=True) or @keyword(timeout=None).
The latter approach to disable timeouts would make it possible to also support @keyword(timeout='1 minute'), but I'm not sure is that actually a good idea. Keywords implemented in Python should be able to take care of not hanging or otherwise ending in timely manner themselves. Robot forcefully killing keywords always has a risk that the system is left into unstable state.
Activity
RunKeywordRegister: Don't run tests that fail on Python 3.
[-]Clean-up semi public mechanism to register "run keyword variants" and disable variable resolving in arguments[/-][+]Clean-up semi-public API to register "run keyword variants" and to disable variable resolving in arguments[/+]pekkaklarck commentedon Dec 15, 2015
We decided to post-pone this to RF 3.1. Main reasons are:
Deprecated semi-public run keyword registering API. Fixes #2265. See …
fkberthold commentedon Mar 17, 2016
In our own code base we have numerous situations where we've created custom control structures. This feature's pretty important to keeping our logs clean looking and making complex behaviors simple to use. One example is a custom if statement that evaluates a javascript expression against the given page instead of a python expression:
Then to make it so it only evaluated variables when it should is at the bottom of the file:
Without
register_run_keywordit would be pretty hard to make this at all clean. It would either involve doing it wholly in RF, or writing a separate python keyword for each case I needed it in.pekkaklarck commentedon Mar 18, 2016
@fkberthold: Thanks for showing a use case where this functionality is used. It makes it explicit that this stuff is used also outside RF standard libraries and it cannot be outright removed. I hope we can make the API better in the future, though.
19 remaining items
pekkaklarck commentedon Feb 16, 2022
Keywords registered to Robot's current "run keyword register" have some special features explained below.
\n. This functionality has two usages.BuiltIn.run_keyword, arguments passed to the lower level keyword will be resolved before it is called. If the higher level keyword is passing arguments it received further, they will be resolved twice which can have undesired results. Thus the higher level keyword should tell Robot to not resolve its arguments. It would make sense to expose this functionality to custom libraries that useBuiltIn.run_keyword, but I actually believe it would be even better to have a better API for keywords to execute other keywords. Instead of usingBuiltIn.run_keyword, they should be able to use some dedicated API where arguments are passed as-is and won't be resolved again. Then arguments passed to the higher level keyword could generally be resolved and this functionality wouldn't need to be exposed publicly at all. I'll submit a separate issue about such a better keyword running API.Run Keyword If False Log ${non-existing-variable}without problems. This functionality was added ages ago (Arguments to keywords executed with 'Run Keyword' variants should be resolved later #67). It was useful back then, but now that we have nativeIF/ELSEsupport I don't think it has any real usage. At least I don't think there's need to expose this kind of functionality publicly.LibrName.Keyword Nameto explicitly select which keyword to use to avoid the warning. If the custom keyword in question has been registered to be a "run keyword variant", it is used without a warning. The use case was to allow more powerful variants ofRun Keyword Ifwhen we hadn't yet addedELSEandELSE IFsupport to it. There's absolutely no real use case for this functionality anymore. The whole functionality should be removed and even if it isn't there's no need to expose it publicly.As you can see, the only really relevant features that the "run keyword variants" have are avoiding timeouts that can corrupt output.xml and avoiding resolving arguments twice when keywords use
BuiltIn.run_keyword. If we have a better API for executing keywords, the only useful feature is disabling timeouts.If we'd only need to care about timeouts, it would simplify API design related to this feature a lot. Most likely it would be enough to add a new optional argument to the
@keyworddecorator that can be used for disabling timeouts. It could work, for example, like@keyword(disable_timeouts=True)or@keyword(timeout=None).The latter approach to disable timeouts would make it possible to also support
@keyword(timeout='1 minute'), but I'm not sure is that actually a good idea. Keywords implemented in Python should be able to take care of not hanging or otherwise ending in timely manner themselves. Robot forcefully killing keywords always has a risk that the system is left into unstable state.pekkaklarck commentedon Aug 25, 2023
No interest towards this lately. Moving to later.
kkotenko commentedon Nov 14, 2023
In light of release of v7.0 alpha 1: is this API still anticipated to change in v7.0?
pekkaklarck commentedon Oct 16, 2024
Yet another problem with the current system is that it disables the named-argument syntax as seen in #5235.
rpuncel commentedon Dec 18, 2025
Very interested in this. Is there something the community can do to help?