How to Call a Keyword with Named Arguments in Python

Hi all,

I’m currently calling Robot Framework keywords from Python using:

BuiltIn().run_keyword(kw_name, *args)

This works well and logs the keyword in the log file (with its own section).


However, I’m running into a problem with keywords that have many optional arguments.

@keyword("Example KW")
def example_kw(self, arg_one=0, arg_two=1, arg_three=1, arg_four=50, arg_five=2000):
    ...

Currently, if I only want to change one or two arguments, I still have to pass all argument positionally:

# what I do right now
BuiltIn().run_keyword("example_kw",  100, 1, 1, 50, 2000)

# what I want to do 
BuiltIn().run_keyword("example_kw",  arg_one=100)

Is there a way to pass named arguments to run_keyword so I don’t need to pass every positional arg, especially when most are using default values?

I’m not strictly tied to BuiltIn().run_keyword(kw_name, *args), if there’s a better way to:

  • The keyword shows up in the log file (i.e., as a proper keyword block)
  • Call a keyword with named argument

I’m happy to switch to another method

have you tried this?:

BuiltIn().run_keyword("example_kw",  [arg_one=100])

If I remember correctly this is what python wants.

Dave.

Sorry for the late response!

Am I missing something here? It seems like the syntax isn’t working as expected.

from robot.libraries.BuiltIn import BuiltIn

@keyword("example_kw")
    def example_kw(self, arg_one, arg_two="2", arg_three="3"):
        BuiltIn().run_keyword("Log To Console ",  arg_one)
        BuiltIn().run_keyword("Log To Console ",  arg_two)
        BuiltIn().run_keyword("Log To Console ",  arg_three)

BuiltIn().run_keyword("example_kw", "100", ["arg_three=200"])

Edit:
The basic syntax actually works!

But I remember this didn’t work in earlier versions
I just upgraded from Robot Framework 6.1 to 7.1.1, so that might be the reason.

Sorry for the silly question, LOL :sweat_smile:

BuiltIn().run_keyword("example_kw",  "100","arg_three=200")

After a few rounds of trial and error, I found that the following code works as intended:

@keyword("example_kw")
def example_kw(self, arg_one, arg_two="2", arg_three="3"):
    BuiltIn().run_keyword("Log To Console", arg_one)
    BuiltIn().run_keyword("Log To Console", arg_two)
    BuiltIn().run_keyword("Log To Console", arg_three)

# Correct invocation with named argument
built = BuiltIn()
built.call_method(built, "run_keyword", "example_kw", "100", "arg_three=200")

1 Like