robotframework-循環

在自動化測試種,重複執行相同的動做屢次是一個至關常見的操做,robotframework 有本身的循環結構python

basic loop syntax:編程

   FOR  item  IN sequencemarkdown

     do something框架

  END編程語言

simple for loop

*** Test Cases ***
Example
    FOR    ${animal}    IN    cat    dog
        Log    ${animal}
        Log    2nd keyword
    END
    Log    Outside loop

Second Example
    FOR    ${var}    IN    one    two    ${3}    four    ${five}
    ...    kuusi    7    eight    nine    ${last}
        Log    ${var}
    END
Three Example
    FOR    ${element}    IN    @{ELEMENTS}
        Start Element    ${element}
    END
複製代碼

在第一個例子中,循環執行了兩次,依次賦值爲 cat, dog, 循環體內有有兩個log 語句ide

在第二個例子中,sequence被分割成了兩行,循環總共執行10次函數

在第三個例子中,sequence是個列表,依次操做列表中的元素oop

Old for loop syntax

*** Test Cases ***
Example
    :FOR    ${animal}    IN    cat    dog
    \    Log    ${animal}
    \    Log    2nd keyword
    Log    Outside loop
複製代碼

舊的語法在Robot Framework 3.2中已經棄用,而對它的支持在Robot Framework 4.0中被徹底移除測試

Nested for loops(嵌套循環)

從Robot Framework 4.0開始,只需在循環中添加另外一個循環,就可使用嵌套的for循環ui

*** Keywords ***
Handle Table
    [Arguments]    @{table}
    FOR    ${row}    IN    @{table}
        FOR    ${cell}    IN    @{row}
            Handle Cell    ${cell}
        END
    END
複製代碼

對於表格的循環,一行一行的去查找數據

*** Test Cases ***
Example
    FOR    ${root}    IN    r1    r2
        FOR    ${child}    IN    c1   c2    c3
            FOR    ${grandchild}    IN    g1    g2
                Log Many    ${root}    ${child}    ${grandchild}
            END
        END
        FOR    ${sibling}    IN    s1    s2    s3
                Log Many    ${root}    ${sibling}
        END
    END
複製代碼

在早期的Robot Framework版本中,不直接支持循環嵌套,但能夠在循環中使用一個user關鍵字,而後在那裏建立另外一個循環

Using several loop variables

也可使用多個循環變量。語法與普通for循環相同,但全部循環變量都列在for和in之間的單元格中。能夠有任意數量的循環變量,可是值的數量必須能均勻地除以變量的數量。

若是有不少值須要迭代,將它們組織在循環變量下面一般是很方便的,就像下面例子的第一個循環:

*** Test Cases ***
Three loop variables
    FOR    ${index}    ${english}    ${finnish}    IN
    ...     1           cat           kissa
    ...     2           dog           koira
    ...     3           horse         hevonen
        Add to dictionary    ${english}    ${finnish}    ${index}
    END
    FOR    ${name}    ${id}    IN    @{EMPLOYERS}
        Create    ${name}    ${id}
    END
複製代碼

For-in-range loop

前面的for循環老是在一個序列上迭代,這也是最多見的用例。有時,使用for循環執行必定次數仍然很方便,而Robot Framework爲此也有一個特殊的for  item  IN RANGE  limit 語法。此語法源自python range()函數。

與其餘for循環相似,for-in-range循環以for開始,循環變量位於下一個單元格中。在這種格式中,只能有一個循環變量,它包含當前循環索引。下一個單元格必須包含IN RANGE(區分大小寫)和隨後的單元格循環限制。

在最簡單的狀況下,只指定循環的上限。在本例中,循環索引從0開始,增長1直到限制,但不包括限制。也能夠給出開始和結束的限制。而後索引從起始限制開始,但增加與簡單狀況相似。最後,還能夠指定步長值來指定要使用的增量。若是步長是負的,它被用做減量。

可使用範圍限制的簡單算術,如加減。當使用變量指定限制時,這尤爲有用。Start、end和step一般以整數形式給出,但也可使用浮點值。

*** Test Cases ***
Only upper limit
    [Documentation]    Loops over values from 0 to 9
    FOR    ${index}    IN RANGE    10
        Log    ${index}
    END

Start and end
    [Documentation]    Loops over values from 1 to 10
    FOR    ${index}    IN RANGE    1    11
        Log    ${index}
    END

Also step given
    [Documentation]    Loops over values 5, 15, and 25
    FOR    ${index}    IN RANGE    5    26    10
        Log    ${index}
    END

Negative step
    [Documentation]    Loops over values 13, 3, and -7
    FOR    ${index}    IN RANGE    13    -13    -10
        Log    ${index}
    END

Arithmetic
    [Documentation]    Arithmetic with variable
    FOR    ${index}    IN RANGE    ${var} + 1
        Log    ${index}
    END

Float parameters
    [Documentation]    Loops over values 3.14, 4.34, and 5.54
    FOR    ${index}    IN RANGE    3.14    6.09    1.2
        Log    ${index}
    END
複製代碼

For-in-enumerate loop

有時,循環遍歷列表並跟蹤您在列表中的位置是有用的。robot有一個特殊的 FOR index ... IN ENUMERATE ...語法。此語法源自Python內置的enumerate()函數。

(1)FOR- IN - ENUMERATE循環的工做方式與常規的for循環同樣,只是循環變量後面的單元格必須是IN ENUMERATE(區分大小寫),而且它們必須在任何其餘循環變量以前有一個額外的索引變量。索引變量第一次迭代的值是0,第二次是1,以此類推。

例如,下面兩個測試用例作相同的事情:

*** Variables ***
@{LIST}         a    b    c

*** Test Cases ***
Manage index manually
    ${index} =    Set Variable    -1
    FOR    ${item}    IN    @{LIST}
        ${index} =    Evaluate    ${index} + 1
        My Keyword    ${index}    ${item}
    END

For-in-enumerate
    FOR    ${index}    ${item}    IN ENUMERATE    @{LIST}
        My Keyword    ${index}    ${item}
    END
複製代碼

(2)從robot 4.0開始,可使用start=number, 來指定索引從哪一個數字開始遞增,跟序列中的實際位置沒有關係,好比序列中有三個元素,start=0, 則索引依次顯示爲0,1,2; start=10, 索引依次顯示爲10,11,12

*** Variables ***
@{LIST}         a    b    c
${START}        10

*** Test Cases ***
For-in-enumerate with start
    FOR    ${index}    ${item}    IN ENUMERATE    @{LIST}    start=0
        My Keyword    ${index}    ${item}
    END

Start as variable
    FOR    ${index}    ${item}    IN ENUMERATE    @{LIST}    start=${start}
        My Keyword    ${index}    ${item}
    END
複製代碼

ride 執行結果:

Starting test: RobotAutoTest.TestCase.MyTest.My Test Suit2 test for.016_TC_For-in-enumerate with start
20210721 16:52:10.240 :  INFO : 0
20210721 16:52:10.241 :  INFO : a
20210721 16:52:10.243 :  INFO : 1
20210721 16:52:10.244 :  INFO : b
20210721 16:52:10.247 :  INFO : 2
20210721 16:52:10.247 :  INFO : c
Ending test:   RobotAutoTest.TestCase.MyTest.My Test Suit2 test for.016_TC_For-in-enumerate with start

Starting test: RobotAutoTest.TestCase.MyTest.My Test Suit2 test for.017_TC_Start as variable
20210721 16:52:10.253 :  INFO : 10
20210721 16:52:10.253 :  INFO : a
20210721 16:52:10.256 :  INFO : 11
20210721 16:52:10.257 :  INFO : b
20210721 16:52:10.259 :  INFO : 12
20210721 16:52:10.260 :  INFO : c
Ending test:   RobotAutoTest.TestCase.MyTest.My Test Suit2 test for.017_TC_Start as variable
複製代碼

(3)就像常規的for循環同樣,你能夠在每次循環迭代中循環多個值,只要你的列表中的值的數量能被循環變量的數量整除(不包括第一個,index變量):

*** Test Case ***
For-in-enumerate with two values per iteration
    FOR    ${index}    ${en}    ${fi}    IN ENUMERATE
    ...    cat      kissa
    ...    dog      koira
    ...    horse    hevonen
        Log    "${en}" in English is "${fi}" in Finnish (index: ${index})
    END
複製代碼

ride執行結果

Starting test: RobotAutoTest.TestCase.MyTest.My Test Suit2 test for.018_TC_For-in-enumerate with two values per iteration
20210721 17:02:23.987 :  INFO : "cat" in English is "kissa" in Finnish (index: 0)
20210721 17:02:23.989 :  INFO : "dog" in English is "koira" in Finnish (index: 1)
20210721 17:02:23.991 :  INFO : "horse" in English is "hevonen" in Finnish (index: 2)
Ending test:   RobotAutoTest.TestCase.MyTest.My Test Suit2 test for.018_TC_For-in-enumerate with two values per iteration
複製代碼

(4)若是你只在for-in-enumerate循環中使用一個循環變量,該變量將成爲包含索引和迭代值的Python元組:

*** Variables ***
@{LIST}         a    b    c
*** Test Case ***
For-in-enumerate with one loop variable
    FOR    ${x}    IN ENUMERATE    @{LIST}
        Length Should Be    ${x}    2
        Log    Index is ${x}[0] and item is ${x}[1].
    END
複製代碼

ride執行結果

Starting test: RobotAutoTest.TestCase.MyTest.My Test Suit2 test for.019_TC_For-in-enumerate with one loop variable
20210721 17:05:50.303 :  INFO : Length is 2
20210721 17:05:50.304 :  INFO : Index is 0 and item is a.
20210721 17:05:50.307 :  INFO : Length is 2
20210721 17:05:50.309 :  INFO : Index is 1 and item is b.
20210721 17:05:50.313 :  INFO : Length is 2
20210721 17:05:50.315 :  INFO : Index is 2 and item is c.
Ending test:   RobotAutoTest.TestCase.MyTest.My Test Suit2 test for.019_TC_For-in-enumerate with one loop variable 
複製代碼

For-in-zip loop

(1)有些測試創建幾個相關的列表,而後將它們一塊兒循環。機器人框架對於這種狀況有一個快捷方式:for…in zip …,它源自Python內置的zip()函數。

*** Variables ***
@{NUMBERS}       ${1}    ${2}    ${5}
@{NAMES}         one     two     five

*** Test Cases ***
Iterate over two lists manually
    ${length}=    Get Length    ${NUMBERS}
    FOR    ${index}    IN RANGE    ${length}
        Log Many    ${NUMBERS}[${index}]    ${NAMES}[${index}]
    END

For-in-zip
    FOR    ${number}    ${name}    IN ZIP    ${NUMBERS}    ${NAMES}
        Log Many    ${number}    ${name}
    END
複製代碼

ride執行結果

Starting test: RobotAutoTest.TestCase.MyTest.My Test Suit2 test for.020_TC_Iterate over two lists manually
20210721 17:10:54.356 :  INFO : Length is 3
20210721 17:10:54.356 :  INFO : ${length} = 3
20210721 17:10:54.358 :  INFO : 1
20210721 17:10:54.359 :  INFO : one
20210721 17:10:54.361 :  INFO : 2
20210721 17:10:54.361 :  INFO : two
20210721 17:10:54.364 :  INFO : 5
20210721 17:10:54.364 :  INFO : five
Ending test:   RobotAutoTest.TestCase.MyTest.My Test Suit2 test for.020_TC_Iterate over two lists manually

Starting test: RobotAutoTest.TestCase.MyTest.My Test Suit2 test for.021_TC_For-in-zip
20210721 17:10:54.369 :  INFO : 1
20210721 17:10:54.370 :  INFO : one
20210721 17:10:54.372 :  INFO : 2
20210721 17:10:54.372 :  INFO : two
20210721 17:10:54.374 :  INFO : 5
20210721 17:10:54.375 :  INFO : five
Ending test:   RobotAutoTest.TestCase.MyTest.My Test Suit2 test for.021_TC_For-in-zip 
複製代碼

(2)與for-in-range循環和for- IN -enumerate相似,for-in-zip循環要求循環變量後的單元格讀取IN ZIP(區分大小寫)。for-in-zip循環使用的值必須是列表或相似列表的對象。當最短列表耗盡時,循環將中止。

要迭代的列表必須老是以標量變量(如${items})的形式給出,或者以列表變量(如@{Lists})的形式給出,這些變量產生實際的迭代列表。前一種方法更常見,上面已經演示過了。後一種方法是這樣的:

*** Variables ***
@{NUMBERS}       ${1}    ${2}    ${5}
@{NAMES}         one     two     five
@{LISTS}         ${NUMBERS}    ${NAMES}

*** Test Cases ***
For-in-zip
    FOR    ${number}    ${name}    IN ZIP    @{LISTS}
        Log Many    ${number}    ${name}
    END
複製代碼

ride執行結果

Starting test: RobotAutoTest.TestCase.MyTest.My Test Suit2 test for.022_TC_LISTS_For-in-zip
20210721 17:14:51.716 :  INFO : 1
20210721 17:14:51.717 :  INFO : one
20210721 17:14:51.719 :  INFO : 2
20210721 17:14:51.719 :  INFO : two
20210721 17:14:51.721 :  INFO : 5
20210721 17:14:51.721 :  INFO : five
Ending test:   RobotAutoTest.TestCase.MyTest.My Test Suit2 test for.022_TC_LISTS_For-in-zip 
複製代碼

迭代列表的數量沒有限制,但必須與循環變量的數量匹配。或者,也能夠只有一個循環變量,而後它變成一個Python元組,從全部列表中獲取項目。

*** Variables ***
@{ABC}           a    b    c
@{XYZ}           x    y    z
@{NUM}           1    2    3    4    5

*** Test Cases ***
For-in-zip with multiple lists
    FOR    ${a}    ${x}    ${n}    IN ZIP    ${ABC}    ${XYZ}    ${NUM}
        Log Many    ${a}    ${x}    ${n}
    END

For-in-zip with one variable
    FOR    ${items}    IN ZIP    ${ABC}    ${XYZ}    ${NUM}
        Length Should Be    ${items}    3
        Log Many    ${items}[0]    ${items}[1]    ${items}[2]
    END
複製代碼

ride執行結果:若是列表的項數不相等,那麼最短列表定義有多少次迭代,而長列表末尾的值將被忽略。例如,上面的示例只循環三次,${NUM}列表中的值4和5將被忽略。

Starting test: RobotAutoTest.TestCase.MyTest.My Test Suit2 test for.023_TC_For-in-zip with multiple lists
20210721 17:18:27.545 :  INFO : a
20210721 17:18:27.546 :  INFO : x
20210721 17:18:27.546 :  INFO : 1
20210721 17:18:27.549 :  INFO : b
20210721 17:18:27.549 :  INFO : y
20210721 17:18:27.550 :  INFO : 2
20210721 17:18:27.552 :  INFO : c
20210721 17:18:27.553 :  INFO : z
20210721 17:18:27.553 :  INFO : 3
Ending test:   RobotAutoTest.TestCase.MyTest.My Test Suit2 test for.023_TC_For-in-zip with multiple lists

Starting test: RobotAutoTest.TestCase.MyTest.My Test Suit2 test for.024_TC_For-in-zip with one variable
20210721 17:18:27.559 :  INFO : Length is 3
20210721 17:18:27.560 :  INFO : a
20210721 17:18:27.561 :  INFO : x
20210721 17:18:27.561 :  INFO : 1
20210721 17:18:27.564 :  INFO : Length is 3
20210721 17:18:27.566 :  INFO : b
20210721 17:18:27.566 :  INFO : y
20210721 17:18:27.566 :  INFO : 2
20210721 17:18:27.569 :  INFO : Length is 3
20210721 17:18:27.570 :  INFO : c
20210721 17:18:27.571 :  INFO : z
20210721 17:18:27.571 :  INFO : 3
Ending test:   RobotAutoTest.TestCase.MyTest.My Test Suit2 test for.024_TC_For-in-zip with one variable
複製代碼

Dictionary iteration

*** Variables ***
&{DICT}          a=1    b=2    c=3

*** Test Cases ***
Dictionary iteration
    FOR    ${key}    ${value}    IN    &{DICT}
        Log    Key is '${key}' and value is '${value}'.
    END

Dictionary iteration with enumerate
    FOR    ${index}    ${key}    ${value}    IN ENUMERATE    &{DICT}
        Log    On round ${index} key is '${key}' and value is '${value}'.
    END

Multiple dictionaries and extra items in 'key=value' syntax
    &{more} =    Create Dictionary    e=5    f=6
    FOR    ${key}    ${value}    IN    &{DICT}    d=4    &{more}    g=7
        Log    Key is '${key}' and value is '${value}'.
    END



One loop variable    FOR    ${item}    IN    &{DICT}
        Log    Key is '${item}[0]' and value is '${item}[1]'.
    END
One loop variable with enumerate
    FOR    ${item}    IN ENUMERATE    &{DICT}
        Log    On round ${item}[0] key is '${item}[1]' and value is '${item}[2]'.
    END

Two loop variables with enumerate
    FOR    ${index}    ${item}    IN ENUMERATE    &{DICT}
        Log    On round ${index} key is '${item}[0]' and value is '${item}[1]'.
    END
複製代碼

除了遍歷字典中的名稱和值以外,還能夠遍歷鍵,而後可能根據鍵獲取值。這種語法要求使用字典做爲列表變量:

*** Test Cases ***
One loop variable
    FOR    ${key}    IN    @{DICT}
        Log    Key is '${key}' and value is '${DICT}[${key}]'.
    END
複製代碼

請注意

for-in-range或for-in-zip循環不支持字典迭代。

Exiting for loop

一般執行for循環直到全部的循環值都被迭代或者循環中使用的關鍵字失敗。若是須要提早退出循環,可使用BuiltIn關鍵字exit For loop和exit For loop If來完成。它們的工做原理相似於Python、Java和許多其餘編程語言中的break語句。

Exit For Loop和Exit For Loop If關鍵字能夠直接在For循環內部或循環使用的關鍵字中使用。在這兩種狀況下,測試在循環以後繼續執行。在for循環以外使用這些關鍵字是錯誤的。

*** Test Cases ***
Exit Example
    ${text} =    Set Variable    ${EMPTY}
    FOR    ${var}    IN    one    two
        Run Keyword If    '${var}' == 'two'    Exit For Loop
        ${text} =    Set Variable    ${text}${var}
    END
    Should Be Equal    ${text}    one
複製代碼

Continuing for loop

除了提早退出for循環外,還能夠在執行全部關鍵字以前繼續執行循環的下一個迭代。這可使用BuiltIn關鍵字Continue For Loop和Continue For Loop If來完成,就像許多編程語言中的Continue語句同樣。

Continue For Loop和Continue For Loop if 關鍵字能夠直接在For循環內部或循環使用的關鍵字中使用。在這兩種狀況下,該迭代中的其他關鍵字都會被跳過,並在下一個迭代中繼續執行。若是在最後一次迭代中使用了這些關鍵字,那麼循環以後將繼續執行。在for循環以外使用這些關鍵字是錯誤的。

*** Test Cases ***
Continue Example
    ${text} =    Set Variable    ${EMPTY}
    FOR    ${var}    IN    one    two    three
        Continue For Loop If    '${var}' == 'two'
        ${text} =    Set Variable    ${text}${var}
    END
    Should Be Equal    ${text}    onethree
複製代碼

Repeating single keyword

在只須要重複一個關鍵字的狀況下,For循環可能會過多。在這些狀況下,一般更容易使用BuiltIn關鍵字Repeat關鍵字。這個關鍵字接受一個關鍵字以及重複它的次數做爲參數。重複關鍵字的時間能夠有一個可選的後綴times或x,以使語法更容易閱讀。

*** Test Cases ***
Example
    Repeat Keyword    5    Some Keyword    arg1    arg2
    Repeat Keyword    42 times    My Keyword
    Repeat Keyword    ${var}    Another Keyword    argument
複製代碼
相關文章
相關標籤/搜索