循环

Python/Java学习交流群:369606713


循环

在测试自动化中,多次重复相同的动作是非常普遍的需求。使用Robot Framework,测试库可以具有任何类型的循环结构,并且大多数时间都在使用循环。

Robot Framework也具有自己的for循环语法,例如在需要重复来自不同库的关键字时,该语法很有用。For循环可与测试用例和用户关键字一起使用。

RF框架循环的语法是:FOR item IN sequence,和Python里面的循环相似。

简单循环

*** Variables ***
@{items}    tom    kite    rose

*** Test Cases ***
case01
    FOR    ${item}    IN    tom    kite     rose
        Log    ${item}
    END
case02
    FOR    ${item}    IN  @{items}
        Log    ${item}
    END

嵌套循环

RF不直接支持嵌套的for循环,但是可以在for循环内使用用户关键字,来实现嵌套循环。

my_var.py

nested = [
    ['a', 'b', 'c'],
    ['d', 'e', 'f'],
    ]

嵌套循环

*** Settings ***
Variables    my_var.py

*** Keywords ***
loop1
    FOR    ${item}     IN     @{nested}
        loop2    @{item}
    END
loop2
    [Arguments]    @{items}
    FOR    ${item}     IN     @{items}
        Log    ${item}
    END
*** Test Cases ***
case01
    loop1

使用多个循环变量

*** Test Cases ***
case01
    FOR     ${id}    ${name}    ${age}    IN
    ...    1    tom    20
    ...    2    kite    30
    ...    3     rose    18
        Log       ${id},${name},${age}
    END

For-in-range 循环

*** Test Cases ***
case01
   FOR    ${index}    IN RANGE    10
      Log    ${index}
   END
case02
   FOR    ${index}    IN RANGE    1    10    2
      Log    ${index}
   END

循环带索引(IN ENUMERATE)

*** Test Cases ***
case01
    FOR    ${index}    ${item}    IN ENUMERATE
    ...    tom    kite    rose
        Log    ${index},${item}
    END

退出循环

*** Test Cases ***
case01
    FOR    ${index}     IN RANGE    10
        Run Keyword If    ${index} == ${5}  Exit For Loop    
        Log    ${index}
    END

继续循环

*** Test Cases ***
case01
    FOR    ${index}     IN RANGE    10
        Run Keyword If    ${index}%2 == ${0}  Continue For Loop    
        Log    ${index}
    END