Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exit from for loop in Robot Framework

I'm choosing a random value from a list to perform some actions over it like Run Keyword If, Exit For Loop If, click element, wait untill page contains and all. Now I'm not able to exit the for loop after the condition is being met but what's happening is that is starts from the beginning conditions that are set for value Batman.

Already tried :break & Exit For Loop keywords but they didn't work out for me

@{Hero}  Batman  Superman  Ironman

View All Details of Superhero
 ${value} =  Evaluate  random.choice($Hero)  random
    input text  ${SEARCH_BAR}  ${value}
    log to console  \nvalue: ${value}
    click element  ${SEARCH_BUTTON}

    Run Keyword If  '${value}' == 'Batman'  click element  ${P1}
    ...  ELSE IF  '${value}' == 'Superman'  click element  ${P2}
    ...  ELSE IF  '${value}' == 'Ironman'  click element  ${P3}
    ...  ELSE  log to console  condition didn't met

    :For  ${value}  IN  @{Hero}
        \  Log    ${value}
        \  Run Keyword If  '${value}' == 'Batman'  sleep  5s
        \  EXIT FOR LOOP IF  '${value}' == 'Batman'
        \  click element  //*[@href='#external']
        \  click element  ${BASIC_INFO}
        \  wait until page contains  Summary
        \  click element  ${RELATIONSHIP}
        @{expected_relationship_result}  create list  Catwoman  Robin
        list should contain value  @{expected_relationship_result}
        \  Log  condition didn't met

        \  Run Keyword If  '${value}' == 'Superman'  click element
        \  click element  //*[@href='#external']
        \  click element  ${BASIC_INFO}
        \  wait until page contains  Summary
        \  click element  ${RELATIONSHIP}
        @{expected_relationship_result}  create list  Wonderwomen  Lois Lane
        list should contain value  @{expected_relationship_result}  
        \  Log  condition didn't met

        \  Run Keyword If  '${value}' == 'Ironman'  sleep  5s  
        \  EXIT FOR LOOP IF  '${value}' == 'Ironman'
        \  click element  //*[@href='#external']  
        \  click element  ${BASIC_INFO}
        \  wait until page contains  Summary
        \  click element  ${RELATIONSHIP}
        @{expected_relationship_result}  create list  Wonderwomen  Lois Lane
        list should contain value  @{expected_relationship_result}
        \  Log  condition didn't met


I'm not able to figure out when the condition is already been met then why Exit For Loop If is not working.
like image 618
Gaurav Marothia Avatar asked Mar 04 '23 21:03

Gaurav Marothia


1 Answers

Solution 1:

FOR    ${value}    IN    @{Hero}
    Do your stuff
    Exit For Loop IF    "${value}" == "${Batman}"
    Do your stuff
END

Can you try above syntax. This is latest for loop syntax. Its working for me and should work for you too. Use SeleniumLibrary latest version.

Solution 2: Old Syntax(this will also work)

: FOR    ${value}    IN    @{Hero}
\    Do your stuff
\    Exit For Loop IF    "${value}" == "${Batman}"
\    Do your stuff
like image 194
Manish Kumar Avatar answered Mar 10 '23 09:03

Manish Kumar