4

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?

1 Answer 1

9

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})
Sign up to request clarification or add additional context in comments.

1 Comment

Second option is much more readable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.