近來經常有童鞋問我一些RF的基本問題,如:如何實現循環?如何退出循環?如何實現判斷?如何做類型轉換?其實,作為一門表格語言,為了保持簡單的結構,RF沒有像別的高級語言那樣提供類似if else while等內置關鍵字來實現各種邏輯功能(注1),而是提供給了用戶BuiltIn庫。如果用戶想在測試用例中實現比較復雜的邏輯,那就需要對BuiltIn中的重要關鍵字有一些了解。另外,BuiltIn庫中還封裝了很多常見方法和能夠控制RF運行狀態的關鍵字,如果想用好RF,一定要對BuiltIn庫中的函數有一個比較全面的理解。下面就帶着大家認識一下BuiltIn庫中比較重要的關鍵字。
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,來實現循環結構。功能還是比較弱的。
