【原創】Robotframework 簡介


概述

Robot Framework是一個通用的關鍵字驅動自動化測試框架。

測試用例以HTML,純文本或TSV(制表符分隔的一系列值)文件存儲。通過測試庫中實現的關鍵字驅動被測軟件。Robot Framework靈活且易於擴展。它非常適合測試有不同接口的復雜軟件:用戶接口、命令行,Web服務,專有的編程接口等。 Robot Framework是開源軟件,安裝包和源代碼等文檔可通過http://robotframework.org獲取。

安裝

http://www.cnblogs.com/maple42/p/5076328.html

最好通過pip安裝,可以自動下載依賴與匹配版本

注意事項

1.32位與64位要分清

2.selenium2library 庫的安裝 有依賴庫 selenium 與 decorator,最好使用pip來安裝,因為pip 會自動安裝依賴庫。

3.如果是公司內網使用代理上網,RF無法識別IE的配置需要使用proxy.pac腳本實現代理上網

配置方法 IE》工具》Internet選項》連接》局域網設置

勾選使用自動配置腳本 file://c:/proxy.pac ------腳本存放路徑 注意斜杠

目前robotFramework 支持的測試庫

https://github.com/robotframework
https://github.com/bulkan/robotframework‐difflibrary
http://robotframework.org/#test‐libraries https://code.google.com/p/robotframework/wiki/TestLibraries

標准測試庫:

• BuiltIn
• OperatingSystem
• Screenshot
• Telnet
• Collections
• String
• Dialogs
• Remote
• XML (new in RF 2.7.4)

外部測試庫:

This is a list of publicly available test libraries that can be used with Robot Framework but need to be installed separately. Please refer to the individual project pages for information about installing and using them.

• SeleniumLibrary - A web testing library that uses popular Selenium tool internally.
• Selenium2Library - A drop-in-replacement for SeleniumLibrary using newer Selenium 2 WebDriver API.
• Selenium2Library Java port - Java implementation of Se2Lib. Compatible with Jython 2.5.
• watir-robot - A web testing library that uses popular Watir tool via the remote library interface.
• WatinLibrary - A web testing library that uses Watin tool (a .NET port of Watir) via the remote library interface.
• SwingLibrary - A Swing GUI testing library.
• EclipseLibrary - A library for testing Eclipse RCP applications using SWT widgets.
• AutoItLibrary - Windows GUI testing library that uses AutoIt freeware tool as a driver.
• DatabaseLibrary (Java) - A test library that provides common functionality for testing database contents. Implemented using Java so works only with Jython.
• DatabaseLibrary (Python) - Another library for database testing. Implemented with Python and works also on Jython.
• SSHLibrary - A test library that enables SSH and SFTP.
• HTTP test library using livetest
• HTTP test library using Requests
• SudsLibrary - library for testing SOAP-based web services
• AndroidLibrary - Library for Android testing that uses Calabash Android internally.
• IOSLibrary - Library for iOS testing that uses Calabash iOS Server internally.
• Rammbock - Generic network protocol test library.
• How-To: Sikuli and Robot Framework Integration - This is not really a library but these instructions explain how to integrate Sikuli tool with Robot Framework Found these also, might be useful to add them as well (to the external libraries):
• PhantomJS and Zombie.js libraries (similar to SeleniumLibrary? but headless)
• Generic Network Protocol test library
• REST specific HTTP library
• SoapUI library (similar approach to Sikuli lib, but with SoapUI)
• IOS (Iphone/Ipad) test automation library (uses Calabash IOS Server to drive IOS device)
• Android test automation library (uses Calabash Android Server to drive android device)
• ADB (android) library
• VMWare and HyperV virtual machine libraries
• Library supporting debugging in robot framework
• Diff style file comparison library
• Closure webdriver library
• Sikuli integration libraries here and here (The how-to is helpful, but not exactly a library. Checkout here for code based on the how-to though)
• email library
• Image Comparison library
• HTML Check library (doesn't look maintained though, for shame Janne )

關鍵字

測試用例由關鍵字創建。關鍵字有三個來源:總是可用的內置關鍵字,來自導入測試庫的庫關鍵字和用戶在創建測試用例時使用表格語法創建的用戶關鍵字。都可以在ride下通過F5來查看

內置關鍵字

一些通用的關鍵字,如獲取時間(Get Time)和應該等於(Should Be Equal)。這些關鍵字來自內建測試庫BuiltIn。通過它的文檔,可以看到一個完整可用關鍵字列表。

庫關鍵字

所有最低級的關鍵字在測試庫中定義,這些測試庫使用標准編程語言實現。 Robot Framework帶有一些庫,包括OperatingSystem:支持常見的操作系統功能,Screenshot截圖庫。

除了這些標准庫,還有其他開源項目,如SeleniumLibrary。如果沒有合適的可用的庫也很容易實現自己的庫。
使用測試庫的關鍵字之前需要導入,它必須考慮到使用。本文件中的測試需要導入標准OperatingSystem的庫中的關鍵字(如Remove File)以及從自定義庫LoginLibrary(如Attempt to login with credentials)。

用戶關鍵字

Robot Framework最強大的功能之一是能夠方便地從其他關鍵字創建新的更高級別的關鍵字。創建用戶定義的關鍵字(簡稱為用戶關鍵字)的語法類似於創建測試用例。
用戶關鍵字可以包括其他用戶定關鍵字,內置關鍵字,或庫關鍵字。正如下面例子可以看到,用戶關鍵字可以帶參數。他們也可以返回值,甚至包含FOR循環。用戶關鍵字可以生成可重用的通用動作序列。用戶關鍵字提高測試的可讀性,並在不同情況下使用適當的抽象層次

找一個項目舉例

*** Settings ***
Library           Selenium2Library
Library           DatabaseLibrary

*** Variables ***
${user} 13917745687 # 默認登陸用戶 *** Test Cases *** 登陸 海淘登陸 ${user} [Teardown] close all browsers 個人中心-消息中心 海淘登陸 ${user} 點擊元素 //*[@id="user-center"] 點擊元素 //*[@class="sf-b2c-mall-center-leftside"]/ul/li/a wait until page contains 您暫未收到任何消息~ [Teardown] close all browsers 個人中心-我的收藏 海淘登陸 ${user} 點擊元素 //*[@id="user-center"] 點擊元素 //*[@class="sf-b2c-mall-center-leftside"]/ul/li[2]/a wait until page contains 全部 [Teardown] close all browsers 個人中心-我的訂單 海淘登陸 ${user} 點擊元素 //*[@class="sf-b2c-mall-center-leftside"]/ul/li[3]/a wait until page contains 所有訂單 [Teardown] close all browsers 個人中心-我的優惠券 海淘登陸 ${user} 點擊元素 //*[@id="user-center"] 點擊元素 //*[@class="sf-b2c-mall-center-leftside"]/ul/li[4]/a wait until page contains 未使用 [Teardown] close all browsers 個人中心-賬戶管理 海淘賬戶管理 [Teardown] close all browsers 個人中心-賬戶管理-修改昵稱性別 海淘賬戶管理 ${rand} 隨機數 點擊元素 //*[@id="user-info-modify-btn"] wait until keyword succeeds 2 min 5 sec input text //*[@id="nickname"] ${rand} Comment wait until keyword succeeds 2 min 5 sec input text //*[@id="user-name"] ${user} select from list //*[@id="select-sex"] FEMALE 點擊元素 //*[@id="user-info-confirm-btn"] [Teardown] close all browsers 個人中心-收貨地址 海淘登陸 ${user} 點擊元素 //*[@id="user-center"] 點擊元素 //*[@class="sf-b2c-mall-center-leftside"]/ul/li[6]/a wait until page contains 添加收貨地址 [Teardown] close all browsers 個人中心-積分管理 積分管理 [Teardown] close all browsers 個人中心-積分管理-收入 積分管理 點擊元素 //*[@class="integral-tab-c1 fl"]/ul/li[2]/a #/html/body/div[2]/div[2]/div/div/div[2]/div/div[1]/div[2]/div[1]/ul/li[2]/a [Teardown] close all browsers 個人中心-積分管理-支出 積分管理 點擊元素 //*[@class="integral-tab-c1 fl"]/ul/li[3]/a #/html/body/div[2]/div[2]/div/div/div[2]/div/div[1]/div[2]/div[1]/ul/li[2]/a [Teardown] close all browsers 個人中心-邀請有禮 海淘登陸 ${user} 點擊元素 //*[@id="user-center"] 點擊元素 //*[@class="sf-b2c-mall-center-leftside"]/ul/li[8]/a wait until page contains 邀請有禮 [Teardown] close all browsers *** Keywords *** 帶狀態登錄 [Arguments] ${url} open browser ${url} ff ff_profile_dir=${config} Maximize Browser Window sleep 1 重試 [Arguments] ${key} ${args1}=none wait until keyword succeeds 2 min 7 s ${key} ${args1} 等待元素 [Arguments] ${locator} # 元素定位xpath、CSS、ID等 wait until page contains element ${locator} 點擊元素 [Arguments] ${locator} 重試 等待元素 ${locator} 重試 click element ${locator} 填寫 [Arguments] ${locator} ${content}=none Selenium2Library.input text ${locator} ${content} 無視錯誤執行 [Arguments] ${key} ${args1}=none Run Keyword And Ignore Error ${key} ${args1} 隨機數 ${num} evaluate random.randint(10000000,99999999) random [Return] ${num} 連接海豚庫 Connect To Database Using Custom Params pymysql database='CSCOnline', user='csc_a', password='dp!@DkbH3ABUv', host='10.1.77.69', port=3306 海豚登錄 清理海豚登錄狀態 open browser http://csc-online-kefu-web01.beta/login.jsp Maximize Browser Window input text xpath=//*[@id="j-login-staffno"] ${global-staff-no} input password xpath=//*[@id="j-login-password"] 123456 wait until element is visible xpath=//*[@id="j-login-button"] 點擊元素 xpath=//*[@id="j-login-button"] run keyword and ignore error wait until page contains element //*[@id="j-button-relogin"] 3 run keyword and ignore error click element //*[@id="j-button-relogin"] wait until page contains 大眾點評 30 sleep 1 更改接線人數 log 修改最大接線人數 click element xpath=//*[@id="j-button-reception"] sleep 2 click element xpath=//*[@id="j-ul-reception"]/li[3]/a 提取數據 [Arguments] ${data} ${out} evaluate ${data}[0][0] [Return] ${out} # 提取出的內容 清理海豚登錄狀態 連接海豚庫 execute sql string Delete FROM icc_staff_online; Comment Delete All Rows From Table icc_staff_online sleep 2 后台sso open browser https://sso.51ping.com/login?TARGET=http%3A%2F%2Fbackend.csc.dp%3A8080%2F ff Maximize Browser Window run keyword and ignore error input text //*[@id="username"] 0018169 run keyword and ignore error input password //*[@id="password"] Ceshi123 run keyword and ignore error click element //*[@id="fm1"]/div[5]/div[3]/input[4] sleep 1 run keyword and ignore error reload page run keyword and ignore error wait until page contains 后台管理 20 展開后台 wait until element is visible xpath=//*[@id="sidebar"]/ul/li[1]/a/span 20 click element xpath=//*[@id="sidebar"]/ul/li[1]/a/span sleep 1 新增部門 后台sso 展開后台 點擊元素 //*[@id="sidebar"]/ul/li[1]/ul/li[8]/a wait until element is visible xpath=//*[@id="add_grid-table"]/div/span 30 點擊元素 xpath=//*[@id="add_grid-table"]/div/span 點擊元素 xpath=//*[@id="editmodgrid-table"] select from list by value //*[@id="deptSelect2"] 10 ${dept} evaluate random.randint(1111111,9999999) random input text xpath=//*[@id="name"] ${dept} sleep 0.5 log 提交新部門信息 點擊元素 xpath=//*[@id="sData"] sleep 1 連接海豚庫 ${data-check} query select id from CSC_Department where name='${dept}' should not be empty ${data-check} log 數據入庫成功 [Return] ${dept} # 部門名稱 新增技能組 后台sso 展開后台 點擊元素 //*[@id="sidebar"]/ul/li[1]/ul/li[7]/a Comment wait until element is visible //*[@id="select-btn"]/button[1] Comment click button //*[@id="select-btn"]/button[1] log 新增技能組 wait until element is visible //*[@id="add_grid-table"]/div/span 點擊元素 //*[@id="add_grid-table"]/div/span ${skill-code} evaluate random.randint(10000,99999) random sleep 0.6 log 新技能組名稱 wait until element is visible //*[@id="name"] input text //*[@id="name"] ${skill-code} wait until element is visible //*[@id="code"] ${skill-code} log 選擇部門為商服 select from list by value //*[@id="deptSelect"] 20 #10user \ \ 20shop \ \ 30sales sleep 0.6 log 提交 wait until element is visible //*[@id="sData"]/i 點擊元素 //*[@id="sData"]/i log 提交成功 sleep 1 [Return] ${skill-code} # 技能組 新增客服 后台sso 展開后台 log 打開員工頁 點擊元素 //*[@id="sidebar"]/ul/li[1]/ul/li[6]/a 點擊元素 xpath=//*[@id="add_grid-table"]/div/span sleep 0.5 ${staff-no} evaluate random.randint(1111111,9999999) random select checkbox //*[@id="staffNOEditable"] log 輸入客服名稱 wait until element is visible xpath=//*[@id="staffName"] input text xpath=//*[@id="staffName"] ${staff-no} log 輸入工號 wait until element is visible xpath=//*[@id="staffNO"] input text xpath=//*[@id="staffNO"] ${staff-no} log 輸入密碼 wait until element is visible xpath=//*[@id="password"] input password xpath=//*[@id="password"] 123456 log 選擇技能組 @{skill-code} set variable 2 3 8 sleep 1 Comment click element xpath=//*[@id="tr_skillIdListStr"]/td[2]/div/button select from list xpath=//*[@id="deptSelect"] 10 click element //*[@id="tr_skillIdListStr"]/td[2]/div/button log 最小接線人數 wait until element is visible xpath=//*[@id="minReceptionNum"] input text xpath=//*[@id="minReceptionNum"] 5 log 最大接線人數 wait until element is visible xpath=//*[@id="maxReceptionNum"] input text xpath=//*[@id="maxReceptionNum"] 10 log 輸入昵稱 wait until element is visible xpath=//*[@id="nickName"] input text xpath=//*[@id="nickName"] ${staff-no} click element xpath=//*[@id="sData"] [Return] ${staff-no} # 員工工號 海淘登陸 [Arguments] ${user} ${bro} open browser http://www.fengqu.com/index.html ff Maximize Browser Window 點擊元素 //*[@id="user-login"] select frame //*[@class="register"]/div[2]/iframe wait until keyword succeeds 2 min 5 sec input text //*[@id="user-name"] ${user} wait until keyword succeeds 2 min 5 sec input password //*[@id="user-pwd"] yiyi521 Comment input text //*[@id="user-name"] ${user} Comment input password //*[@id="user-pwd"] yiyi521 點擊元素 //*[@id="gotologin"] unselect frame wait until page contains 歡迎回來 [Return] ${bro} # 登陸頁面driver 海淘賬戶管理 海淘登陸 ${user} 點擊元素 //*[@id="user-center"] 點擊元素 //*[@class="sf-b2c-mall-center-leftside"]/ul/li[5]/a wait until page contains 基本資料 積分管理 海淘登陸 ${user} 點擊元素 //*[@id="user-center"] 點擊元素 //*[@class="sf-b2c-mall-center-leftside"]/ul/li[7]/a wait until page contains 我的積分

setting中的內容為三方庫,可以使用開源的也可以自己編寫,如何自己編寫自己的庫見下文

*** Settings *** Library Selenium2Library Library DatabaseLibrary

Variables中放的是變量,維度可以是project、suite、case,類型可以是單獨變量或者list

*** Variables *** ${user} 13917745687 # 默認登陸用戶

Test Cases中就是執行單元了,可以直接使用內置庫、三方庫、用戶關鍵字

*** Test Cases *** 登陸 海淘登陸 ${user} [Teardown] close all browsers 個人中心-消息中心 海淘登陸 ${user} #用戶關鍵字 點擊元素 //*[@id="user-center"] 點擊元素 //*[@class="sf-b2c-mall-center-leftside"]/ul/li/a wait until page contains 您暫未收到任何消息~ #三方庫方法 [Teardown] close all browsers

Keywords用戶關鍵字,對於復用率高、可抽離公共熟悉的操作或者方法可以通過Keywords來將這些邏輯抽象出來,Keywords可以定義入參與返回值,這樣可使得case中操作的連貫,也更易於case的維護

*** Keywords ***
帶狀態登錄
    [Arguments]    ${url} open browser ${url} ff ff_profile_dir=${config} Maximize Browser Window sleep 1 重試 [Arguments] ${key} ${args1}=none wait until keyword succeeds 2 min 7 s ${key} ${args1} 等待元素 [Arguments] ${locator} # 元素定位xpath、CSS、ID等 wait until page contains element ${locator} 點擊元素 [Arguments] ${locator} 重試 等待元素 ${locator} 重試 click element ${locator} 填寫 [Arguments] ${locator} ${content}=none Selenium2Library.input text ${locator} ${content} 無視錯誤執行 [Arguments] ${key} ${args1}=none Run Keyword And Ignore Error ${key} ${args1}

組織測試用例

Test suites

Robot Framework中測試用例的集合被稱為test suites。每個包含測試用例的文件形成了一個test suites。

Setup 和 Teardown

如果想在每個測試執行之前和之后執行某些操作,可以使用Test Setup和Test Teardown:

Setting Value Test Setup Clear Login Database #清理連接的數據庫 Test Teardown

同樣,也可以使用Suite Setup 和Suite Teardown來指定整形整個test suite之前和之后要執行的操作。

使用標簽

Robot Framework允許給測試用例的設置標簽。通過Default Tags或Force Tags可以所有的測試用例打上標簽。也可以給單個測試用例打上標簽。

在測試報告中可以看到測試及其標簽以及基於標簽的統計。標簽有很多其他用途,最重要的是選擇執行測試:
pybot --include smoke quickstart.html
pybot --exclude database quickstart.html

編寫測試庫

CombineLibrary.py

import clr
clr.AddReferenceToFileAndPath('Combine.dll') #include full path to Dll if required from Test import Combine def add_two_numbers(num1, num2): try: intNum1 = int(num1) intNum2 = int(num2) except: raise Exception("Values must be integer numbers!") cmb = Combine() total = cmb.AddNumbers(intNum1, intNum2) return str(total) def add_two_words(word1, word2): cmb = Combine() return cmb.AddStrings(word1, word2)

測試報告

case運行結束后會生成三個文件:Output.xml、Log.html、Report.html

我們重點查看Log.html和Report.html ,Log.html更關注腳本的執行過程的記錄,Report.html更關注腳本的執行結果的展示。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM