本身2011年作保險系統中理賠流程自動化時,開發的一個小框架。當時尚未selenium,不過基於UFT的一些功能仍是比較實用的,可用於參考。sql
框架源碼:https://yunpan.cn/cBVBxA6x2iXHD (提取碼:2165)數據庫
框架已經實現的功能以下:express
一、腳本數據分離瀏覽器
二、異常截圖並關聯截圖app
三、無界面啓動UFT框架
四、定時啓動UFTdom
五、步驟日誌跟蹤ide
六、數據庫檢查點驗證函數
七、測試結果發送工具
八、測試報告生成
九、場景恢復
十、腳本執行速度控制
十一、系統資源監控,如Cpu峯值監控(基於UFT)
十二、執行過程錄製回放(基於UFT)
一、必須導入的驅動腳本,用戶全局環境控制
Call driver() Function driver() '初始化環境,包括得到project所在的本地目錄。 projectPath = getProjectPath() sourceDataFile = projectPath&"\TEST.xls" sourceDataSheet = "Sheet1" Dim qtApp 'As QuickTest.Application ' Declare the Application object variable Dim qtTest 'As QuickTest.Test ' Declare a Test object variable Dim qtResultsOpt 'As QuickTest.RunResultsOptions ' Declare a Run Results Options object variable Set qtApp = CreateObject("QuickTest.Application") ' Create the Application object qtApp.Launch ' Start QuickTest qtApp.Visible = True ' Make the QuickTest application visible ' Set QuickTest run options qtApp.Options.Run.CaptureForTestResults = "OnError" 'qtApp.Options.Run.RunMode = "Fast" qtApp.Options.Run.ViewResults = False ' 打開入口的測試腳本,而且加載testCase的腳本 qtApp.Open projectPath&"\理賠系統流程", False, False ' 打開入口的測試腳本,可寫,不保存 Set qtTest = qtApp.Test ' set run settings for the test Set qtResultsOpt = CreateObject("QuickTest.RunResultsOptions") qtResultsOpt.ResultsLocation = projectPath&"\result" '運行結果保存到臨時文件夾中 qtTest.Run qtResultsOpt, True qtTest.Close Set qtResultsOpt = Nothing ' Release the Run Results Options object Set qtTest = Nothing ' Release the Test object qtApp.quit Set qtApp = Nothing ' Release the Application object End Function Function getProjectPath() Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.GetFile(wscript.scriptfullname) getProjectPath = objFSO.GetParentFolderName(objFile) Set objFSO = Nothing Set objFile = Nothing End Function
二、驅動的工具集
包括:路徑查找,生成指定規則的隨機數,數據庫操做,瀏覽器操做,日誌操做,日期工具等。
'################################' '---------------通用方法--------------------------' '################################' Function pathFind( searchingFolder,searchingFileName,fileType) '根據傳入的根目錄,查找該目錄下的指定名稱,以及指定文件類型的文件,並返回其絕對路徑。 tempArray = Split(searchingFileName,".") fileName = tempArray(0)&"."&fileType Set fso=CreateObject( "Scripting.FileSystemObject" ) Set objFolder = fso.GetFolder( searchingFolder ) Set objFileCollection = objFolder.Files for each objFile in objFileCollection If objFile.Name = fileName Then i=i+1 searchedFilePath = objFile.Path Exit for End If Next If i=0 then '遍歷子文件夾 Set objSubFoldersCollection = objFolder.SubFolders For each objInputSubFolder in objSubFoldersCollection searchedFilePath= pathFind(objInputSubFolder,searchingFileName,fileType) If searchedFilePath<>"" Then Exit For End if Next End If pathFind = searchedFilePath End Function Function getParentFolderPath(curPath) '輸入一個路徑的字符串,得到其上級目錄的字符串,主要目的是根據QTP腳本所在文件夾,找到工程所在的文件夾 tempArray = split(curPath,"\") tempStr ="" For i=LBound(tempArray) to UBound(tempArray)-1 tempStr = tempStr&tempArray(i)&"\" Next getParentFolderPath = tempStr End Function Function generateFilterExp(Sheet_Name,filterExp) ''解析條件語句,只支持 >= ,<= , <>, >, <, = 這6種表達式 '對錶達式做了處理,支持中文的分號,不區分英文的大小寫。 If filterExp<>"" Then If InStr(filterExp,";")>0 Then filterExp = Replace(filterExp,";",";") End If expressArray = Split(LCase(filterExp),";") For i=LBound(expressArray) To UBound(expressArray) If InStr(expressArray(i),">=") Then tempArray = Split(expressArray(i),">=") If i=LBound(expressArray) Then expressStr = "DataTable("&Chr(34)&tempArray(0)&Chr(34)&","&Chr(34)&Sheet_Name&Chr(34)&")"&">="&chr(34)&tempArray(1)&chr(34) else expressStr = expressStr&" and "&"DataTable("&Chr(34)&tempArray(0)&Chr(34)&","&Chr(34)&Sheet_Name&Chr(34)&")"&">="&chr(34)&tempArray(1)&chr(34) End If ElseIf InStr(expressArray(i),"<=") Then tempArray = Split(expressArray(i),"<=") If i=LBound(expressArray) Then expressStr = "DataTable("&Chr(34)&tempArray(0)&Chr(34)&","&Chr(34)&Sheet_Name&Chr(34)&")"&"<="&chr(34)&tempArray(1)&chr(34) else expressStr = expressStr&" and "&"DataTable("&Chr(34)&tempArray(0)&Chr(34)&","&Chr(34)&Sheet_Name&Chr(34)&")"&"<="&chr(34)&tempArray(1)&chr(34) End If ElseIf InStr(expressArray(i),"<>") Then tempArray = Split(expressArray(i),"<>") If i=LBound(expressArray) Then expressStr = "DataTable("&Chr(34)&tempArray(0)&Chr(34)&","&Chr(34)&Sheet_Name&Chr(34)&")"&"<>"&chr(34)&tempArray(1)&chr(34) else expressStr = expressStr&" and "&"DataTable("&Chr(34)&tempArray(0)&Chr(34)&","&Chr(34)&Sheet_Name&Chr(34)&")"&"<>"&chr(34)&tempArray(1)&chr(34) End If ElseIf InStr(expressArray(i),"<") Then tempArray = Split(expressArray(i),"<") If i=LBound(expressArray) Then expressStr = "DataTable("&Chr(34)&tempArray(0)&Chr(34)&","&Chr(34)&Sheet_Name&Chr(34)&")"&"<"&chr(34)&tempArray(1)&chr(34) else expressStr = expressStr&" and "&"DataTable("&Chr(34)&tempArray(0)&Chr(34)&","&Chr(34)&Sheet_Name&Chr(34)&")"&"<"&chr(34)&tempArray(1)&chr(34) End If ElseIf InStr(expressArray(i),">") Then tempArray = Split(expressArray(i),">") If i=LBound(expressArray) Then expressStr = "DataTable("&Chr(34)&tempArray(0)&Chr(34)&","&Chr(34)&Sheet_Name&Chr(34)&")"&">"&chr(34)&tempArray(1)&chr(34) else expressStr = expressStr&" and "&"DataTable("&Chr(34)&tempArray(0)&Chr(34)&","&Chr(34)&Sheet_Name&Chr(34)&")"&">"&chr(34)&tempArray(1)&chr(34) End If ElseIf InStr(expressArray(i),"=") Then tempArray = Split(expressArray(i),"=") If i=LBound(expressArray) Then expressStr = "DataTable("&Chr(34)&tempArray(0)&Chr(34)&","&Chr(34)&Sheet_Name&Chr(34)&")"&"="&chr(34)&tempArray(1)&chr(34) else expressStr = expressStr&" and "&"DataTable("&Chr(34)&tempArray(0)&Chr(34)&","&Chr(34)&Sheet_Name&Chr(34)&")"&"="&chr(34)&tempArray(1)&chr(34) End If Else MsgBox("不支持此表達式") End If Next Else expressStr = "DataTable( 1 "&","&Chr(34)&Sheet_Name&Chr(34)&")"&"<>"&chr(34)&chr(34) End If 'logPrint("在generateFilterExp方法中,條件語句解析結果:"&expressStr) generateFilterExp = expressStr End Function '隨機函數生成 '輸入值:生成值範圍 i~j '返回值:隨機數 Public Function Get_RandNum(fromNum,toNum) If (fromNum<0) Or (toNum<0) Then MsgBox "只接受大於零的輸入" ElseIf fromNum>toNum then MsgBox "起始值必須小於結束值" Else Dim RunTime Randomize RunTime = Int((10 * Rnd) + 1) Dim MyValue,i For i = 1 To RunTime Randomize MyValue = Int(((toNum - fromNum + 1) * Rnd) + (fromNum)) Next Get_randNum=MyValue End If End Function Function MakeString(inputlength) Dim I,x,B,A If IsNumeric(inputlength) Then For I = 1 To inputlength A = Array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z") Randomize x=Get_RandNum(0,35) B = A(x) makestring =makestring +B Next MakeString = makestring else msgbox ("只接受數字輸入") End If End Function Function CarNum(inputlength) Dim I,x,B,A If IsNumeric(inputlength) Then For I = 1 To inputlength A = Array("0","1","2","3","4","5","6","7","8","9") Randomize x=Get_RandNum(0,9) B = A(x) CarNum =carnum +B Next CarNum = carnum else msgbox ("只接受數字輸入") End If End Function Function dyht(sqlstrm,keyname) Dim Cnn '定義一個數據庫鏈接串 Set Cnn = CreateObject("ADODB.Connection") Cnn.Open ="Provider=OraOLEDB.Oracle.1;Password=ywquery;Persist Security Info=True;User ID=ywquery;Data Source=ceshidb" 'If Cnn.State = 0 Then '判斷數據庫鏈接是否成功 ' Reporter.ReportEvent micFail, "CESHIDB", "鏈接數據庫失敗" 'else ' Reporter.ReportEvent micPass, "CESHIDB", "鏈接數據庫成功" 'End If Dim Rs Dim sql Dim uid if Cnn.State<> 0 then Set Rs=CreateObject("ADODB.Recordset") '生成記錄集對象 sql =sqlstrm '從數據庫中查詢cust的全部記錄(需參數化) Rs.Open sql ,Cnn,1,3 '執行sql語句,記錄能夠自由移動,單數記錄處於只讀模式 uid=Rs(keyname) '取得字段爲custid的記錄,遊標定義在第一行,因此取得的是該字段所在行的第一行數據 'msgbox uid End If dyht=uid End Function Function openie(url) Dim ie '定義瀏覽器對象 set ie=createobject("internetexplorer.application") ie.visible=true ie.navigate url '打開系統 End Function Function updatedb(sql)'更新數據庫 Dim res,Conn,strConn,Cmd strConn="Provider=OraOLEDB.Oracle.1;Password=ywuser252;Persist Security Info=True;User ID=ywuser;Data Source=ceshidb" 'sql="update t_address set address='HK' where id=3" '創建數據庫鏈接對象 Set Conn=CreateObject("adodb.connection") '打開數據庫鏈接 Conn.open strConn '創建記錄集對象 Set res=CreateObject("adodb.recordset") '創建命令對象 Set Cmd=CreateObject("adodb.command") Cmd.ActiveConnection=Conn Cmd.CommandText=sql '執行更新 Cmd.Execute Conn.close '釋放資源 Set res=Nothing Set Conn=Nothing '輸出顯示 End Function '關閉ie Function closeie() dim s s="iexplore.exe" Set objWMIService =GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") Set colProcessList=objWMIService.ExecQuery _ ("Select * from Win32_Process Where Name='" & s & "'") For Each objProcess in colProcessList 'msgbox "已發現目標!" objProcess.Terminate() Next End Function '格式化時間的函數 Function FormateDateTime(sendTime,Para) select case Para Rem YYYYMMDDHHmmss case "1" sendTime = year(sendTime) & right( "00" & month(sendTime),2) & right( "00" & day(sendTime),2) & right( "00" & hour(sendTime),2) & right( "00" & minute(sendTime),2) & right( "00 " & second(sendTime),2) Rem YYYYMMDD case "2" sendTime = year(sendTime) & right( "00" & month(sendTime),2) & right( "00" & day(sendTime),2) Rem YYYY-MM-DD case "3" sendTime = year(sendTime) & "-"& right( "00" & month(sendTime),2) & "-"& right( "00" & day(sendTime),2) Rem YYYY年MM月DD日 case "4" sendTime = year(sendTime) & "年"& right( "00" & month(sendTime),2) & "月"& right( "00" & day(sendTime),2)& "日 " Rem YYYY-MM-DD HH:mm:ss case "5" sendTime = year(sendTime) & "-"& right( "00" & month(sendTime),2) & "-"& right( "00" & day(sendTime),2) & " "& right( "00" & hour(sendTime),2) & ": "& right( "00 " & minute(sendTime),2) & ": "& right( "00 " & second(sendTime),2) end select FormateDateTime = SendTime end Function 'msgbox FormateDateTime(date(),"3") '建立txt的函數 Function CreateFile(sFilename,bOverwrite) Set fso = CreateObject("Scripting.FileSystemObject") Set CreateFile = fso.CreateTextFile(sFilename,bOverwrite) End Function 'Set f = CreateFile("C:\been.txt",True) '打印log Public Function logPrint(ByVal logMessage) Dim fso, logFile Set fso = CreateObject("Scripting.FileSystemObject") Set logFile = fso.OpenTextFile(ProjectDir&"runtime.log", 8, True) 'Open a file and write to the end of the file and open as Unicode logFile.WriteLine(date() & " " & hour(now) & ":" & minute(now) & ":" & second(now) & ": " & logMessage) logFile.Close End Function 'logPrint("this is log") Public Function ErrorHandle() If Err.Number <> 0 Then logPrint "Error Num: " & Err.Number & "; Error Src: " & Err.Source & "; Error Desc: " & Err.Description Err.Clear End If End Function
三、腳本代碼片斷,完整代碼請見工程源碼
option explicit '規範聲明 '定義變量 Dim ProjectDir '根目錄 Dim TestSetExcelFile '測試用例Excel路徑 Dim vbprpDuserUserCode1'定義用戶名 Dim vbprpDuserUserCode2'定義用戶名 Dim vbprpDuserPassword'定義密碼 Dim row_count ' Global中用例行數 Dim rownum ' 當前運行的行 Dim sqlstrm 'sql查詢變量 Dim keyname '數據庫字段 Dim TestCasesFile Dim SheetNameExcel Dim isRun Dim StartTime Dim EndTime Dim inRespTime Dim n '定義循環變量 Dim vbprpNRegistReportorName1'定義客戶名 Dim vbprpNRegistReportorName'定義客戶姓名 Dim vbprpNRegistDamageAddress'定義出險地點 Dim vbprpNRegistDamageCourse'定義出險過程 Dim vbprpNRegistCarDriverPhoneNumber'定義客戶聯繫方式 Dim vbprpNRegistCarDrivingLicenseNo'客戶證件號 Dim vbprpNRegistCarDrivingLicenseNo2'三者證件號 Dim vbprpNRegistCarDriverPhoneNumber2'三者聯繫方式 Dim vbprpNRegistCarReportLoss'定義報損金額 Dim vbBAOANNum1'定義商業險報案號 Dim vbBAOANNum2'定義交強險報案號 Dim vbJIEANNum Dim vbJIEANNum1 Dim vbBAODANNum1'定義保單號 Dim vbBAODANNum2 Dim vbOutHospDate Dim vbAccountCode Dim accountcode Dim vbSHUNum'計算書號 Dim vbPEIANNum'賠案號 Dim vbSHUNum1 Dim vbPEIANNum1 Dim vbSHIGUNum'定義事故號 Dim vbscheduleDeptCode'定義客戶二帳戶名 Dim vbLicenseNo'定義車牌號 Dim JBYXSJ'定義日期 '參數賦值 '定義項目所在的根目錄 ProjectDir = getParentFolderPath(Environment.Value("TestDir")) '項目所在的根目錄, 'msgbox ProjectDir Extern.Declare micVoid,"capture",ProjectDir&"CreateBitmap.dll","",micString '將用例轉化爲中間表 TestCasesFile = ProjectDir&"TEST.xls" '管理全部測試用例的Excel文件的路徑 'msgbox TestCasesFile SheetNameExcel = "Sheet1" Call carinsured() Function carinsured() JBYXSJ = FormateDateTime(date(),"3") vbOutHospDate = date+3 Call DataTable.ImportSheet(TestCasesFile,"Sheet1","Global") row_count = DataTable.GetSheet("Global").GetRowCount'獲得用例中總行數 'msgbox row_count '此處添加循環 For n = 1 to row_count DataTable.GetSheet("Global").SetCurrentRow(n) datatable.Export ProjectDir&JBYXSJ&"流程驗證結果.xls" '導出運行結果,生成result.xls文件 '導入中間表 TestSetExcelFile = ProjectDir&JBYXSJ&"流程驗證結果.xls" '管理全部測試用例的Excel文件的路徑 Call DataTable.ImportSheet(TestSetExcelFile,"Global","Global") 'row_count = DataTable.GetSheet("Global").GetRowCount'獲得用例中總行數 isRun=DataTable.Value("RUN", "Global") If isRun = "Y" Then rowNum = datatable.GetSheet("Global").GetCurrentRow ' msgbox rowNum vbprpDuserUserCode1 = DataTable("賬號一","Global") vbprpDuserUserCode2 = DataTable("賬號二","Global") vbprpDuserPassword = DataTable("用戶密碼","Global") vbprpNRegistReportorName = DataTable("客戶姓名","Global") vbprpNRegistDamageAddress = DataTable("出險地點","Global") vbprpNRegistDamageCourse = DataTable("出險通過","Global") vbprpNRegistCarDriverPhoneNumber = DataTable("客戶聯繫電話","Global") vbprpNRegistCarDrivingLicenseNo = DataTable("客戶證件號碼","Global") vbLicenseNo = DataTable("客戶車牌號","Global") vbprpNRegistCarReportLoss = DataTable("報損金額","Global") ...... '調用用戶自定義的邏輯代碼 DataTable("流程是否經過") = "是" DataTable("備註") = "流程所有經過" datatable.ExportSheet ProjectDir&JBYXSJ&"流程驗證結果.xls","Global" '導出運行結果,生成result.xls文件 closeie() End If Next End FUNCTION