[转载]QTP日志实践的几点启示
上一篇 / 下一篇 2008-07-31 15:07:58 / 个人分类:QTP
1、 生成txt文件。这是从开发那边得到的启示。
gdY$?BDq1H0首先定义一个sub:51Testing软件测试网(}"F| |xu
Public Sub WriteLineToFile(message)
*Ft\%U*c^0Const ForReading = 1, ForWriting = 2, ForAppending = 851Testing软件测试网
kR,d2Pa g
Dim fileSystemObj, fileSpec51Testing软件测试网0q8qdPj
q-BC
Dim currentTime
w9D(@'C8|S)],JN,c0currentDate = Date
3a1B%Jb3S-u6D\,E0currentTime = Time51Testing软件测试网{]7dR'W
testName = "log"51Testing软件测试网
UQ"\B1H$fx
Set fileSystemObj = CreateObject("scrīpting.FileSystemObject")51Testing软件测试网 I+C1K4h1T[ Hp3u
fileSpec ="C:\" &testName& ".txt" 'change this according to your directory51Testing软件测试网dUR)K(V^jw
If Not (fileSystemObj.FileExists(filespec)) Then
$F"d q}G+F0Set logFile = fileSystemObj.CreateTextFile(fileSpec, ForWriting, True)
&u8v&b2l(w0logFile.WriteLine ("#######################################################################")
'R-b0[1D^emTv0logFile.WriteLine (currentDate & currentTime & " Test: " & environment.Value("TestName") ) 51Testing软件测试网:\}o7S$M,A^_
logFile.WriteLine ("#######################################################################")
;w+D
X4a$AL__O0logFile.Close
l!j-?j-JHyA0Set logFile = Nothing51Testing软件测试网R2f{'f8ifb
End If51Testing软件测试网$?@@o-rJ
Set logFile = fileSystemObj.OpenTextFile(fileSpec, ForAppending, False, True)51Testing软件测试网j+tN,rUaseW
logFile.WriteLine (currentDate & currentTime & " " & message)51Testing软件测试网;\"Bx]
?"?
logFile.Close
!_K0\(D3MQ9d)tC
]0Set logFile = Nothing51Testing软件测试网;S5DgGH
n
Set fileSystemObj = Nothing51Testing软件测试网Kc'u
Rt!@a
N
End Sub
这样就能在txt中直接明了的看到自己的日志了。(个人感觉比看QTP的results好多了了,当然QTP自身的results还有错误图片等等,下面会介绍我的解决方案)
A7R4Suss0题外话:在实际应用中,我会在sub中增加一个flag,来标志不同的严重等级,在不同的情况下控制输出不一样类型的日志。
9E(O~ Inn!O*_02、 使用QTP自身的抓图功能
\a3P%x8@I8L0Public 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的这个方法,把桌面的图片抓下来,这样比较自由的抓下任意时候桌面的图片,方便我们检查结果。
|6^OA,OcE"sC0题外话:可以通过Function的返回值和上面提到的方法结合在一起的话,效果会更好的。51Testing软件测试网N*x7Y&B\8v$~
3、 使用outlook发送邮件
I(v!Y|J5kk'\051Testing软件测试网~-cn'D i9Q{,Lp2PPublic Sub SendEmail(testname,strsubject,stremailcontent,attachment,strrecipient)51Testing软件测试网h0Fz%]z vY
[;wi(j
Set ōut = CreateObject("Outlook.Application")
)g'q*ac r+]0Set mapi = out.GetNameSpace("MAPI")
0C-dp#S cmi0Set email = out.CreateItem(0)51Testing软件测试网:v@$xH(q
email.Recipients.Add(strrecipient)51Testing软件测试网zR*R5wG&N5[1s7Jdl%R
email.Subject = strsubject51Testing软件测试网-d;i1p7p!Xq
email.Body = stremailcontent51Testing软件测试网Gv.PcX
Set ōAttachment = email.Attachments.Add(attachment)51Testing软件测试网7iWp2yy
email.Send51Testing软件测试网&]6G0LO&dM9?Q
Set ōutlook = Nothing
9O#R R)SVt(WxUh0Set mapi = Nothing