Selecting keyword via variable

Hello all,
I am unsuccessfully trying to achieve the following (simplified):

Calculate ${operation} of ${a} and ${b}

Calculate sum of ${a} and ${b}
    ${result} =    Evaluate ${a} + ${b}

Calculate product of ${a} and ${b}
    ${result} =    Evaluate ${a} * ${b}

So I would like to call a keyword based on ${operation} type, since the selection will vary depending on test and be dynamically set.

When I try to run the test, I get:

No keyword with name ‘Calculate ${operation} of ${a} and ${b}’ found.

Is it possible to achieve what I am trying to do?

Thank you and best regards,
JC

That’s possible by embedding arguments into the Keyword name.
See Robot Framework User Guide

*** Test Cases ***
Embedded arguments
    Select cat from list
    Select dog from list

*** Keywords ***
Select ${animal} from list
    Open Page    Pet Selection
    Select Item From List    animal_list    ${animal}

Thank you for your reply @Many .
I have tried using embedded arguments to achieve this result. Do you see any mistake in the example I posted?
I can’t figure what I am doing wrong.

Edit:
I think my example is opposite to yours.
In my case, keyword names are defined with static text and called with dynamic variable.
In your case, keyword has an embedded argument and is called with static string in the name that gets resolved to argument, if I understood correctly.

Ah ok, you want to use a Variable in the Keyword call - but not as an argument but as a way to select a keyword with a specific name.

One way of doing that is using the Built In keyword Run Keyword
https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Run%20Keyword

I think there might also be a different solution - but Run Keyword is one I have used in other projects

2 Likes

Thank you @Many .

Using Run Keyword worked… only thing I had to modify is pass ${a} and ${b} as [Arguments]. Keeping them embedded in the keyword didn’t work.

2 Likes

I have the feeling it should also work with the embedded arguments, but I cannot find an example right now…

Or simply add the “missing” keyword:

Calculate ${operation} of ${a} and ${b}
IF “${operation}” == “sum”
Calculate sum of ${a} and ${b}
ELSE IF “${operation}” == “product”
Calculate product of ${a} and ${b}
END

RF evaluate the keywords before running the tests.
So RF can not link “Calculate ${operation} of ${a} and ${b}” to “Calculate sum of ${a} and ${b}”

The other solution will be to define only “Calculate ${operation}of ${a} and ${b}” and use the IF statement to perform directly the required actions according to ${operation}.

Calculate ${operation} of ${a} and ${b}
IF “${operation}” == “sum”
${result} = Evaluate ${a} + ${b}
ELSE IF “${operation}” == “product”
${result} = Evaluate ${a} * ${b}[Arguments] ${col1} ${col2}
END
[Return] ${result}

Thank you for the explanation and suggestion @orenault

RF evaluate the keywords before running the tests.
So RF can not link “Calculate ${operation} of ${a} and ${b}” to “Calculate sum of ${a} and ${b}”

This is the part I was somehow not able to figure out on my own. Now I understand the behaviour and of course it makes sense.