IF ELSE in robot framework with variables assignment

neliCZka picture neliCZka · Jan 4, 2017 · Viewed 69.5k times · Source

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!

Answer

Laurent Bristiel picture Laurent Bristiel · Jan 5, 2017

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.