有關循環的關鍵字
- 在一系列元素中循環
- 根據數字的範圍來循環
- 重複執行某一個關鍵字多次
最後一個和真正的循環是有差别的,意味着你得将所有操作封裝到一個關鍵字中。并且,在執行結束之前無法退出循環。
文法非常直接,不需要過多解釋。唯一需要注意的是,循環體内的關鍵字必須用 ‘\’ 來進行轉義。
測試:
*** Settings ***
Library String
*** Test Cases ***For-Loop-In-Range
: FOR ${INDEX} IN RANGE 1 3
\ Log ${INDEX}
\ ${RANDOM_STRING}= Generate Random String ${INDEX}
\ Log ${RANDOM_STRING}
For-Loop-Elements
@{ITEMS} Create List Star Trek Star Wars Perry Rhodan
:FOR ${ELEMENT} IN @{ITEMS}
\ Log ${ELEMENT}
\ ${ELEMENT} Replace String ${ELEMENT} ${SPACE} ${EMPTY}
\ Log ${ELEMENT}
For-Loop-Exiting
@{ITEMS} Create List Good Element 1 Break On Me Good Element 2
:FOR ${ELEMENT} IN @{ITEMS}
\ Log ${ELEMENT}
\ Run Keyword If '${ELEMENT}' == 'Break On Me' Exit For Loop
\ Log Do more actions here ...
Repeat-Action
Repeat Keyword 2 Log Repeating this ...
輸出:
Starting test: StandardLoopDemo.For-Loop-In-Range20130426 11:24:14.389 : INFO : 120130426 11:24:14.390 : INFO : ${RANDOM_STRING} = B20130426 11:24:14.390 : INFO : B20130426 11:24:14.391 : INFO : 220130426 11:24:14.392 : INFO : ${RANDOM_STRING} = ih20130426 11:24:14.392 : INFO : ih
Ending test: StandardLoopDemo.For-Loop-In-Range
Starting test: StandardLoopDemo.For-Loop-Elements20130426 11:24:14.394 : INFO : @{ITEMS} = [ Star Trek | Star Wars | Perry Rhodan ]20130426 11:24:14.395 : INFO : Star Trek20130426 11:24:14.396 : INFO : ${ELEMENT} = StarTrek20130426 11:24:14.396 : INFO : StarTrek20130426 11:24:14.397 : INFO : Star Wars20130426 11:24:14.398 : INFO : ${ELEMENT} = StarWars20130426 11:24:14.398 : INFO : StarWars20130426 11:24:14.399 : INFO : Perry Rhodan20130426 11:24:14.400 : INFO : ${ELEMENT} = PerryRhodan20130426 11:24:14.400 : INFO : PerryRhodan
Ending test: StandardLoopDemo.For-Loop-Elements
Starting test: StandardLoopDemo.For-Loop-Exiting20130426 11:24:14.402 : INFO : @{ITEMS} = [ Good Element 1 | Break On Me | Good Element 2 ]20130426 11:24:14.402 : INFO : Good Element 120130426 11:24:14.403 : INFO : Do more actions here ...20130426 11:24:14.404 : INFO : Break On Me
Ending test: StandardLoopDemo.For-Loop-Exiting
Starting test: StandardLoopDemo.Repeat-Action20130426 11:24:14.408 : INFO : Repeating keyword, round 1/220130426 11:24:14.408 : INFO : Repeating this ...20130426 11:24:14.408 : INFO : Repeating keyword, round 2/220130426 11:24:14.409 : INFO : Repeating this ...
Ending test: StandardLoopDemo.Repeat-Action
有關條件判斷的關鍵字
在測試代碼中使用條件判斷會帶來不少争議。不用擔心,唯一應該記住的是,測試實作應該盡可能簡單明了,不要混雜過多條件邏輯。
在下面的例子中将使用以下相關的關鍵字。
- Run Keyword - 這個關鍵字将其他關鍵字作為一個變量傳入。這意味着,測試能夠動态地改變執行時所使用的關鍵字,比如執行其他函數傳回的關鍵字。
- Run Keyword If - 在測試複雜結構時非常有用,比如被測的 web 頁面在輸入不同時會有不同的選項。但是在測試中混有過多的程式結構會使 troubleshooting 變得困難。
- Run Keyword And Ignore Error - 哈,我還沒有找到對應的實際例子。
- Run Keyword If Test Failed - 如果測試失敗了可以用這個打一些 log,或者打一個快照。在 troubleshooting 時有用。
測試:
*** Test Cases ***Run-Keyword ${MY_KEYWORD}= Set Variable Log
Run Keyword ${MY_KEYWORD} TestRun-Keyword-If ${TYPE}= Set Variable V1
Run Keyword If '${TYPE}' == 'V1' Log Testing Variant 1 Run Keyword If '${TYPE}' == 'V2' Log Testing Variant 2 Run Keyword If '${TYPE}' == 'V3' Log Testing Variant 3Run-Keyword-Ignore-Error @{CAPTAINS} Create List Picard Kirk Archer
Run Keyword And Ignore Error Should Be Empty ${CAPTAINS} Log Reached this point despite of error
輸出:
Starting test: Robot Blog.StandardConditionDemo.Run-Keyword20130426 13:34:50.840 : INFO : ${MY_KEYWORD} = Log20130426 13:34:50.841 : INFO : Test
Ending test: Robot Blog.StandardConditionDemo.Run-Keyword
Starting test: Robot Blog.StandardConditionDemo.Run-Keyword-If20130426 13:34:50.843 : INFO : ${TYPE} = V120130426 13:34:50.844 : INFO : Testing Variant 1
Ending test: Robot Blog.StandardConditionDemo.Run-Keyword-If
Starting test: Robot Blog.StandardConditionDemo.Run-Keyword-Ignore-Error20130426 13:34:50.847 : INFO : @{CAPTAINS} = [ Picard | Kirk | Archer ]20130426 13:34:50.848 : INFO : Length is 320130426 13:34:50.849 : FAIL : '[u'Picard', u'Kirk', u'Archer']' should be empty20130426 13:34:50.850 : INFO : Reached this point despite of error
Ending test: Robot Blog.StandardConditionDemo.Run-Keyword-Ignore-Error