Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Robot Framework: assign variable with if-else statement

I use latest Robot Framework. I need to assign a value to my variable depending on value of an argument. That's how it would be in JavaScript:

ITEM_SELECTOR = RECENT_ITEM_SELECTOR + (
    position === 'last' ? ':last-child' : ':nth-child' + '(' + position + ')'
)

This is how I try to write it in Robot Framework:

${ITEM_SELECTOR} =    Run Keyword If    ${position} == 'last'    ${RECENT_ITEM_SELECTOR}:last-child
...    ELSE    ${RECENT_ITEM_SELECTOR}:nth-child(${position})

but this way ${RECENT_ITEM_SELECTOR}:nth-child(${position}) is considered a keyword, not assigned to ITEM_SELECTOR.

Then I try to preprend it with No Operation, but then my return value is considered its argument and I get Keyword 'BuiltIn.No Operation' expected 0 arguments, got 1.

How can I write it?

like image 664
Mikhail Batcer Avatar asked Feb 07 '23 16:02

Mikhail Batcer


1 Answers

Since you are calling run keyword if, you have to give it a keyword to run. You can use set variable to make your code work:

${ITEM_SELECTOR} =    Run Keyword If    ${position} == 'last'
...  Set variable    ${RECENT_ITEM_SELECTOR}:last-child
...  ELSE    
...  Set variable    ${RECENT_ITEM_SELECTOR}:nth-child(${position})    

However, you can also use set variable if for a slightly more compact and readable solution:

${ITEM_SELECTOR} =    Set variable if    ${position} == 'last'
...  ${RECENT_ITEM_SELECTOR}:last-child
...  ${RECENT_ITEM_SELECTOR}:nth-child(${position})
like image 132
Bryan Oakley Avatar answered Apr 02 '23 01:04

Bryan Oakley