Skip to content

Language proposal: Keyword chaining or piping #5846

Closed as duplicate
Closed as duplicate

Description

@Lakitna

I was talking to a colleague that does not use Robot recently. Their OOP brain asked about a pattern that someone used in their tests, and if they could use a builder pattern instead. The builder pattern would work great in that particular situation, but Robot does not support it.

The situation that we talked about was related to building complex data objects. In this case, we needed ~10 highly specialized keywords after each other to build up a single data object. With that object, we can then test stuff. These highly specialized keywords use keywords of their own. It's a lot of code that is hard to understand in the best case scenario. But Robot forced us to make it harder to read than necessary in our opinion. I can't share the code itself because it's company code. Our discussion was about good ways of approaching this test requirement.

After this discussion, we briefly talked about what something like this could look like in Robot Framework. This issue is the outcome of that discussion.


I see 2 distinct ways of approaching this:

  1. Chaining
  2. Piping

1. Chaining

Inspired by Object Oriented Programming and the builder pattern.

I'm using -> based on a whim. This can, of course, use a different syntax.

In your test

What this could look like when you're writing a test.

GUI tests

Allows you to dedupe locators in a very readable (in my opinion) way.

Get Element    //button
->  Should have text    Hello world
->  Should have attribute    aria-enabled    true
->  Click

API tests

Allows you to define stuff in the 'correct' order. It's one of the main reasons I don't advise people to use Robot for pure API testing.

${request} =    POST    https://example.com
->  Add Header    Authorization    Bearer 923u49ujjidashuihdc8923yh98rhjd2
->  Add Header    Foo    Bar
->  Add Body    lorum=ipsum    dolor=sit

${response} =    ${request}    ->    Send

# Do something with the response

This gives us the reading order: Method -> URL -> Headers -> Body. Which feels much more natural to me compared to the current best Headers -> Body -> Method -> URL. The order still breaks a bit for complex bodies, where you'll have to create Body -> Method -> URL -> Headers.

In your resource file

How you can create these keywords in a resource file.

This approach does not work well in a resource file. It may have to be library-only. My best suggestion here is not great. It's essentially to give people a sanctioned way of using private keywords outside their defined file.

I would leave this part out entirely. Building chainables is an advanced thing, so forcing that to Python sounds like a fair tradeoff to me. Using chainables should be easy enough to use in .robot and .resource though.

Click to open partial code example
*** Keywords ***
Get Element
    [Arguments]    ${selector}
    VAR  @{chainable_keywords}    Should Have Text    Should Have Attribute    Click

    ${chainable_element} =    Create Chainable Object    context=${selector}    keywords=${chainable_keywords}
    RETURN   ${chainable_element}

Should Have Text
    [Tags]    robot:private
    [Arguments]    ${chainable_element}    ${text}
    # Truncated
    RETURN   ${chainable_element}


Should Have Attribute
    [Tags]    robot:private
    [Arguments]    ${chainable_element}    ${attribute}    ${value}
    # Truncated
    RETURN   ${chainable_element}


Click
    [Tags]    robot:private
    [Arguments]    ${chainable_element}
    # Truncated
    RETURN   ${chainable_element}

In your library

How you can create these keywords in a library.

This approach uses multiple classes due to the OOP influence. Let's use the GUI one above as an example. For the purposes of this example, I'll assume that Browser library fully supports this. I've used the explicit @libary and @keyword decorators for clarity.

Click to open partial code example
class Element:
  def __init__(self, element):
    self.el = element

  @keyword
  def should_have_text(self, expected_text):
    actual_text = somehow_get_element_text(self.el)
    should_be_equal(actual_text, expected_text)
    return self

  @keyword
  def should_have_attribute(self, attribute, expected_value):
    # truncated
    return self

  @keyword
  def click(self):
    # truncated
    return self

@library
class Browser:
  @keyword
  def get_element(self, locator):
    located_element = somehow_find_element_on_page(locator)
    return Element(located_element)

Couple of things of note:

  • The get_element keyword returns an Element object instance.
  • There is state in the Element instance. In this example, it's just self.el, but more can be added. This can be really useful. More useful sometimes than state in a library.
  • There are keywords on the Element class. This is the new part. These keywords can only be used from an Element instance. This is what the -> operator on the Robot side does on a technical level. It calls a keyword on a given object instance. These keywords don't exist in the global keyword store. This prevents name collisions, which keeps the keyword names short.
  • The keyword on the Element class all return self. This allows you to chain multiple of these keywords. It also allows you to return another class instance. For example, Request.send can return Response.

2. Piping

Inspired by Functional Programming and the pipeline pattern.

I'm using |> based some informal proposals to add this idea to Python. The syntax comes from functional programming languages. This can, of course, use a different syntax. It probably has to due to the history of | in robot.

In your test

What this could look like when you're writing a test.

GUI tests

Get Element    //button
|>  Get Text    ==    Hello world
|>  Get Attribute    aria-enabled    ==    true
|>  Click

Note: These are all existing Browser keywords.

API tests

Allows you to define stuff in the 'correct' order. It's one of the main reasons I don't advise people to use Robot for pure API testing.

${request} =    POST    https://example.com
|>  Add Header To Request   Authorization    Bearer 923u49ujjidashuihdc8923yh98rhjd2
|>  Add Header To Request    Foo    Bar
|>  Add Body To Request    lorum=ipsum    dolor=sit

${response} =    ${request}    |>    Send Request

# Do something with the response

This gives us the reading order: Method -> URL -> Headers -> Body. Which feels much more natural to me compared to the current best Headers -> Body -> Method -> URL.

In your resource file

How you can create these keywords in a resource file.

Click to open partial code example
*** Keywords ***
Get Element
    [Arguments]    ${selector}
    # Truncated
    RETURN    ${resolved_selector}

Get Text 
    [Arguments]    ${selector}    ${assertion_operator}    ${assertion_expected}
    # Truncated 
    RETURN    ${selector}

Get Attribute
    [Arguments]    ${selector}    ${assertion_operator}    ${assertion_expected}
    # Truncated 
    RETURN    ${selector}

Click
    [Arguments]    ${selector}
    # Truncated
    RETURN    ${selector}

Couple of things of note:

  • All keywords have ${selector} as their first argument.
  • All keywords return a selector

These 2 are very much related. In a pipeline, the returned value of a keyword is the first argument of the next.

In your library

How you can create these keywords in a library.

This approach uses no classes due to the functional programming influence. Let's use the GUI one above as an example. For the purposes of this example, I'll assume that Browser library fully supports this. I've used the explicit @libary and @keyword decorators for clarity.

Click to open partial code example
@keyword
def get_element(selector):
  located_element = somehow_find_element_on_page(locator)
  return located_element

@keyword
def get_text(selector, assertion_operator, assertion_expected):
  # truncated
  return selector

@keyword
def get_attribute(selector, assertion_operator, assertion_expected):
  # truncated
  return selector

@keyword
def click(selector):
  # truncated
  return selector

Couple of things of note:

  • All keywords have ${selector} as their first argument.
  • All keywords return a selector

These 2 are very much related. In a pipeline, the returned value of a keyword is the first argument of the next.


Please disagree with me

What do you think of this proposal? This would be a major language change and would take a lot of time and effort to implement. So I think it's important to have a good discussion on this first.

Both the chaining and piping approaches have their pros and cons. I am personally leaning towards the chaining approach because I think it's easier to reason about and does not suffer as much from keyword naming conflicts. On the other hand, last week I leaned more towards the piping approach because it feels like it belongs more in the existing language and way of thinking.

Relevant existing proposals

Please let me know if I missed any existing relevant proposals

Activity

  1. pekkaklarck commented on Nov 3, 2026

    @pekkaklarck
    Member

    Interesting ideas! The first one seems to be close to foo(arg).bar().zap() whereas the latter is basically foo(bar(zap())) with a different syntax. Then we also have the inline keyword call idea that's basically same as the latter proposal but with more traditinal syntax.

  2. Lakitna commented on Nov 4, 2026

    @Lakitna
    Author

    Indeed, they map very nicely to those syntaxes!

    The technical equivalence of foo(bar(zap())) and foo() |> bar() |> zap() is why I mentioned that doing this may partially replace the inline keyword call proposal. If we decide to go down this road, I think it would be good to revisit the usefulness of the inline call syntax. Inline still has uses that can't be covered by one of these approaches, but going in with open eyes is always better :)

  3. pekkaklarck commented on Nov 4, 2026

    @pekkaklarck
    Member

    I agree, it wouldn't really make sense to add both inline keyword call syntax and pipelining. The former has benefits that it could work also in the variables section, with imports, and so on. The chaining syntax would have its usages anyway.

    Overall I think it would have been better to submit the chaining and piping proposals separately. It would be easier then to discuss the proposals independently.

  4. Lakitna commented on Nov 4, 2026

    @Lakitna
    Author

    Alright, I'll turn these into 2 new issues so we can discuss them separately. I'll reference this one as the reason for having 2

  5. Lakitna commented on Nov 4, 2026

    @Lakitna
    Author
  6. pekkaklarck commented on Nov 10, 2026

    @pekkaklarck
    Member

    Thanks for submitting the separate issues. We probably should start discussion about these enhancements on Slack. Do you want to do that or should I do it? I think it makes sense to have a separate thread for chaining. Pipeline and inline keyword call syntax should probably be discussed in the same thread because they solve mostly the same problem and it's unlikely we'd ever implement both.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions