python 3.6 + robotFramework自動化架構 環境搭建、學習筆記,
#################################################################
#author: 陳月白
#_blogs: http://www.cnblogs.com/chenyuebai/
#################################################################
一、環境搭建
概覽:win 7+ python 3.6 + pycharm + robotframework + IntelliBot
1.安裝相關的庫
依次安裝:
pip install robotframework
https://pypi.python.org/pypi/selenium/ #依賴包
https://pypi.python.org/pypi/robotframework-seleniumlibrary #依賴包
https://pypi.python.org/pypi/robotframework-selenium2library
2.pycharm
開發IDE, 略
3.IntelliBot pycharm開發外掛程式【文法高亮】
方法1:pycharm>File>setting>Plugins>Browse reponsitories
方法2:https://plugins.jetbrains.com/plugin/7386-intellibot 下載至本地
pycharm>File>setting>Plugins>install plugin from disk
4.pycharm配置執行器
File>setting>Tools>External tool,配置完成後即可在.robot檔案中選擇使用該執行器執行
Parameters:當前檔案路徑
Working directory:工作路徑,日誌、結果輸出等,如下:
註:log.html 執行日誌
output.xml 執行相關資訊的xml,可用於後續二次解析
report.xml 執行結果報表
二、使用cmd執行
運行一條用例:
pybot --test test_case test_suit.robot
運行指定檔案:
pybot test_suit.robot
運行目前的目錄下以.robot為尾碼名的測試檔案
pybot *.robot
運行當前testpath目錄下的所有用例
pybot testpath
三、案例指令碼(.robot檔案)基本文法
1.基本文法
*** Settings ***Library MyLib #匯入自訂的庫Library Selenium2Library*** Test Cases *** #第一行為固定格式,標識 #建議同python一致,使用tab縮排對齊(pycharm中設定tab=4空格),否則可能報執行失敗,報 "Test case contains no keywords"case1 helloworld #案例名 log chenyuebai first rfw case #log 相當於python的print,可在產生的log.xml中查看case 2 log #打異常日誌,支援多種層級 log test line 2 ERRORcase 3 varible ${myname} Set variable chen #定義變數 log my name is ${myname} #使用變數#case 4 use varible #變數範圍為其定義所在的案例(case3),否則報"Variable '${myname}' not found."# log ${myname}case 5 Catenate ${hi1} Catenate hello world #定義變數方式2 串連兩個對象,支援自訂串連符 log ${hi1} ${hi2} Catenate SEPARATOR=---- hello world log ${hi2}case 6 list @{mylist} create list a b c #定義列表 log many @{mylist} #列印列表中的元素case 7 get time ${currentTime} get time #擷取目前時間 2018-01-02 18:05:47 log ${currentTime} ${currentYear} get time format=year #支援多種傳參,詳情看函數定義 log current year is ${currentYear}case 8 sleep log get time sleep 1 #睡眠,等同於python的time.sleep(5) log get timecase 9 if ${score} set variable 55 run keyword if ${score}>=90 log 優秀 ... ELSE IF ${score}>=70 log 良好 #ELSE/ELSE IF要大寫。。。文法很蛋疼。。為啥不直接套用python的文法。。 ... ELSE log 很差 # ... 不知道基於什麼考慮的。。看起來像是標識屬於“run keyword if”這個判斷邏輯;類似python的tab?case 10 for #for迴圈,注意需要使用\來標識這個for迴圈範圍,感覺和上面的...類似 :FOR ${i} IN RANGE 5 # for in in range(5): \ log ${i} # print(i)case 11 allround the list @{myList} create list 1 2 3 4 # myList = [1,2,3,4] :FOR ${i} IN @{myList} # for i in myList: \ log ${i} # print(i)case 12 Evauate #調用python中的方法,很強大 ${randomNum} EVALUATE random.random() random #變數 關鍵字EVALUATE 調用python的方法 方法所在的庫名 log ${randomNum}case 13 call my python #匯入自訂庫,下面有詳細說明 ${nowTime} get current time log ${nowTime}case 14 Comment log start #line 1 comment line 2 #標明注釋:使用關鍵字或者#均可 log endcase 15 Selenium2Library #Selenium2Library庫,操作瀏覽器,可作web介面自動化,待細化 open_browser http://www.baidu.com firefox Input text id=kw 陳月白 click button id=su sleep 3 close Browser
2.robot檔案中調用自訂庫
Selenium2Library提供了很多的方法,但在實際使用中仍有部分情境需要自行編寫,robotFramework支援匯入使用者自訂的庫。
以我的目錄結構為例:
(1)自訂方法所在的檔案:MyRobotFrameworkLib.py
import timeclass MyRobotFrameworkLib(): def __init__(self): pass #擷取目前時間 def get_current_time(self): current_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) return current_time
(2)建立MyLib.py
"""繼承自訂庫中的類,rfw會解析類中的函數作為關鍵詞網上也有說此檔案應命名為 __init__.py,但我試了,不行.robot檔案執行時報匯入失敗,看起來像是必須檔案名稱和類名相同(當前是這麼處理的)"""from MyRobotFrameworkLib import *class MyLib(MyRobotFrameworkLib): ROBOT_LIBRARY_SCOPE = "GLOBAL"
(3).robot檔案匯入自訂類、使用類中的方法
*** Settings ***Library MyLib #匯入自訂的庫case 13 call my python ${nowTime} get current time log ${nowTime}
3.使用Selenium2Library進行web介面自動化測試
#todo
20180126