3

I am trying to assign a variable - body ,depending on the status of another variable phonenumber_id. If the phonenumber_id is NULL, body gets assigned False.
But it doesnt seem to be working. It works only if he phonenumber_id is not NULL.

${body}=    Run keyword if  '${phonenumber_id}'!='NULL'     Set variable    TRUE
...         Else            Set Variable    FALSE   

Not sure what i am doing wrong.

2
  • What does "not working" mean? Do you get an error, or is it always setting it to the same value no matter what ${phonenumber_id} is? Also, are you wanting to check against the string "NULL", or against the python value None? Commented Apr 2, 2015 at 23:19
  • When the first condition was true, it was assigning ${body} with this Set variable TRUE ... Else Set Variable FALSE Commented Apr 3, 2015 at 22:52

2 Answers 2

14

The keyword Set Variable If will set a variable based on a given condition

${body}=    Set Variable If    '${phonenumer_id} != 'NULL'
...    ${True}    ${False}
Sign up to request clarification or add additional context in comments.

1 Comment

But if edit your answer, I remove downvote with joy.
1

You got it almost right - just mistyped the ELSE - it must be in capital letters, to be considered a part of the Run Keyword If. So in your particular case, it should've been:

${body}=    Run keyword if  '${phonenumber_id}'!='NULL'     Set variable    TRUE
...         ELSE            Set Variable    FALSE

For the simple case of just setting a new constant value though, @ILostMySpoon answer is good enough - and more "readable".


In general, for someone stumbling on this post, the Run Keyword If combined with ELSE Set Variable is a very powerful construct to set/change a variable - based on the fact that it not only runs a keyword(s) conditionally, but also propagates its return values back to the stack.

Consider this example:

${var}=    Run Keyword If    ${bool condition}   Do Some Action Returning A Value    
           ...    ELSE    Set Variable    ${var}

In it {var} will be set to the return value of Do Some Action Returning A Value only if ${bool condition} evaluates to true, and will keep its old value otherwise.

Another artifical but less abstract example:

    ${value}=    Run Keyword If    ${should be int}     Convert To Integer   ${value}
                 ...    ELSE IF    ${should be float}   Convert To Number    ${value}
                 ...    ELSE    Set Variable    ${value}

Comments

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.