Argument Typing in User Keywords

Hello Everyone!

is it possible to explicitly set a type to my arguments in User Keywords? I know its possible to do in libraries but I’d like to avoid that.

So far I’ve checked the official docs and tried googling. It seems its not possible do to so, but i just want to be sure and ask :slight_smile:

Hi Volkan,

Start with Using variables

the normal scalar variable syntax ${var}, how to use variables in list and dictionary contexts like @{var} and &{var}, respectively, and how to use environment variables like %{var}

By default if you declare a scalar variable it’s a string, e.g.:

    VAR    ${stringscalar}=    3

if you need a numeric value you can declare them like this:

    VAR    ${numericscalar}=    ${3}

For other variable types you may need to use Evaluate to get python to return the type you need.

If your still let us know what type of variable you need to create and we can try to help,

Dave.

Thanks for your answer!

Sadly this is not what I’m looking for. For Example the browserlib has the library keyword click:

def click(self, selector: str, button: MouseButton = MouseButton.left):

button is only allowed to be of the enum type MouseButton. I’d like to do that with a user keyword and without going through the hassle of coding it in python.

Hi Volkan,

Ah I see what you mean, unfortunately I don’t know the answer, hopefully someone else can help :crossed_fingers:

However I’m also not sure if you need to do that? for example when I look at the documentation for Click, I see in the examples that you just pass the string left or right so perhaps try something like this:

*** Keywords ***
My Click
    [Arguments]    ${selector}    ${button}=left
    Click    ${selector}    ${button}

Dave.

I believe that @pekkaklarck mentioned in this video: RoboCon 2024 - What’s new in Robot Framework 7.0 and why you should update!, that literals can be used to enforce certain values in keywords implemented in Python, but that it is not yet possible for keyword implemented in RobotFramework.
Maybe it will come in a future release? If no enhancement request is already open on the topic we should definitly open one!

1 Like