QTP日誌實踐的幾點總結

作者:風過無息               2007528 

 一起測試網 : 軟件質量專家

 背景:在日常使用QTP中,因爲有QC的存在,導致了QTP的察看結果的功能並不是用的很順手,所以筆者在脫離開QC的情況下,在工作實踐中總結了QTP的日誌實現的一些方法。

生成txt文件。這是從開發那邊得到的啓示。
首先定義一個sub:
Public Sub WriteLineToFile(message)
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fileSystemObj, fileSpec
Dim currentTime
currentDate = Date
currentTime = Time
testName = "log"
Set fileSystemObj = CreateObject("scrīpting.FileSystemObject")
fileSpec ="C:/" &testName& ".txt" 'change this according to your directory
If Not (fileSystemObj.FileExists(filespec)) Then  
Set logFile = fileSystemObj.CreateTextFile(fileSpec, ForWriting, True)  
logFile.WriteLine ("#######################################################################")  
logFile.WriteLine (currentDate & currentTime & " Test: " & environment.Value("TestName") )  
logFile.WriteLine ("#######################################################################")  
logFile.Close  
Set logFile = Nothing
End If
Set logFile = fileSystemObj.OpenTextFile(fileSpec, ForAppending, False, True)
logFile.WriteLine (currentDate & currentTime & " " & message)
logFile.Close
Set logFile = Nothing
Set fileSystemObj = Nothing
End Sub
這樣就能在txt中直接明瞭的看到自己的日誌了。(個人感覺比看QTP的results好多了了,當然QTP自身的results還有錯誤圖片等等,下面會介紹我的解決方案)
 
題外話:在實際應用中,我會在sub中增加一個flag,來標誌不同的嚴重等級,在不同的情況下控制輸出不一樣類型的日誌。
使用QTP自身的抓圖功能
Public Function capture_desktop()
Dim datestamp
Dim filename
datestamp = Now()
filename = Environment("TestName")&"_"&datestamp&".png"
filename = Replace(filename,"/","")
filename = Replace(filename,":","")
filename = "C:/QTP_ScreenShots"&""&filename
Desktop.CaptureBitmap filename
Reporter.ReportEvent micFail,"image","<img src='" & filename & "'>"
End Function
該函數主要就是用到Desktop.CaptureBitmap的這個方法,把桌面的圖片抓下來,這樣比較自由的抓下任意時候桌面的圖片,方便我們檢查結果。
 
題外話:可以通過Function的返回值和上面提到的方法結合在一起的話,效果會更好的。
使用outlook發送郵件
Public Sub SendEmail(testname,strsubject,stremailcontent,attachment,strrecipient)
Set out = CreateObject("Outlook.Application")
Set mapi = out.GetNameSpace("MAPI")
Set email = out.CreateItem(0)
email.Recipients.Add(strrecipient)
email.Subject = strsubject
email.Body = stremailcontent
Set oAttachment = email.Attachments.Add(attachment)
email.Send
Set outlook = Nothing
Set mapi = Nothing
End Sub
題外話:假如我們把日誌和抓圖都結合在一起作爲附件發出去的話,在自動化測試中後期會很有用,當然前提是不使用QC的情況下。
 
這是一個針對上面第3點如何把抓圖放在一個doc中。
Set oWord = CreateObject("Word.Application")
oWord.DisplayAlerts = False
oWord.Visible = False
oWord.documents.open "testWordDoc.doc"
Set oDoc = oWord.ActiveDocument
Set oRange = oDoc.content
oRange.ParagraphFormat.Alignment = 0
oRange.insertafter vbcrlf '& " " & vbcrlf
oRange.collapse(0)
oRange.InlineShapes.AddPicture "ImagePath.bmp", False, True
oWord.ActiveDocument.Save
oWord.Application.Quit True
Set oRange = Nothing
Set oDoc = Nothing
Set oWord = Nothing
當然QTP和excel,word結合的不錯的,所以在測試中靈活運動,效果會很好的。
 

總結:畢竟大規模的QTP測試中,需要總結實踐的很多,這是筆者是在如何將測試結果的產生中拋磚引玉,學無止境,和大家一起進步。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章