一. 简介
前面简单学习了一下,robotframework中的 for循环语句,文章如下:
Robot Framework的 for循环语句-CSDN博客
本文继续学习 有关 for循环的其他操作,例如跳出 for循环,或者退出某一次循环等操作。
二. Robot Framework的 退出 for循环
一般打印某些数据时,会遍历完所有的数据,但是在特殊情况下,FOR循环在使用时,需要提前终止并跳出循环。
1. 跳出 FOR循环
可以使用 Exit For Loop 语句可以跳出 FOR循环。这个关键字需要与条件判断语句 Run Keyword IF 一起使用。举例说明:
*** Test Cases ***
Exit For Loop
    [Documentation]  退出循环
    @{num_list}  Create List  1  2  3
    FOR  ${num}  IN  @{num_list}
        Run Keyword IF  ${num}==3  Exit For Loop
        Log  ${num}
    END结果如下:

2. 根据条件退出循环
可以使用 Exit For Loop If 语句,根据条件退出循环。语句格式如下:
Exit For Loop If  condition    # condition表示退出循环的条件下面来举例说明:
*** Test Cases ***
Exit For Loop based on condition
    [Documentation]  根据条件退出循环
    @{num_list}  Create List  1  2  3
    FOR  ${num}  IN  @{num_list}
    Exit For Loop If  ${num}==3
    Log  ${num}
    END3. 使用 Return From Keyword 关键字跳出循环
如果你在一个用户自定义关键字(即你自己编写的测试步骤集合)中使用 FOR 循环,你可以使用 Return From Keyword 来提前结束整个关键字的执行,这也会导致循环的提前结束。
下面来举例说明:
*** Test Cases ***
Exit ForLoop based on condition
    [Documentation]  根据条件退出循环
    Example With Return
*** Keywords ***
Example With Return
    FOR  ${number}  IN RANGE  1  10
        Log  Current number: ${number}
        Run Keyword If  ${number} == 5  Return From Keyword
    END结果如下:

关于常用的退出 FOR循环的语句,先学习到这里。



















