Skip to content

Clean-up semi-public API to register "run keyword variants" and to disable variable resolving in arguments #2190

Description

@pekkaklarck

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:

  1. 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.
  2. Investigate could the whole "run keyword variant" system be removed from the public API or, preferably, altogether.
  3. 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.

Activity

  1. added this to the 3.0 milestone on Nov 2, 2015
  2. 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
  3. removed this from the 3.0 milestone on Dec 15, 2015
  4. pekkaklarck commented on Dec 15, 2015

    @pekkaklarck
    MemberAuthor

    We decided to post-pone this to RF 3.1. Main reasons are:

  5. fkberthold commented on Mar 17, 2016

    @fkberthold

    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:

    robot.libraries.BuiltIn.register_run_keyword('WsSelenium', WsSelenium.js_run_keyword_if)
    

    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.

  6. pekkaklarck commented on Mar 18, 2016

    @pekkaklarck
    MemberAuthor

    @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.

  7. 19 remaining items

  8. pekkaklarck commented on Feb 16, 2022

    @pekkaklarck
    MemberAuthor

    Keywords registered to Robot's current "run keyword register" have some special features explained below.

    1. 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.
    2. 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.
      1. 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.
      2. 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.
    3. 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.

  9. modified the milestones: v5.1, v5.2 on Jun 20, 2022
  10. modified the milestones: v6.1, on Jan 3, 2023
  11. modified the milestones: , v7.0 on Jun 2, 2023
  12. pekkaklarck commented on Aug 25, 2023

    @pekkaklarck
    MemberAuthor

    No interest towards this lately. Moving to later.

  13. modified the milestones: v7.0, v8.0 on Aug 25, 2023
  14. kkotenko commented on Nov 14, 2023

    @kkotenko
    Contributor

    In light of release of v7.0 alpha 1: is this API still anticipated to change in v7.0?

  15. pekkaklarck commented on Oct 16, 2024

    @pekkaklarck
    MemberAuthor

    Yet another problem with the current system is that it disables the named-argument syntax as seen in #5235.

  16. rpuncel commented on Dec 18, 2025

    @rpuncel

    Very interested in this. Is there something the community can do to help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions