1.keyword系列關鍵字:
1.Run Keyword/Run Keywords/Run Keyword And Continue On Failure/Run Keyword And Expect Error/Run Keyword And Ignore Error/Wait Until Keyword Succeeds
Run Keyword:執行給定的關鍵字,待執行的關鍵字可以使一個動態變化的變量。
Run Keywords:執行給定的一系列關鍵字,多用於setups和teardowns 中。
Run Keyword And Continue On Failure:在給定的關鍵字執行失敗后繼續執行,在case結束后報錯。
Run Keyword And Expect Error:
Run Keyword And Ignore Error:執行給定的關鍵字,忽視錯誤,返回兩個值:第一個值時關鍵字的執行結果(Pass/Fail),第二個值是關鍵字的返回值或錯誤信息。
Wait Until Keyword Succeeds:在給定時間內等待關鍵字執行成功。
2.return from keyword/return from keyword if/Run Keyword And Return/Run Keyword And Return If/Run Keyword And Return Status
return from keyword:當滿足條件時,從關鍵字中跳出來,不執行該關鍵字之后的步驟。
使用在關鍵字中,不能在測試用例中直接使用(會報Invalid 'Return From Keyword' usage.的錯誤)
return from keyword if:后跟條件,當條件成立時,從用戶關鍵字中跳出,類似於上圖中第一種情況。
Run Keyword And Return:執行給定關鍵字KEYWORD1,然后從用戶關鍵字中跳出,類似於run keywords KEYWORD1 AND return from keyword
Run Keyword And Return KEYWORD1
Run Keyword And Return If:滿足一定條件后執行給定關鍵字KEYWORD1,然后從用戶關鍵字中跳出,類似於run keyword if 條件 關鍵字1,return from keyword
Run Keyword And Return If 條件 KEYWORD1
Run Keyword And Return Status:執行關鍵字,並返回關鍵字的狀態(True/False)
注意:此關鍵字不會捕獲由無效語法、超時或致命異常引起的錯誤。多與Run Keyword And Continue On Failure配合使用,較雞肋。
3.Run Keyword If/Run Keyword Unless/Run Keyword If All Critical Tests Passed/Run Keyword If All Tests Pased/Run Keyword If Any Critical Tests Failed/Run Keyword If Any Tests Failed/Run Keyword If Test Failed/Run Keyword If Test Passed/Run Keyword If Timeout Occurred
Run Keyword If/Run Keyword Unless:如果條件滿足/不滿足,執行后跟關鍵字
Run Keyword If All Critical Tests Passed/Run Keyword If Any Critical Tests Failed/Run Keyword If All Tests Passed/Run Keyword If Any Tests Failed:只能被用在suite teardown
Run Keyword If Test Failed/Run Keyword If Test Passed/Run Keyword If Timeout Occurred:只能被用在test teardown
2.Should 系列關鍵字:
Should Be Empty/Should Not Be Empty
Should Be Equal/Should Not Be Equal
Should Be Equal As Integers/Should Not Be Equal As Integers
Should Be Equal As Numbers/Should Not Be Equal As Numbers
Should Be Equal As Strings/Should Not Be Equal As Strings
Should Be True/Should Not Be True
Should Contain/Should Not Contain
Should Contain Any/Should Not Contain Any
Should End With/Should Not End With
Should Not Start With/Should Start With
Should Match/Should Not Match
Should Match Regexp/Should Not Match Regexp
Variable Should Exist/Variable Should Not Exist
Should Contain X Times
3.Convert To系列關鍵字:
Convert To Binary · Convert To Boolean Convert To Bytes Convert To Hex · Convert To Integer · Convert To Number · Convert To Octal · Convert To String
4.set 系列關鍵字
Set Global Variable/Set Suite Variable/Set Test Variable
Set Library Search Order
用法:該關鍵字用於解決測試數據中的關鍵字名稱與多個關鍵字匹配時的沖突。
例如:file should exist在OperatingSystem與SSHLibrary中都存在,如果在使用時未指定庫,則會報錯:Keyword 'File Should Exist' found both from a custom test library 'SSHLibrary' and a standard library 'OperatingSystem'. The custom keyword is used. To select explicitly, and to get rid of this warning, use either 'SSHLibrary.File Should Exist' or 'OperatingSystem.File Should Exist'.
使用該關鍵字指定OperatingSystem為第一搜索順序:
注意:該關鍵字執行后只適用於該suite;resources 中關鍵字的優先級要比庫中的高;設置參數(library/resource name)要注意大小寫&空格
Set Suite Documentation/Set Test Documentation
用法:設置suite/test的描述信息
Set Test Message
設置當前測試用例的消息,設置后在case的teardown中通過RF 內置變量${TEST MESSAGE}來獲取到。
Set Suite Metadata
用法:設置當前用例的元數據,設置后可以通過內置變量${SUITE METADATA}來獲取到。
Set Log Level
Valid levels are TRACE, DEBUG, INFO (default), WARN, ERROR and NONE (no logging)
Set Tags
在當前case或整個suite所有case中增加新的標簽,可通過內置變量@{TEST TAGS}獲取所有標簽
Set Variable
用法很廣泛,用時需注意,建議只把它當成一個對普通變量進行賦值的關鍵字,例如第一第二種用法:
Set Variable If
5.loop/execution類關鍵字
continue for loop/continue for loop if
用法:跳過該次循環中后續的步驟,繼續下一次的循環
結果:
Exit For Loop/Exit For Loop If
用法:退出循環
結果:
pass execution/pass execution if
用法:退出
6.import類關鍵字
import library/import resource/import variables
7.get 類關鍵字
Get Count/Get Length/Get Library Instance/Get Time/Get Count/Get Variable Value/Get Variables
8.常用類關鍵字
Sleep
comment
在日志中顯示信息。
evaluate
create dictionary create list
Fail
catenate
log /log many/log to console/log variables
9.其他類關鍵字
call method
Arguments: [ object | method_name | *args | **kwargs ]
Fatal Error
keyword should exist
length should be
no operation
regexp escape
reload library
remove tags
repeat keyword
replace variables
Evaluate 關鍵字:如果你需要進行一些數值運算並得到結果,你就需要用到Evaluate關鍵字。Evaluate會把你想要計算的表達式直接傳遞給Python,並把Python的計算結果返回給你。這是最經常要用到的。
Should 系列關鍵字:Should系列關鍵字是Should大頭的一系列關鍵字。
Should Be Empty · Should Be Equal · Should Be Equal As Integers · Should Be Equal As Numbers · Should Be Equal As Strings · Should Be True · Should Contain · Should Contain X Times· Should End With · Should Match · Should Match Regexp · Should Not Be Empty · Should Not Be Equal · Should Not Be Equal As Integers · Should Not Be Equal As Numbers · Should Not Be Equal As Strings · Should Not Be True · Should Not Contain · Should Not End With · Should Not Match ·Should Not Match Regexp · Should Not Start With · Should Start With
這些關鍵字都是用作判斷時用的,每個用例都會用到,比如我們的執行結果得到了一個字符串,我們要判斷這個字符串要與一個預期字符串相等,否則用例就無法通過,這時候,肯定會用上 Should Be Equal As String
關鍵字,其它關鍵字我們通過關鍵字的名字就能顧名思義,知道它的作用。
Convert To系列關鍵字:
Convert To Binary · Convert To Boolean · Convert To Hex · Convert To Integer · Convert To Number · Convert To Octal · Convert To String
做類型轉換比不可少。
Run keyword系列關鍵字:
Run Keyword If · Run Keyword If All Critical Tests Passed · Run Keyword If All Tests Passed · Run Keyword If Any Critical Tests Failed ·Run Keyword If Any Tests Failed · Run Keyword If Test Failed · Run Keyword If Test Passed · Run Keyword If Timeout Occurred ·
這些關鍵字能根據一個判斷條件的真假來看是否執行關鍵字。一般使用這些關鍵字來實現高級語言中的if else功能。最常用的是Run Keyword If 和 Run Keyword unless 他們倆實現的效果正好相反。
Exit For Loop關鍵字:
用作退出循環,一般和Run keyword if 關鍵字聯合使用,來實現條件退出。
Wait Until Keyword Succeeds
這是一個將異步調用變為同步調用的關鍵字。舉一個例子:如果call某個WebService,並且需要得到返回結果才能做下一部操作。我們就會用到這個關鍵字。
BuiltIn庫里還有很多寶貝,比如日期相關的關鍵字Get Time。讓測試暫停的Sleep等。都相當有用。還等什么?
去這個鏈接遍歷一遍它吧:)http://robotframework.googlecode.com/hg/doc/libraries/BuiltIn.html
注1:RF目前僅有2個內置關鍵字:FOF 和 IN,來實現循環結構。功能還是比較弱的。