概述
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=//*[