标题: QTP日志实践的几点总结 [打印本页] 作者: oftime999 时间: 2007-6-18 14:48 标题: QTP日志实践的几点总结 背景:在日常使用QTP中,因为有QC的存在,导致了QTP的察看结果的功能并不是用的很顺手,所以笔者在脱离开QC的情况下,在工作实践中总结了QTP的日志实现的一些方法。
1、生成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,来标志不同的严重等级,在不同的情况下控制输出不一样类型的日志。
2、使用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的返回值和上面提到的方法结合在一起的话,效果会更好的。
3、使用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
Public function capture_desktop (filepath)
'Dim datestamp
'Dim filename
'datestamp = Now()
'filename = Environment("TestName")&"_"&datestamp&".png"
'filename = Replace(filename,"/","")
'filename = Replace(filename,":","")
'filename = "C:\QTP_ScreenShots\"&""&filename
Desktop.CaptureBitmap filepath
'Reporter.ReportEvent micFail,"image","<img src='" & filename & "'>"
End function
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
Sub CreateWord(filepath)
Set oWord = CreateObject("Word.Application")
oWord.DisplayAlerts = False
oWord.Visible = False
'oWord.documents.open "c:\testWordDoc1.doc"
'Set oDoc = oWord.ActiveDocument
Set oDoc=oWord.Documents.add
' 临时创建一个新的word对象,对象尚未命名
oWord.Application.Quit True
Set oRange = Nothing
Set oDoc = Nothing
Set oWord = Nothing
End Sub
sub DeleteSource(filepath)
On error resume next
Set fso=CreateObject("Scripting.FileSystemObject")
fso.deletefile filepath &"\capture_desktop.bmp"
fso.deletefile filepath &"\TestReport.doc"
Set fso=nothing
end sub
'MinimizeWindow()
Sub MinimizeWindow
Dim objQTPWin
Set objQTPWin = GetObject("" , "QuickTest.Application")
objQTPWin.WindowState = "Minimized"
Set objQTPWin = Nothing
'其中objQTPWin.WindowState还支持最大化"Maxmized"和恢复"Restored"状态,如果把上面的代码包装成一个函数,就是根据需要在测试运行中
'随时改变QTP窗口的状态了。