【问题标题】:How to write a loop while in Robot Framework如何在 Robot Framework 中编写循环
【发布时间】:2016-07-19 14:57:22
【问题描述】:

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

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

我想从地址和修改后的变量“i”的地址中检索值。我想执行直到存在这样的地址,因为它是表中的一行。

${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}

有什么想法吗?

【问题讨论】:

    标签: automated-tests robotframework


    【解决方案1】:

    Robot Framework 没有 while 循环。您必须使用 FOR 循环和“exit for loop if”关键字退出。它将在有限时间内运行,但如果您在范围内选择足够大的数字,它对于实际用途来说已经足够接近了。

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

    【讨论】:

    • 谢谢回答。我有第二个问题。是否可以编写像 ${i} 2 和 ${link} xpath=//div/div[${i}]/table... 之类的代码?当它位于 xpath 的中间时,我无法将变量分配给另一个变量。 RF 向我返回一个错误,例如 No keyword with name xpath=//div/div[${i}]/table...
    • 我认为您需要使用“设置变量”关键字。试试这个:"${link}= Set Variable xpath=//div/div[${i}]/table" 如果这不起作用,请提出一个新问题。
    • 不,我不是。我和 Pekka 甚至同时在同一个房间。
    • 请注意,for 循环有一种新语法! robotframework.org/robotframework/latest/…
    • 感谢您对 kuga 的评论,我已将代码更改为新语法。
    【解决方案2】:

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

    然后您使用自定义关键字,当您需要结束“循环”时该关键字会失败。

    【讨论】:

    • 这会在等待关键字成功时在日志中生成大量不需要的错误,尤其是当循环多次迭代时,即使最终结果通过了。如果您检查错误的变量很长,则日志文件会很大。
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      这是 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
      

      【讨论】:

        猜你喜欢
        • 2021-06-17
        • 2023-01-28
        • 2019-10-21
        • 1970-01-01
        • 1970-01-01
        • 2021-06-16
        • 1970-01-01
        • 2015-09-12
        • 2018-06-21
        相关资源
        最近更新 更多