如何迭代For循环,直到满足特定条件?

时间:2020-03-04 10:11:59

标签: for-loop robotframework

我需要迭代循环,直到在Robot Framework中满足特定条件为止。

${counter}=  Set Variable  1

    :FOR    ${item}    IN RANGE    ${counter}
    \    Check condition
    \    ${counter} = ${counter} + 1

是否可以在此处增加$ {counter}变量值?

enter image description here

2 个答案:

答案 0 :(得分:2)

是的

${counter}=    Set Variable     1
FOR    ${item}    IN RANGE    1     20
    ${counter}=     Evaluate     ${counter} + 1
    Log To Console    ${counter}
    Exit For Loop If     ${counter} == 10
END

然后可以使用Exit For LoopExit For Loop If关键字退出FOR循环。 Keywords documentation

在评论后编辑。

答案 1 :(得分:0)

您正在询问一个while循环。机器人没有while循环。根本不支持它,并且机器人可能至少要到2021年才支持它。唯一的循环结构是{​​{3}}。

您似乎不愿意将限制设置为20,但是必须对迭代次数有实际的限制,无论是1,000、10,000还是100万或更多。只需使用一个上限非常大的FOR循环,就所有目的而言,您都创建了while循环。

FOR    ${item}    IN RANGE    1000000
    Exit FOR loop if  <some condition>
    ${counter}=  evaluate  $counter + 1
END

虽然看起来不像While <some condition>那样漂亮,但假设您的条件在一百万次迭代之前的某个时刻变为真,则最终结果将是相同的。