如何在 Robot Framework 中编写循环

How to write a loop while in Robot Framework

我做了第一个简单的测试用例,但遇到了一个问题。

是否可以在 Robot Framework 中编写一个循环?

我想从修改变量的地址和地址中取值"i"。我想一直执行到这样的地址存在,因为它是table.

中的一行
${f1}       A
${f_temp}   B

While   ${f1} != ${f_temp}
or
While element xpath=//${i} is visible


\  ${F_temp}                Get Text     xpath=//${i}
\  ${i}                     ${i}+1
\  Run Keyword And Continue On Failure   Should be equal  ${f_temp}  ${f1}

有什么想法吗?

Robot Framework 没有 while 循环。您必须使用 FOR 循环和“exit for loop if”关键字退出。它会在有限的时间内 运行,但如果你 select 范围内的数字足够大,它就足够接近实际目的了。

*** Test Cases ***
For Test
    FOR    ${i}    IN RANGE    999999
           Exit For Loop If    ${i} == 9
           Log    ${i}
    END
    Log    Exited

您可能正在寻找 Wait Until Keyword Succeeds 关键字,它使您能够对 while 循环进行类似的构造。它比 FOR 有条件退出的循环更具可读性。

然后您使用自定义关键字,当您需要结束 "loop" 时它会失败。

正如上面的回答所说,Robot 不支持原生的 WHILE 循环。 但如果你坚持,这个可能会有所帮助。 https://github.com/robotframework/robotframework/issues/3235

这是 Robot Framework 中的其他类型的 FOR 循环,我在自己的笔记中有这个,它非常有用。

FOR Loop with Upper Bounds Range
    [Documentation]  This gives us a 0 based range
    FOR  ${Index}  IN RANGE  5
      Do Something  ${Index}
      ${RANDOM_STRING} =  Generate Random String  ${Index}
      Log  ${RANDOM_STRING}
    END

FOR Loop with Start and Finish Range
    [Documentation]  No longer a 0 based range because I provided start
    FOR  ${Index}  IN RANGE  1  4
      Do Something  ${Index}
      ${RANDOM_STRING} =  Generate Random String  ${Index}
      Log  ${RANDOM_STRING}
    END

FOR Loop with Start, Finish, and Step Range
    [Documentation]  The counter will jump by 2 each time ("step" value = 2)
    FOR  ${Index}  IN RANGE  1  10  2
       Do Something  ${Index}
       ${RANDOM_STRING} =  Generate Random String  ${Index}
       Log  ${RANDOM_STRING}
    END


#index for elements in for
${index} =    Set Variable    0
    FOR    ${col}    IN    @{cols}
       ${colum}    Format String    css:div[class='v-widget v-has-caption v-caption-on-top'] table[aria-rowcount='{0}'] tbody tr:nth-of-type({1}) td:nth-of-type(10)    ${r_count}    ${col}
        Click element    ${colum}
        press Keys    none    ${net_config.broadcast}
        Press Keys    none    TAB
        Press Keys    none    ${net_config.${index}}
        ${index}=    Evaluate    ${index} + 1
    END
#-------

FOR Loop with List
    @{ITEMS} =  Create List  Item 1  Item 2  Item 3

    FOR  ${MyItem}  IN  @{ITEMS}
       Log  ${MyItem}
    END

Exit a FOR Loop
    @{ITEMS} =  Create List  Item 1  Item 2  Item 3  Item 4

    FOR  ${MyItem}  IN  @{ITEMS}
       Log  ${MyItem}
       Run Keyword If  "${MyItem}" == "Item 3"  Exit For Loop
       Log  Didn't exit yet
    END

    Log  Now we're out of the loop