Context managers in robot framework

Hi!

I need to create some kind of RAII environment for my keywords…:

The idea is that in some situations, I want to execute a set of keywords with an action being taken before, and an action taken after.

I do so currently, by creating keywords which call other keywords:

Delegate run
...  Nested keyword   Some arguments

The “Delegate Run” keyword basically does some stuff before / after calling “Run keyword and ignore errors”

This works great, but has 2 downsides:

  1. nested keyword must be a single keyword. I cannot call multiple keywords within the same environment, so if I want to call multiple KWs I need to wrap them under a ‘meta-keyword’
  2. In terms of auto-completion, it kind of sucks, since the nested keyword will be considered as a simple argument to the “Delegate Run” keyword, and I won’t get autocompletion in my fav’ IDE for the nested KW arguments…

I doubt that I’m the only one who faced this issue, and maybe I don’t hard the proper words in mind to properly google it… but I haven’t found anyone online proposing a solid solution to my problem.

Is there one somewhere already implemented in robotframework? and if now, how would you go about implementing such feature?

Wouldn’t making a new keyword that has other keywords inside it solve this case 1 ? Like this:

*** keywords ***
This Has Multiple
  [Arguments]   ${foo}  ${bar}
  Nested Keyword    ${foo}
  Another Nested One   ${bar}

*** Test Cases ***
Example TC
  Delegate Run
  ... This Has Multiple    Hello    World

Then, the only thing that might not “auto complete” is the This Has Multiple keyword but everything inside it would ?

1 Like

What might be a solution is the [Setup] and [Teardown] of a keyword, where you call your generic “context keywords”: Robot Framework User Guide