19

I need to execute some keywords conditionally in robot framework, but I dont know how to do it, it does not work. I tried many options, but I guess I have the "IF-ELSE" statement completely wrong..

Choose Particular Filter ${FILTER} And Uncheck All Values
    ${bool}=   is filter opened   ${AVAILABLE FILTERS}   ${FILTER}
    ${uncheck_all_button}=    run keyword  if    "${bool}" == "True"   uncheck all in filter  ${AVAILABLE FILTERS}   ${FILTER}
    ...                       click element   ${uncheck_all_button}
    ...                       ELSE
    ...                       Set variable    ${particular_filter}:    find particular filter   ${AVAILABLE FILTERS}  ${FILTER}
    ...                       click element   ${particular_filter}
    ...                       Set variable    ${uncheck_all_button}:   uncheck all in filter  ${AVAILABLE FILTERS}   ${FILTER}
    ...                       click element   ${uncheck_all_button}

It fails with: Variable '${particular_filter}' not found. But in case I run it it should not even go to ELSE branch because ${bool} is True... My custom function is filter opened just checks whether filter is already opened - if so, returns True. My custom function uncheck all in filter just returns XPATH of "uncheck all" button. My custom function find particular filter returns XPATH of "filter dropdown" button. In this whole keyword I need to check whether the filter dropdown is already opened - if so, then I have to click directly on ${uncheck_all_button}, else if the filter dropdown is not opened yet, I need to first click on the filter itself ${particular_filter} and after that I can click on ${uncheck_all_button}

I also tried the "run keyword" line to have like this:

${uncheck_all_button}=    run keyword  if    "${bool}" == "True"    Set Variable    uncheck all in filter    ${AVAILABLE FILTERS}    ${FILTER}

or this:

run keyword  if    "${bool}" == "True"   ${uncheck_all_button}=    uncheck all in filter    ${AVAILABLE FILTERS}    ${FILTER}

I also tried it with ${bool} == "True" and ${bool} == True

But nothing really works, still the same error :(

Thank you so much for any help!

2
  • I would recommend you to use "Log To Console ${bool}" to see what value it holds and then go by adding a condition. Commented Jan 5, 2017 at 5:58
  • sure, it was the first thing I did - as I wrote, the value is True..so dont understand why it goes to else branch.. Commented Jan 5, 2017 at 8:30

5 Answers 5

15

Having IF/THEN/ELSE with multiple statements in each block does not work in Robot (or you would have to use "Run Keywords" I suppose, but that would become unreadable). So I would refactor your code this way:

Choose Particular Filter ${FILTER} And Uncheck All Values
    ${is_filter_opened}=   is filter opened   ${AVAILABLE FILTERS}   ${FILTER}
    run keyword  if    ${is_filter_opened}    actions_when_unchecked
    ...                ELSE  actions_when_checked

actions_when_unchecked
    uncheck all in filter  ${AVAILABLE FILTERS}   ${FILTER}
    click element   ${uncheck_all_button}

actions_when_checked    
    ${particular_filter}:    find particular filter   ${AVAILABLE FILTERS}  ${FILTER}
    click element   ${particular_filter}
    ${uncheck_all_button}:   uncheck all in filter  ${AVAILABLE FILTERS}   ${FILTER}
    click element   ${uncheck_all_button}   

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

11

Based on the below syntax, update your code n check it.

Syntax for IF-ELSE:

   Run Keyword If    '${Condition}'== 'True'    Run Keywords    <Keyword 1>     <Keyword 2>
   ...    ELSE    <Keyword 1>

Syntax for "Set Variable" based on condition:

 ${Status}=    Run Keyword If    '${Condition}'== 'True'    Set Variable    <Yes>    <No>

As per your code in IF part,

if "bool=true", it will execute only the custom keyword "uncheck all in filter" but not the "Click element" keyword. If you want both the keywords to be executed based on the condition, then use "Run Keywords" keyword as mentioned in IF-ELSE syntax.

Comments

6

From the version above 4, robot framework supporting assignment inside IF-Else refer: https://robocorp.com/docs/languages-and-frameworks/robot-framework/conditional-execution

IF    ${random} == ${NUMBER_TO_PASS_ON}
    ${var}=    set variable    10
ELSE IF    ${random} > ${NUMBER_TO_PASS_ON}
    ${var}=    set variable  ${random}
ELSE
    ${var}=    set variable  ${NUMBER_TO_PASS_ON}
END

2 Comments

This is awesome. 'Run keyword if' is annoying to me.
Definitely, since proper IF/ELSE blocks became supported, the Run Keyword If madness needs to stop :) Congrats on your 100 reputation mark ;)
1

Thank you so much, Laurent, your solution is right! I just had to do some small changes to make it working:

Choose Particular Filter ${FILTER} And Uncheck All Values
${is_filter_opened}=   is filter opened   ${AVAILABLE FILTERS}   ${FILTER}
run keyword if      ${is_filter_opened}    actions_when_unchecked ${FILTER}
...                ELSE  actions_when_checked ${FILTER}

actions_when_unchecked ${FILTER}
${uncheck_all_button}=  uncheck all in filter  ${AVAILABLE FILTERS}   ${FILTER}
click element   ${uncheck_all_button}

actions_when_checked ${FILTER}
${particular_filter}=    find particular filter   ${AVAILABLE FILTERS}  ${FILTER}
click element   ${particular_filter}
${uncheck_all_button}=   uncheck all in filter  ${AVAILABLE FILTERS}   ${FILTER}
click element   ${uncheck_all_button}

1 Comment

Welcome to SO! By the way, you can actually edit @Laurent's answer with the changes that you made.
0
${error_status} Set Variable    False       
log ${error_status}         
Run Keyword If  ${error_status} calltrue    ELSE    login_sucessful

calltrue and login_sucessful are keywords it can be any keyword of your choice. so in this case login_sucessful  keyword is executed. if error_status is set to true then calltrue function will get executed.

Please be careful about the indentation a single space will mess up your execution flow

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.