IF ELSE in robot framework with variables assignment

无人久伴 提交于 2019-12-03 05:31:34

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.

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.

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}
MD5
${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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!