Repository navigation
Description
It is a relatively common need for library keywords to be able to run other keywords. One good example is SeleniumLibrary, that makes it possible to register a keyword that will be executed if any of its own keywords fails. Sometimes, especially when doing task automation, it is also beneficial to log actions done by the keyword in the log file separately. For example,
def do_something():
do_first_thing()
do_second_thing()will in the log file be shown as a single keyword without any child keywords, but there are sometimes needs to show do_first_thing and do_second_thing as child keywords.
Nowadays keywords can execute other keywords by using BuiltIn.run_keyword. It works but has several problems:
-
Arguments are resolved twice. This means replacing variables and handling syntax like
\n. For example, if we have a keyword likedef example(arg): BuiltIn().run_keyword('Log To Console', arg)
and call it like
Example c:\\tempthe actually logged string has a tab character. The reason is that
\\tis first resolved to string\twhich is then on the second round resolved to a tab.Double resolving can be avoided be registering the keyword to be a "run keyword variant", but that API isn't really public and it's also pretty bad in general. We have Clean-up semi-public API to register "run keyword variants" and to disable variable resolving in arguments #2190 about enhancing it and making it public, but it would probably be better to avoid this double resolving altogether.
-
Related to above, the keyword using
BuiltIn.run_keywordcannot safely call it without escaping strings. For example, something likeBuiltIn.run_keyword('Log To Console', 'c:\\temp')won't work correctly and the argument should bec:\\\\tempinstead. -
BuiltIn.run_keywordonly accepts positional arguments. For example, if we had a keyword implemented likedef example(upper=False): ...we couldn't call it like
BuiltIn.run_keyword('Example', upper=True)but instead needed to useBuiltIn.run_keywod('Example', 'upper=True').
The above examples ought to make it clear that a better API is needed. I have some ideas how to API could look and will write separate comments about that a bit later.
Activity
pekkaklarck commentedon Mar 3, 2022
One possible way to implement this would be using our current
robot.running.Keyword. It's already used byBuiltIn.run_keywordand it needed to be enhanced to avoid the problems explained in the above description. This is how it can be used currently:This usage has several problems:
runinstead of passing them toKeyword.contextsomehow. Alternatively, and probably better,Keyword.runcould find it out itself somehow if not given.Keywordaccepts also many arguments not valid in this kind of usage such asassign.Basically the best we could do would be this:
This would be an enhancement to the current situation but still not great.
pekkaklarck commentedon Mar 3, 2022
A benefit of using
robot.running.Keywordthat was discussed above is that we already have it and it is part of the public API. A problem is that it actually represents a keyword call in test data like:Because
robot.running.Keywordrepresents a keyword call, it containsassignand itsargscontains whatever there is in the data. This causes most of the problems discussed earlier. The class should arguably also be calledKeywordCallor something like that instead ofKeyword. Due to it being a part of the public API (e.g. visitors use it) renaming it isn't practical.We'd actually need something that represents a keyword in a library that we could then call with whatever arguments we want. We have such model objects in
robot.runningas well, but unfortunately they are pretty bad and not something we want to expose publicly. They should be enhance also for other reasons, but that is likely quite a bit task and not something we can do in RF 5.1.pekkaklarck commentedon Mar 3, 2022
In addition to having model objects representing actual keywords, not keyword calls, under
robot.running, alsorobot.libdocpkgimplementing Libdoc has them. These model objects aren't a mess like those underrobot.runningbut they aren't part of the public API either. We might want to expose them more publicly so that e.g. IDE plugins could use them, but I'm not sure would they still work well in this particular usage.Libdoc is in general working with libraries that it imports, but in here we'd just need to directly get a keyword somehow. Libdoc doesn't have access to the execution context and doesn't know what keywords are available.
pekkaklarck commentedon Mar 3, 2022
Because we don't have any model objects that could directly be used for this purpose, it could be that the best solution is adding a new entry point to
robot.api. I guess something like this would be fine:The function could work so that you can either pass a name of the keyword to execute, like you can with
BuiltIn.run_keyword, or a callable to execute as a keyword. In the latter case the name to show in logs would naturally be got from the__name__of the callable. Arguments would be passed to the keyword as-is to avoid problemsBuiltIn.run_keywordhas.The usage would be like this:
An alternative API would be:
It would make the usage nicer
but make it impossible to add possible configuration options to
execute_keyworditself. We possibly would like to add things likedocandtagsto add to the log and there could also be other configuration needs later.pekkaklarck commentedon Mar 4, 2022
One thing to take into account with the possible new API is how to handle argument conversion. As this example demonstrates,
BuiltIn.run_keywordhandles conversion automatically. If we add an API to replace it, that API probably should handle conversion as well.KotlinIsland commentedon Mar 15, 2022
Desired functionality: perform "run keyword and continue on failure" and support continuable failures in python code
(psuedo code moment)
Continue Example Validate All Things On Page Log endpekkaklarck commentedon Mar 15, 2022
I don't think continue-on-failure functionality is that important with Python code. You can simply avoid raising exceptions, and catch exceptions caused by code you call, until the keyword ends. For example, instead of something like
you can use
This way you have a full control on the final error message. Another problem with us automatically trying to catch exceptions in Python code is that it would likely be rather complicated and even more likely slow down execution.
Being able to log multiple individual failure messages under a single keyword could be nice, though. That we could easily support by making it possible to raise an exception so that its message is actually a list of messages. In the simplest form it could just mean
AssertiongError(['First error', 'Second error']). We could log each error separately and then generate "Several failures occurred" error based on the errors automatically. This would be easy to implement and doesn't have that much to do with an API to run other keywords. It would need its own issue, though, so please submit one!KotlinIsland commentedon Mar 15, 2022
Sure, but 99% of the time I want exactly whats in my mockup. Also this leads to a massive amount of boilerplate and potential to make a small mistake that could hide real errors.
My example doesn't raise any exceptions, a hypothetical implementation of
robot.api.failcould just correctly logs, and appends the failures to a list, and when a keyword is finished that list of errors is handled and flushed.I don't like this, as it would lose the context of when the error occurred, my pseudo code retains the order, timestamp etc of the messages.
I have been asking for this feature for ages across a couple of issues, but sure, I'm happy to raise another. 😀
KotlinIsland commentedon Mar 15, 2022
#4252 (comment)
pekkaklarck commentedon Aug 25, 2023
No interest towards this lately. Moving to later.