Robot Framework - 一些練習
01 - 安裝Robot Framework TA環境
根據系統請選擇對應的版本包來安裝,下面是以Win7-64bit系統為例,來說明如何搭建一個可以運行練習三test case的RF TA環境。
1)首先,要安裝好版本對應的python環境,
C:\Users\guowli>python Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
在命令行執行python,顯示如上信息,則表示python安裝成功。
添加環境變量到系統Path:”C:\Python27\;C:\Python27\Lib\site-packages;C:\Python27\Scripts”
2)安裝Setuptools和pip,這兩個是安裝python擴展庫的工具,可以在網絡連通的情況下,簡單快捷的安裝所需要的擴展庫。
3)安裝Robot Framework。可以手工下載源碼安裝,也可以利用Pip工具來完成robotframework的安裝,只需在網絡連通的情況下,執行如下命令:”pip install robotframework”
安裝完成後,驗證一下:
C:\Python27\Scripts>pybot --version Robot Framework 2.8.5 (Python 2.7.8 on win32)
4)安裝WxPython和Robot Framework IDE(RIDE)。WxPython是RIDE正常運行所必須依賴的,要先安裝,否則在沒有WxPython情況下,運行RIDE會出現如下提示:
註意:wxPython必須為2.8.12.1的版本
02 - 創建一個簡單的測試用例
Test Case : Invalid and Valid login via SSH 1- Install the test libraries (SSH Library)請按照如下順序安裝相應library,否則很可能安裝失敗。
- PyCrypto:Website of Michael Foord. Python programming articles, projects and technical blog.
- ecdsa : ECDSA cryptographic signature library (pure python)
- paramiko : SSH2 protocol library
- robotframework-sshlibrary : Robot Framework test library for SSH and SFTP
*** Settings *** Library SSHLibrary *** Test Cases *** SSHValidLogin Open Connection 10.68.75.111 22 Loginmcadmin testsc Writedf -h Readdelay=0.5s Start Command pwd ${pwd}Read Command Output Should Be Equal ${pwd}/home/mcadmin Close Connection
3- 獲取幫助信息的方法:
- 鼠標:箭頭單擊相應關鍵字或參數等, 所指區域背景色會變換為黃色,並顯示簡要信息
- Ctrl鍵:選定關鍵字後,按Ctrl鍵會顯示該關鍵字的詳細信息。
- F5鍵:在RIDE界面,按F5鍵 出現”Search Keywords”窗口,可根據需求查找詳細信息。也可根據source類別,查看某test library的全部關鍵字信息
03 - 了解Robot Framework標準庫
http://robotframework.org/#test-libraries
BuiltIn : Contains generic often needed keywords. Imported automatically and thus always available.
An always available standard library with often needed keywords.
BuiltIn provides a set of generic keywords needed often.
It is imported automatically and thus always available.
http://robotframework.org/robotframework/latest/libraries/BuiltIn.html
OperatingSystem : Enables performing various operating system related tasks.
A test library providing keywords for OS related tasks.
OperatingSystem enables various operating system related tasks to be performed in the system where Robot Framework is running.
http://robotframework.org/robotframework/latest/libraries/OperatingSystem.html
Collections ------ Contains keywords for handling lists and dictionaries. DateTime ------ Supports creating and verifying date and time values as well as calculations between them. Dialogs ------ Supports pausing the test execution and getting input from users. Process ------ Supports executing processes in the system. Remote ------ Special library acting as a proxy between other libraries running different machines or processes. Screenshot ------ Provides keywords to capture and store screenshots of the desktop. String ------ Library for manipulating strings and verifying their contents. Telnet ------ Supports connecting to Telnet servers and executing commands on the opened connections. XML ------ Library for verifying and modifying XML documents.04 - 創建連接數據庫的測試用例
測試用例:Robot Framework連接Database,並進行簡單操作;
- 較完整的case結構:包含測試集 、測試用例、變量、關鍵字、資源文件等
- 標準庫及擴展庫的安裝引用
- 可重用的高層關鍵字
- 資源文件包含變量、關鍵字
- 資源文件的引用
- 根據相關日誌調試,並使之測試通過
1.安裝Robot Framework DatabaseLibrary
robotframework-databaselibrary有2個版本:Python和Java版
Python版下載地址:https://pypi.python.org/pypi/robotframework-databaselibrary/
下載並解壓縮,然後在命令行,cd到此文件夾下,然後python setup.py install
驗證安裝是否成功:把DatabaseLibrary加入Library內,然後按F5,看是否出現相關內容
2.安裝Python database applications
任何一個Robot Framework的Library基本上都是一個雙層結構:
- 外層,實現標準接口供Robot Framework調用;
- 內層,實現具體的功能,提供API供外層進行封裝。
Robot Framework DatabaseLibrary只是實現標準接口供Robot Framework調用,為了使它能夠真正還需要一個符合Python數據庫接口規範的庫文件。
兩部分缺一不可。
數據庫接口:http://wiki.python.org/moin/DatabaseInterfaces
根據需求選擇相應的DatabaseInterface下載安裝。
示例1:安裝MySQL數據庫的Database Interfaces
https://wiki.python.org/moin/MySQL
從如上網頁內容得知支持MySQL的DB API 2.0 Drivers有如下幾種:
- MySQL for Python
- PyMySQL
- mxODBC
- pyodbc 。。。。。。
請根據實際需求(License、Platforms、Python versions、。。。)來選擇使用
比如使用PyMySQL:https://github.com/petehunt/PyMySQL
下載包並解壓,命令行進入此目錄,運行“python setup.py install”即可;
示例2:安裝SQLServer的Database Interfaces
https://wiki.python.org/moin/SQL%20Server
從如上網頁內容得知支持SQLServer的DB API 2.0 Drivers有如下幾種
- adodbapi
- pymssql
- mxODBC
- pyodbc 。。。。。。
請根據實際需求(License、Platforms、Python versions、。。。)來選擇使用
比如使用pyodbc:http://code.google.com/p/pyodbc/downloads/list,下載對應的版本並安裝;
Pyodbc同時也支持連接mysql,但還需要安裝MySQL Connector:http://www.mysql.com/downloads/connector/odbc/
下載對應版本並安裝,打開ODBC數據源(控制面板-管理工具-ODBC數據源),點擊添加MySQL的ODBC。
安裝完成後可以通過pip命令參看包的信息。
3.Test Case示例
通過Robot Framework驗證MySQL數據庫的一些信息;
Test--Database_Library
*** Settings *** Library DatabaseLibrary Resource test--resource-databaselibrary.txt *** Test Cases *** test_DatabaseLibrary Connect To Database Using Custom Params pymysql host=‘${hostIP}‘, port=${port}, user=‘${user}‘, passwd=‘${password}‘, db=‘mysql‘ ${version} query select version() user-defined compare ${version} ((‘5.0.45‘,),) ${databases} query show databases user-defined compare ${databases} ((‘information_schema‘,), (‘mysql‘,), (‘test‘,)) ${count} Row Count show tables user-defined compare ${count} 17 Table Must Exist user Row Count Is Greater Than X select * from user 1 Disconnect From Database
test--resource-databaselibrary.txt
*** Variables *** ${hostIP} 10.68.75.203 ${port} 3306 ${user} root ${password} password *** Keywords *** user-defined compare [Arguments] ${arg1} ${arg2} [Documentation] convert and compare ${convert} Convert To String ${arg1} Should Be Equal ${convert} ${arg2}
4.問題處理
問題處理-1
運行testcase出現如下錯誤提示:
FAIL : OperationalError: (2003, ‘Can\‘t connect to MySQL server on \‘10.68.75.203\‘ ((1130, u"Host \‘10.140.1.177\‘ is not allowed to connect to this MySQL server"))‘)
解決方法
提示信息說明賬號沒有權限連接指定IP的主機,處理方法如下:
[root@localhost ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.0.45 Source distribution Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the buffer. mysql> GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘ password ‘ WITH GRANT OPTION; Query OK, 0 rows affected (0.00 sec) mysql>
命令說明
GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘password‘ WITH GRANT OPTION;
含義:允許root用戶使用password密碼從任何主機連接到mysql服務器。
GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘ 10.140.1.177‘ IDENTIFIED BY ‘password‘ WITH GRANT OPTION;
含義:只允許root用戶使用password密碼從ip為10.140.1.177的主機連接mysql服務器。
問題處理-2
運行testcase出現如下錯誤提示:
FAIL : OperationalError: (2003, ‘Can\‘t connect to MySQL server on \‘10.68.75.203\‘ ((1045, u"Access denied for user \‘root\‘@\‘10.140.1.177\‘ (using password: YES)"))‘)
解決方法:
確認登陸的用戶名及密碼是否正確,如需更改用戶名及密碼請按照如下步驟操作:
# 關閉mysql: /etc/init.d/mysql stop 或 service mysqld stop mysqld_safe --user=mysql --skip-grant-tables --skip-networking & mysql -u root mysql mysql> UPDATE user SET Password=PASSWORD(‘新定義的密碼‘) where USER=‘root‘; mysql> FLUSH PRIVILEGES; mysql> quit # 啟動mysql :/etc/init.d/mysql start 或 service mysqld start mysql -uroot -p 新定義的密碼
05 - 創建測試庫
參考信息:http://robotframework.org/robotframework/2.8.5/RobotFrameworkUserGuide.html#creating-test-libraries
1-確認要實現的內容
- 公式:(a+b)的a次方,a,b是正整數
- 有一網址(字符串),http://www.example.com?ip=192.187.111.198&code=12345&name=cat,想得到ip內容,即192.187.111.198
- (用戶名+10位隨機數+一個key)進行md5加密
2-編寫test library
測試庫以.py為後綴名,文件名與實現該測試庫的模塊名或者類名相同: <Class name>.py。
根據需要編寫testlibrary,定義相關的class及函數等。
返回值在 Python中采用 return 語句。
CreatNewLibrary.py:
# -*- coding: utf-8 -*- import re## re 模塊提供了一系列功能強大的正則表達式 (regular expression) 工具 import hashlib## Secure hashes and message digests import random## random 模塊包含許多隨機數生成器 import string## string 模塊提供了一些用於處理字符串類型的函數, class CreatNewLibrary(): def ABA(self,a,b): ‘‘‘ 公式:(a+b)的a次方,a,b是正整數 Example: | ${c} | aba | 2 | 3 | 結果${c}=25 ‘‘‘ return (int(a)+int(b))**int(a) def Find_IP(self,url): ‘‘‘ 從網址獲取IP地址 Example: | ${ip} | Find IP | http://www.example.com?ip=192.187.111.198&code=12345&name=cat | 結果${ip}=192.187.111.198 ‘‘‘ ip = re.findall(‘ip=(.*?)&‘,url,re.I) if(ip and ip[0]!=‘‘): return ip[0] else: return "沒有匹配到IP" def MD5_RandStr(self,username,key=‘UYTYUT-65HGj-IYR8760-YRJKKL9‘): ‘‘‘ (用戶名+一個key)進行MD5加密,key有默認的,也可以執行設定 Example : | ${string} | MD5 RandStr | tester | | ${string} | MD5 RandStr | tester | UYTYUT-65HGj-IYR8760-YRJKKL9 ‘‘‘ return hashlib.md5(username+key).hexdigest()
3-編譯調試test library
4- 導入及確認test library
因為將test library文件放在了testcase的同一目錄下,所以直接輸入文件名全稱即可。
如果test library文件在其他地方,要保證文件地址信息正確,或者以查找文件的方式導入也可。
正常的情況下,導入完成test library文件名稱在settings中應顯示為黑色。
按F5鍵,選擇對應的Source名稱,正常情況下,可以看到自定義庫和說定義的關鍵字信息。
5- 應用test library :Test Case 的編寫及調試
*** Settings *** Force Tags CreatNewBasicLibrary Library CreatNewLibrary.py *** Test Cases *** CreatNewBasicLibrary ${c}ABA 2 3 Log${c} ${ip}Find IPhttp://www.example.com?ip=192.187.111.198&code=12345&name=cat Log${ip} ${string1} MD5 RandStr tester Log${string1} ${string2} MD5 RandStr tester123456-654321-98765-56789 Log${string2}
執行測試用例
Robot Framework - 一些練習