介紹下Robotframework接口測試--常用的API,掌握其api就可以做更多的操作啦!
======================
庫類似於對象,關鍵字類似於對象中的函數、方法
那自定義關鍵字時,可以理解成自己寫函數、方法。
Robot Framework 並沒有像其它框架一樣提供一份完整的 API 文檔,所以,我們沒辦法通過官方 API文檔進行習。RIDE 提供了 F5 快捷鍵來打開幫助文檔。
一、標准庫之 BuiltIn
提供的關鍵字有用於驗證的(e.g. Should Be Equal, Should Contain),轉換的(e.g. Convert To Integer),其他(e.g. Log, Sleep, Run Keyword If, Set Global Variable)。
1.Name: Call Method
Arguments: [object | method_name | *args | **kwargs ]
Calls the named method of the given object with the provided arguments.
調用對象object的方法method_name
2.Name: Catenate
Arguments:[ *items ]
Catenates the given items together and returns the resulted string.
連接給定參數,返回字符串(默認用空格連接)
例子:
${str1} = |
Catenate |
Hello |
world |
|
${str2} = |
Catenate |
SEPARATOR=--- |
Hello |
world |
${str3} = |
Catenate |
SEPARATOR= |
Hello |
world實際結果: |
=>
- ${str1} = 'Hello world'
- ${str2} = 'Hello---world'
- ${str3} = 'Helloworld'
3.Name: Comment
Arguments:[ *messages ]
Displays the given messages in the log file as keyword arguments.
在日志文件中以關鍵字參數形式顯示指定信息。
Comment對參數不進行任何處理,僅僅是顯示在日志中,用於顯示簡單信息。
如果要顯示變量值,可以使用Log 或 Log Many關鍵字。
4.Name: Continue For Loop
Arguments:[ ]
Skips the current for loop iteration and continues from the next.
跳出當前迭代,繼續下一個迭代
Convert 轉換系列
5.Name: Convert To Binary
Arguments:[ item | base=base | prefix=prefix | length=length ]
Converts the given item to a binary string.
轉換參數為二進制字符串
Examples:
${result} =Convert To Binary 10 # Result is 1010
6.Name: Convert To Boolean 轉換參數為布爾值true或false。
7.Name: Convert To Bytes 根據輸入類型將給定的輸入轉換為字節。
8.Name: Convert To Hex Convert To Hex 轉換參數為十六進制字符串
${result} =Convert To Hex 255 # Result is FF
9.Name: Convert To Integer 轉換參數為整數。
10.Name: Convert To Number
Arguments:[ item | precision=precision ]
Converts the given item to a floating point number.
轉換參數為浮點數。
注意:precision=-1時的使用
11.Name: Convert To Octal 轉換參數為八進制字符串
12.Name: Convert To string 轉換參數為Unicode字符串
13.Name: Create Dictionary
Arguments:[ *items ]
Creates and returns a dictionary based on the given items.
根據給定參數創建字典
這里不能換成${dict}哦
小知識:robotframework可以使用 變量 , 列表 或 字典, 使用語法: ${變量}
@{列表}
和 &{字典}
14.Name: Create List
Arguments:[ *items ]
Returns a list containing given items. The returned list can be assigned both to ${scalar} and @{list} variables.
根據給定參數創建list,同時返回的列表可以被分配給 ${變量}
@{列表}使用。
15.Name: Evaluate
Arguments:[ *items ]
Arguments:[ expression | modules=modules | namespace=namespace ]
Evaluates the given expression in Python and returns the results.
用Python計算表達式並返回結果。
注意:如果表達式很復雜,建議在測試庫中實現邏輯。
16. Name: Exit For Loop
立即停止執行for循環。
17.Name:Fail
停止執行當前測試用例
Fails the test with the given message and optionally alters its tags.
18.Name:Fatal Error
停止執行整個測試。 后續用例不會執行,可能的teardown也不會執行。
Stops the whole test execution.
Get系列
19.Name: Get Count
Arguments:[ item1 | item2 ]
Returns and logs how many times item2 is found from item1.
返回並在日志中記錄item2在Item1中出現的次數。
20.Name: Get Length
Arguments:[ item]
Returns and logs the length of the given item as an integer.
返回並在日志中記錄item的長度. item可以是任何有一個長度的對象,例如,字符串,列表,或映射.
len1=‘hello,world!’ 字符串的長度, 而len2=['heoo,','word!']列表的長度是2
21.Name:Get Library Instance
返回指定測試庫的當前活動實例。
22.Name:Get Time
Arguments:[ format=format | time_=time_ ]
Returns the given time in the requested format.
按指定格式返回時間(下面第3行,返回的是UNIX 時間戳)
1) 如果格式中包含epoch,返回的是(Jan 1, 1970 0:00:00)開始的秒數。
2) 如果格式包含任何'year', 'month', 'day', 'hour', 'min', or 'sec',則返回對應部分。
3) 默認格式'2006-02-24 15:08:31'
默認返回當前時間。
23.Name:Get Variable Value
Arguments:[ name, default=None ]
Returns variable value or default if the variable does not exist.
返回變量值,如果變量不存在就返回default
23.Name:Get Variables
返回包含在當前范圍內的所有變量的字典
24.name:Import Library
在test suite導入庫。
可以在測試執行時動態導入庫。支持庫名和絕對路徑導入。使用'WITH NAME'還可以更改庫名。
25.name:Import Resource
在test suite導入資源文件。path必須是絕對路徑。斜線/作為路徑分隔符(包含Windows)。
26.name:Import Variables
導入變量文件
27.name:Keyword Should Exist
Arguments:[ name | msg=msg ]
Fails unless the given keyword exists in the current scope
除非關鍵字在當前作用域存在,否則失敗。有多個同名的關鍵字存在也會Fail
28.Name:Length Should Be
Arguments: [ item | length | msg=msg ]
Verifies that the length of the given item is correct.
驗證item的長度。(先通過關鍵字Get Length獲取item的長度。再使用該方法驗證)
Log系列
29.Name:Log
在指定日志級別下記錄message。
有效的日志級別有TRACE, DEBUG, INFO (default), HTML and WARN。
注意,無效的HTML會破壞整個日志文件,一定要謹慎使用。HTML消息實際上是使用了INFO級別。
WARN級別記錄的消息將在控制台和日志中測試執行的錯誤部分都可以看到。
30.Name:Log Many
在INFO級別下記錄messages為獨立的條目。分別打印每一項元素。
31.Name:Log To Console
把信息在控制台打印出來
32.Name:Log Variables
用給定的日志級別記錄當前作用域中的所有變量。
33.Name:No Operation
無操作,不做任何事情
34.Name:Pass Execution
Arguments:[ message | *tags ]
Skips rest of the current test, setup, or teardown with PASS status.
無條件的跳過后面的步驟
35.Name:Pass Execution if
滿足條件時,跳過后面的步驟
36.Name:Regexp Escape
Returns each argument string escaped for use as a regular expression.
返回轉義后用作正則表達式的每個參數字符串
此關鍵字可用於轉義要與“should match regexp”和“should not match regexp”關鍵字一起使用。
37.Name:Reload Library
重新檢查指定庫提供的關鍵字。
可以在測試數據中顯式調用,也可以在它提供的關鍵字發生更改時由庫本身調用。
38.name:Remove Tags
從當前測試用例或suite中刪除標簽 。
Arguments:[ *tags ]
Tag可以精確地給出,或者使用模式:'*'匹配所有字符,“?”單個字符的通配符給出。
39.name:Repeat Keyword
Arguments:[ repeat | name | *args ]
Executes the specified keyword multiple times.
執行指定的關鍵字times次 或多少時間(e.g. 1 minute, 2 min 3 s)
40.Name:Replace Variables
替換text中的變量。
如果文本中包含未定義的變量時,關鍵字執行Fail。
例子:假定文件“template.txt”的內容為“Hello $ {NAME}!”,變量的'${NAME}'的值為'Robot'。
41.name:Return From Keyword
Arguments:[ *return_values ]
Returns from the enclosing user keyword.
此關鍵字可用於從具有pass狀態的用戶關鍵字返回,而無需完全執行它。
此關鍵字通常包裝為其他關鍵字,如“run keyword if”或“run keyword if test passed”,以根據條件返回:
42.name:Return From Keyword if
43.name:Run Keyword
Arguments:[ name | *args ]
Executes the given keyword with the given arguments.
根據參數args執行關鍵字name。
name可以動態設定,例如,來自另一個關鍵字的返回值或命令行。
44.name:Run Keyword And Continue On Failure
根據參數args執行關鍵字name,錯誤時依舊繼續執行。
45.name:Run Keyword And Expect Error
Arguments:[ expected_error | name | *args ]
運行關鍵字並檢查是否發生了預期的錯誤。
預期的錯誤以與robot框架報告相同的格式給出預期錯誤。默認情況下,它被解釋為帶有*、?的全局模式。和[chars]作為通配符。
從robot framework 3.1開始,預期錯誤可以使用下表中解釋的各種前綴進行更改。前綴區分大小寫,必須用冒號和可選的空格(如prefix:message或prefix:message)將其與實際消息分開。
46.name:Run Keyword And Ignore Error
根據參數args執行關鍵字,並忽略錯誤
47.name:Run Keyword And Return
48.name:Run Keyword And Return if
49.name:Run Keyword And Return Status
返回狀態值作為一個Boolean變量。
50.name:Run Keyword If
滿足條件時run keyword
51.name:Run Keyword If All Critical Tests Passed
如果所有Critical測試用例通過,則根據參數args執行關鍵字。
只能用於suite teardown。
52.name:Run Keyword If All Tests Passed
如果所有測試用例通過,則根據參數args執行關鍵字。
只能用於suite teardown。
53.Run Keyword If Any Critical Tests Failed
如果有Critical測試用例Fail,則根據參數args執行關鍵字。
只能用於suite teardown。
54.Run Keyword If Any Tests Failed
55.Run Keyword If Test Failed
如果測試用例Fail,則根據參數args執行關鍵字。
只能用於test teardown。
56.Run Keyword If Test Passed
57.Run Keyword If Timeout Occurred
如果測試用例超時,則根據參數args執行關鍵字,只能用於test teardown。
58.Run Keyword Unless
如果condition為假,根據參數args執行關鍵字。
59.Run Keywords
按序執行所有關鍵字
主要用於setups 和 teardowns中,不想創建更高級別的關鍵字但是有很多很多動作要進行
set系列
通常情況下,每個變量默認都是局部變量。
一個case里的變量,作用域在這個case內部;
一個userkeyword里的變量,作用域在這個userkeyword內部;
一個文件型suite里的變量,作用域在這個suite內部,所有下面的case也都可以使用。
一個目錄型suite里的變量,作用域在這個目錄內,他下面的文件型suite是無法使用的,所以一般在目錄下新增變量沒有太大意義。
作用域是可以修改的,即通過一些系統關鍵字,對變量進行作用域的設定,常用的關鍵字有:
Set Global Variable ——設定全局級變量
Set Suite Variable ——設定suite級變量
Set Test Variable ——設定case級變量
60.name:Set Global Variable
Arguments:[ name | *values ]
Makes a variable available globally in all tests and suites.
設置全局變量。
和命令行使用'--variable' or '--variablefile'創建的效果一樣。
65.name:Set Suite Variable
Makes a variable available everywhere within the scope of the current suite.
此關鍵字設置的變量在當前執行的測試套件范圍內的任何位置都可用,
默認情況下,可能的子測試套件看不到該變量,
它的作用域?
第2行,children=true是說能作用於子suite嗎?
如果變量的值本身是變量(轉義或非轉義),則必須始終使用轉義格式設置變量??
此限制也適用於“set test variable”、“set global variable”、“variable should exist”、“variable should not exist”和“get variable value”關鍵字。
71.name:Set Variable
主要用於設置標量變量。
此外,它還可以用於將包含列表的標量變量轉換為列表變量或多個標量變量。建議在創建新列表時使用“create list”
本關鍵字創建的變量僅僅在當前范圍有效。
72.name:Set Variable if
Arguments:[ condition | *values ]
Sets variable based on the given condition.
基於condition設置變量。
${rc} is assumed to be zero.
多個Set Variable If關鍵字可以嵌套,效果類似於'Else If'……
61.name:Set Library Search Order
Sets the resolution order to use when a name matches multiple keywords.
name與多個關鍵字匹配時,設置庫查找順序
當測試數據中的關鍵字名稱與多個關鍵字匹配時,庫搜索順序用於解決沖突。
62.name:Set Log Level
Sets the log threshold to the specified level and returns the old level.
設置log級別並返回舊的log級別
默認級別是INFO.可用的log級別有:TRACE, DEBUG, INFO (default), WARN and NONE (no logging),對應的信息由多到少
63.name:Set Suite Documentation
64.name:Set Suite Metadata 元數據
66.name:Set Tags
Adds given tags for the current test or all tests in a suite.
在測試用例或者suite中設置標簽。
Teardown中不能使用該關鍵字。
67.name:Set Task Variable
Arguments:[ name | *values ]
Makes a variable available everywhere within the scope of the current task.
使變量在當前任務范圍內的任何位置都可用
68.name:Set Test Documentation
69.name:Set Test Message
為當前測試用例設置消息。
This keyword can not be used in suite setup or suite teardown.
70.name:Set Test Variable
should系列
73.name:Should Be Empty 期望為空
Arguments:[ item | msg=msg ]
Verifies that the given item is empty.
驗證給定的項目為空。
Item的長度通過Get Length關鍵字獲取。
74.name:Should Be Equal 期望相等
Arguments:[ first | second | msg=msg | values=values | ignore_case=ignore_case ]
If msg is given and values gets a true value (default), the error message is <msg>: <first> != <second>.
這就是message的作用
省略時,默認的values=true,即發生錯誤時,要打印出來
75.name:Should Be Equal As Integers
Arguments:[ first | second | msg=msg | values=values | base=base ]
Fails if objects are equal after converting them to integers.
轉換為整數數,比較,是否相等
76.name:Should Be Equal As Numbers
轉換為實數后,期望相等
77.name:Should Be Equal As Strings
轉換為字符串后,期望相等
78.name:Should Be True
期望condition為true
79.name:Should Contain
期望container中包含item
80.name:Should Contain Any
期望container中包含itemS中的任意一項
80.name:Should Contain Any X Times
期望container中包含item X次
81.name:Should End With
期望str1以str2結尾
82.name:Should Match
string匹配模式pattern
模式匹配是和shell中的文件匹配類似,它區分大小寫,'*'匹配所有字符,“?”單個字符。
83.name:Should Match Regexp
string能匹配正則表達式pattern
84.name:Should Not Be Empty
變量不能為空
85.name:Should Not Be Equal
期望兩個參數不相等
86.name:Should Not Be Equal As Integers
兩個參數轉換為整數后,期望不相等
87.name:Should Not Be Equal As Numbers
轉換為實數后,是否相等
采用Convert To Number關鍵字使用指定的精度進行轉換。
88.name:Should Not Be Equal As Strings
轉換為字符串后,是否相等
89.name:Should Not Be True
Arguments:[ condition | msg=msg ]
期望condition不為true
90.name:Should Not Contain
Arguments:[ container | item | msg=msg | values=values | ignore_case=ignore_case ]
Fails if container contains item one or more times.
期望container中不包含item
91.name:Should Not Contain Any
Arguments:[ container | *items | **configuration ]
Fails if container contains one or more of the *items.
期望container中不包含itemS中的任意一項
92.name:Should Not End With
Arguments:[ str1 | str2 | msg=msg | values=values | ignore_case=ignore_case ]
Fails if the string str1 ends with the string str2.
93.name:Should Not Match
string不能匹配模式pattern
模式匹配是和shell中的文件匹配類似,它區分大小寫,'*'匹配所有字符,“?”單個字符。
94.name:Should Not Match Regexp
string不能匹配正則表達式pattern
95.name:Should Not Start With
Arguments:[ str1 | str2 | msg=msg | values=values | ignore_case=ignore_case ]
不以str2開頭
96.name:Should Start With
以str2開頭
97.name:Sleep
暫停指定時間
時間可以是一個數字(單位是:秒)或時間字符串。時間字符串格式如'1 day 2 hours 3 minutes 4 seconds 5milliseconds' 或 '1d 2h 3m 4s 5ms'等。
98.name:Variable Should Exist
給定的變量在當前范圍內,期望存在
99.name:Variable Should Not Exist
給定的變量在當前范圍內,期望不存在
100.name:Wait Until Keyword Succeeds
等到指定的關鍵字成功或超時
======================
參考1:http://blog.chinaunix.net/uid-20393955-id-3074760.html
先學習這些吧!熟悉掌握這些API,讓其做更多的事,還有常用API(二)會後繼更新~
無想法就無成就!