天天看點

Robot Framework 自動化測試架構

Robot Framework 自動化測試架構

安裝配置

簡介

  • 一款python編寫的通用的自動化測試架構。
  • 具備良好的可擴充性。
  • 支援關鍵字驅動。
  • 可以同時測試多種類型的用戶端或者接口。
  • 可以進行分布式測試。
  • 主要用于驗收測試和驗收測試開發(ATDD)。

特點

  • 易于使用,采用表格式文法,統一測試用例格式
  • 重用性好,可以利用現有關鍵字來組合新關鍵字
  • 支援變量
  • 支援建立基于資料驅動的測試用例
  • 結果報告和日志采用HTML格式,易于閱讀
  • 提供标簽以分類和選擇将被執行的測試用例
  • 平台、應用無關
  • 功能全面,支援協定級接口的測試,GUI 界面的測試,資料庫的測試,移動App 的 測試,指令行測試等
  • 易于擴充,提供了簡單API,使用者可以自定義的基于Python或Java的測試庫
  • 易于內建,提供了指令行接口和基于XML的輸出檔案
  • 易于與版本管理內建(Jenkins)

基本架構

Robot Framework 自動化測試架構
步驟一

首先配置python的環境變量

安裝RobotFramework
pip install robotframework
           
安裝Ride
pip install wxPython==4.0.7.post2
pip install robotframework-ride==1.7.4.2
           
步驟二

安裝SeleniumLibrary庫

pip install robotframework-seleniumlibrary
           
安裝RequestsLibrary庫
pip install robotframework-requests
           
安裝DatabaseLibrary庫
pip install robotframework-databaselibrary
           

中文支援問題!!!

針對1.7.4.X版本
修改testrunnerplugin.py檔案

此檔案位于…\Lib\site-packages\robotide\contrib\testrunner目錄下

def _AppendText(self, textctrl, string, source="stdout", enc=True):  if not self.panel or not textctrl:
return
textctrl.update_scroll_width(string)
# we need this information to decide whether to autoscroll or not  new_text_start = textctrl.GetLength()
……
if PY2:
……
else:
textctrl.AppendTextRaw(bytes(string, encoding['SYSTEM']))  # DEBUG .encode('utf-8'))
# 注意修改此處的
SYSTEM為OUTPUT

           

啟動

指令行
python [PythonDir]\Scripts\ride.py
           
官網資訊https://robotframework.org/#libraries

Ride界面

Robot Framework 自動化測試架構

Ride測試報告

Robot Framework 自動化測試架構

UI自動化測試

步驟
  • 建立項目

    可以選擇檔案或目錄存儲。

    為了管理期間建議選擇目錄存儲。

  • 建立測試套件

    添加測試需要導入的庫。

    最好選用robot檔案格式存儲。

    點選Library按鈕,填寫導入SeleniumLibrary庫。

    注意庫導入後,名字黑色表示庫導入正确,名字變色為紅色表示庫導入失敗。

  • 建立測試用例

    在表格中填寫代碼。

    依照關鍵字的方式編寫代碼。

  • 儲存
  • 勾選要執行的測試用例
  • 點選Start運作按鈕

代碼

Robot Framework 自動化測試架構

接口自動化測試

步驟
  • 建立項目(已經存在可以不必建立新的)
  • 建立測試套件

    導入RequestsLibrary庫

  • 建立測試用例
  • 儲存
  • 勾選要執行的測試用例
  • 點選Start運作按鈕

代碼

Robot Framework 自動化測試架構

進階用法

Tags
測試用例可以标記多個Tag
可以設定按照Tag來執行或者跳過執行測試用例
Log
列印日志,相當于python中的print
Set Variable
設定變量
${SPACE} 和 ${EMPTY}
分别用來表示空格和空字元串
Catenate
連接配接對象拼接字元串
Create List
建立一個清單
Create Dictionary
建立一個字典
Get Time
獲得系統目前時間
Sleep
休眠一段時間
Evaluate
執行python提供的方法

           

If條件判斷

Run Keyword If

ELSE IF

ELSE

注意ELSE前的…不能丢掉

字元串、以及布爾值比較需要加引号,而數值不需要加引号。比如:’${username}’ == ‘admin’ 或‘${condition}’ == ‘${true}’

執行多語句需要使用Run Keywords 關鍵字1 值1 AND 關鍵字2 值2

Robot Framework 自動化測試架構
Robot Framework 自動化測試架構

For循環(1.7.4.X版本)

FOR……END定義循環

IN關鍵字定義一般循環

IN RANGE關鍵字可以指定循環範圍

\用于定義循環體語句的行首關鍵字

Robot Framework 自動化測試架構
Robot Framework 自動化測試架構

自定義庫

簡單方式

Python安裝目錄下…\Lib\site-packages目錄下

建立一個CalculatorLibrary.py的檔案(注意檔案名和類名一緻,首字母大寫)

class CalculatorLibrary:

def add(self, a, b):  
    
return a + b

           
進階方式(1.7.4.X版本)

Python安裝目錄下…\Lib\site-packages目錄下

建立一個XXX.pth的檔案,其中寫自定義庫所在的項目位置

E:\Documents\work\Script\RF\project_name
           

在項目路徑下建立檔案夾CalculatorLibrary ,自定義庫的名字calculator.py

建立自定義類CalculatorLibrary,其中GLOBAL的取值還有TEST CASE,TEST SUITE。

class CalculatorLibrary:  ROBOT_LIBRARY_SCOPE = 'GLOBAL'  ROBOT_LIBRARY_VERSION = '0.01'
def add(self, a, b):  
return a + b
def sub(self, a, b):
return a - b
def multi(self, a, b):  
return a * b
def div(self, a, b):
return a / b
           

在項目路徑下的CalculatorLibrary檔案夾中

建立 _init_ .py

from CalculatorLibrary.calculator import CalculatorLibrary
def sub(self, a, b):
return a - b
def multi(self, a, b):  
return a * b
def div(self, a, b):
return a / b
           

在項目路徑下的CalculatorLibrary檔案夾中

建立_init_ .py

from CalculatorLibrary.calculator import CalculatorLibrary