Chapter1. 初識 RF
1. 功能
支持 Web 界面測試、Web 接口測試、GUI 測試、多終端測試
2. 框架
3. RF 提供的相關插件
- Eclipse plugin: https://github.com/NitorCreations/RobotFramework-EclipseIDE/wiki
- Robot Plugin for InteliJ IDEA: http://plugins.jetbrains.com/plugin/7430-robot-plugin
- Jenins plugin: https://wiki.jenkins-ci.org/display/JENKINS/Robot+Framework+Plugin
- Maven plugin: http://robotframework.com/MavenPlugin/
- Ant task: http://code.google.com/p/robotframework-ant
- Pabot: 1)pip install -U robotframework-pabot;2)https://github.com/mkorpela/pabot
- Atom plugin: https://atom.io/packages/language-robot-framework
4. 常用的使用
用途 | 操作 | 說明 |
快速查詢關鍵字的API說明 | 選中關鍵字 + (Ctrl +Alt) | |
快速補全關鍵字 | 關鍵字前綴 + (Ctrl + Alt + BackSpace) | |
創建列表 | @{list1} Create List hello rebot fremawork Log ${list1} |
1)"@"定義,$打印;2)中間用 tab |
創建字典 | &{Dict1} Create Dictionary a=hello b=robotframework Log Many &{Dict1} |
& 定義字典 |
拼接字符 | ${var2} Catenate Hello Robot | |
Exit For Loop 退出循環 |
1) Run KeyWord If ${index}==3 Exit for loop 2) Exit For Loop If ${index}==3 |
|
Return From KeyWord vs Return From KeyWord If 返回及條件返回 |
常用在 用戶自定義關鍵字中。參照下面的例子 | |
Should Match vs Should Be Equal |
Test_ShouldEqual Test_ShouldMatch |
Should Match:常用於字符串比較 Should Be Equal 可用於多種形式的比較 |
Built In 庫其他關鍵字 | 1)字符串中字符或字符串出現的次數:Get Count 2)用例中導入 Import Library / Import Resource / Import Variables 3)Run Keywords:執行多個關鍵字 4)Run Keyword And Return:執行關鍵字並返回 5)Fail:直接判斷失敗 6)Wait Until Keyword Succeeds:等待的時間內循環,超過時間后失敗還是失敗 7)Pass Execution:跳過當前測試,后面的都不執行 |
Test Wait \Variable Should Exist ${value}
Test PassExecution Test Fail
|
----------- 用戶自定義關鍵字 ----------- *** Keywords *** Example Return From Keyword [Arguments] ${valueReturn} @{list2} Create List 1 2 3 4 FOR ${value} IN @{list2} Return From Keyword If '${value}'=='${valueReturn}' ${value} Log ${value} END Return From KeyWord ${value} ----------- Test Case -------------- *** Settings *** Resource Custom_Keyword.txt *** Test Cases *** Test1.2 ${result} Example Return From Keyword 2 Log ${result} ========= Output ========= Starting test: RIDE Lesson.Chapter1.Test1.2 20211108 13:56:40.911 : INFO : @{list2} = [ 1 | 2 | 3 | 4 ] 20211108 13:56:40.914 : INFO : 1 20211108 13:56:40.915 : INFO : Returning from the enclosing user keyword. 20211108 13:56:40.916 : INFO : ${result} = 2 20211108 13:56:40.917 : INFO : 2 Ending test: RIDE Lesson.Chapter1.Test1.2
Chapter2. RF對數據庫的操作
1. DatabaseLibrary庫
1)庫導入
- pip install -U robotframework-databaselibrary
- pip install PyMySQL
- RIDE 導入 DatabaseLibrary
2)相關操作
關鍵字 | 說明 |
Connect To Database | 數據庫連接 |
Connect To Database Using Custom Params | 自定義參數連接 |
Query | 數據庫查詢 |
Execute Sql String | 執行 sql 語句 |
Execute Sql Script | 執行 sql 腳本 |
Row Count Row Count Is Equal to / Greater Than / Less Than X |
獲取 sql 查詢返回的記錄數 |
Disconnect From Database | 斷開數據庫連接 |
2. MongoDBLibrary 庫
1)庫導入
- 連接:https://github.com/iPlantCollaborativeOpenSource/Robotframework-MongoDB-Library,查看說明
- RIDE 導入庫 MongoDBLibrary
2)相關操作
關鍵字 | 說明 |
Connect To Mongodb | 連接到MongoDB數據庫 |
Get Mongodb Databases | 獲取當前MongoDB下的所有在用的數據庫 |
Get Mongodb Collections | 獲取到指定MongoDB數據庫下的所有Collection |
Save MongoDB Records | 向指定的Collection中保存插入的記錄 |
Retrieve All Mongodb Records | 獲取指定Collection下的所有數據記錄 |
Update Many Mongodb Records | 更新Collection中的數據記錄 |
Remove Mongodb Records | 刪除指定Collection中的數據記錄 |
Disconnect From Mongodb | 斷開已經建立的MongoDB數據庫連接 |
Chapter 3. HTTP 接口自動化測試
庫 | 關鍵字 | 用途 | 說明 |
HttpLibrary.HTTP | 1) pip install robotframework-httplibrary 2)RIDE 導入 HttpLibrary.HTTP |
||
1) Create Http Context | 創建一個 HTTP 調用的環境 Create Http Context host=www.baidu.com:80 scheme=http |
版本兼容問題 | |
Get / Post | get/post 請求 | ||
Get / Set Response Body | 獲取 / 請求傳參 body | ||
Get Response Status | 獲取 status | ||
Get /Set Response Header | 獲取 / 請求傳參 header | ||
Follow Response | 重定向請求 | ||
RequestLibrary | 1)pip install requests 2)pip install robotframework-request 3)RIDE導入 RequestLibrary |
||
Create Session | 創建session 連接 | ||
Get Request -> Get On Session | get 請求 | ||
Post Request -> Post On Session | post 請求 | ||
RESTinstance | 1)pip install RESTinstance 2)RIDE導入 RESTinstance |
||
Post | RESTful 形式的 post 請求 | ||
Get | RESTful 形式的 get 請求 | ||
Put | RESTful 形式的 put 請求 |
Chapter4. 手機自動化測試
1. Appium第三方自動化框架
- IOS:Apple 的 UIAutomation
- Android:Google 的 UiAutomator
2. Appium 庫的使用
關鍵字 | 解釋 |
appium 庫安裝 |
1)pip install robotframework-appiumLibrary 2)導入 AppiumLibrary |
Open Application http://localhost:4723/wd/hub platformName=Android platformVersion=7.1.2 deviceName=3141a881 \app=E:\\TestData\\QiYunZhiCe\\qyzc_3.5.23.apk \skipServerInstallation=${True} newCommandTimeout=30000 |
打開APP |
Input Text | 輸入文本 |
Clear Text | 清空文本 |
Click Button | 點擊按鈕 |
Click Element | 點擊元素 |
Click A Point | 點擊某個點 |
Click Element At Coordinates | 通過具體的坐標點擊 element |
Get Element Location | 獲取 element 的 x,y 坐標 |
Get Current Context | 獲取當前的上下文 |
Get Contexts | 獲取所有的上下文 |
Switch To Context | 頁面切換,通過 Get Contexts,然后切換到指定的 Context |
Get Elements | 通過 locator 獲取匹配到的所有元素 |
Get Element Attribute | 通過元素獲取 屬性值 |
Get /Set Network Connection Status | 獲取/設置 手機的網絡連接狀態 |
Element Attribute Should Match Element Name Should Be Element Value Should Be |
元素屬性判斷 |
Chapter 5. Web 自動化測試
關鍵詞 | 說明 |
導入 | 1)pip install robotframework-seleniumlibrary 2)下載瀏覽器對應的 driver 3)RIDE 導入 Selenium2Library |
Open/Close Browser | 打開/關閉 瀏覽器 |
Input Text | 輸入文本 |
Click Button | 點擊按鈕 |
Click Element | 點擊元素 |
Click Link | 點擊鏈接 |
Get/Add/Delete Cookie | 向本地瀏覽器,獲取/添加/刪除 Cookie緩存 |
Get All Links | 獲取所有頁面上所有的 href 鏈接的元素對應的 id,無id時,輸出空字符串 |
Choose File | 選擇文件(多用於上傳文件) |
Get Text | 獲取文本內容 |
Get Title | 獲取瀏覽器的 title |
Get Window Titles | 獲取當前已經打開的瀏覽器窗口所有的 Title |
Get Value | 獲取元素的 value 屬性 |
Get WebElements / Get WebElement | 獲取 WebElement 對象 所有的列表/匹配到的第一個對象 |
Go Back / Go to | 返回 / 跳轉 |
Get List Items | 獲取下拉列表的所有下拉選項 |
Get Selected List Value | 獲取頁面選中的一個下拉列表的 Value 值 |
Select From List | 從指定的下拉列表中選擇指定的下拉列表選項 |
Selenium2Library 其他關鍵字 | P120-128 |
Chapter 6. 編寫自定義的 Robot Framework Lib
6.1.1 使用 Python 編寫的本地庫
6.1.2 遠程庫 Remote
6.2 使用 Java 編寫的
1)DatabaseLibrary
RoboFramework 官網,https://robotframework.org/#resources,1)下載 java版本的 Database Library (Java),2)解壓,3)執行“mvn clean install -Dmaven.test.skip=true”,4)在 target 目錄下,生成 robotframework-dblibrary-3.2.1-SNAPSHOT.jar
mysql 官網下載 jar 包,https://www.cnblogs.com/it-mh/p/11205866.html
Chapter 8. 自動化框架的設計
8.1 Jenkins
1. 在主節點上創建自動化部署
- 源碼 SVN,設置RF源碼的 SVN地址
- 構建,選擇 batch file,
* pybot.bat 后面的命令行部分,可以參考自 RIDE 執行后生成的 命令輸出
call pybot.bat -L DEBUG --argumentfile C:\Users\ADMINI~1\AppData\Local\Temp\2\RIDE2qk6q9zc.d\argfile.txt --listener "c:\program files (x86)\python37\lib\site-packages\robotide\contrib\testrunner\TestRunnerAgent.py:49731:False" C:\Users\jenkins\AppData\Local\Jenkins\.jenkins\workspace\RobotFramework自動化測試\RIDE_Lesson exit 0