0

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

1 Answers1

0

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
Dan Constantinescu
  • 1,426
  • 1
  • 7
  • 11
  • 1
    yeah thank you. You provide me with a new solution to my testcase. Though I'm wondering is there a way to exit nested for loops without moving these loops in keywords. It's OK if you tell me there is none, I'm just curious. Thanks again. – Miller Qinnn Mar 08 '22 at 07:53
  • From what I know there is no other clean solution to break from multiple nested loops. You can have a look at this topic for python: https://stackoverflow.com/questions/189645/how-to-break-out-of-multiple-loops as some solutions from there can be ported to Robot Framework. From all of them the return / Return From Keyword seem to be the best. – Dan Constantinescu Mar 08 '22 at 10:02