Search code examples
robotframeworknested-for-loop

How to exit a nested for loop in Robot Framework 4.0 in ride 1.7.4.2


I wanna exit all nested for loops when ${port} == 3,however whatever keywords I use, such as 'Exit for loop' or 'Exit for loop if ${port} == 3' ,it will still continue the whole nested for loop. Meanwhile Robot Framework ride tells me that 'break' is a reserved keyword and can't be used. My code is below.

*** Test Cases ***
demo_01
    ${port}    Set Variable    0
FOR    ${i}    IN RANGE    2
FOR    ${j}    IN RANGE    2
FOR    ${k}    IN RANGE    2
    ${port}    Evaluate    ${port} + 1
    Log    ${port}
    IF    ${port} == 3
    Exit FOR Loop
END
END
END
END

And the result is here enter image description here

It just exited the inner for loop. :(

So I wanna know how to exit a nested for loop in Robot Framework Ride. Thank you all. BTW here is my pip list below

robotframework                  4.1.2
robotframework-databaselibrary  1.2.4
robotframework-pythonlibcore    3.0.0
robotframework-ride             1.7.4.2
robotframework-selenium2library 3.0.0
robotframework-seleniumlibrary  3.0.0
robotframework-sshlibrary       3.8.0

And my python version is

C:\WINDOWS\system32>python -V
Python 3.7.9

Thanks


Solution

  • Easiest way to achieve this is to move the enclosing FOR loops into a keyword and then return from it whenever a condition is met. In your case it will look like this:

    *** Keywords ***
    Log ports
        [Return]    Return when port 3 is reached
    
        ${port}    Set Variable    0
        FOR    ${i}    IN RANGE    2
            FOR    ${j}    IN RANGE    2
                FOR    ${k}    IN RANGE    2
                    ${port}    Evaluate    ${port} + 1
                    Log    ${port}
                    IF    ${port} == 3
                        Return From Keyword
                    END
                END
            END
        END
    

    Then in the test you just call the above keyword:

    *** Test Cases ***
    demo_01
        Log ports