测试共享,雁过留痕

[转载]QTP日志实践的几点启示

上一篇 / 下一篇  2008-07-31 15:07:58 / 个人分类:QTP

背景:在日常使用QTP中,因为有QC的存在,导致了QTP的察看结果的功能并不是用的很顺手,所以笔者在脱离开QC的情况下,在工作实践中总结了QTP日志实现的一些方法。

1、 生成txt文件。这是从开发那边得到的启示。

gd Y$?BDq1H0

首先定义一个sub51Testing软件测试网(}"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"dq}G+F0Set logFile = fileSystemObj.CreateTextFile(fileSpec, ForWriting, True)  
&u8v&b2l(w0logFile.WriteLine ("#######################################################################")  
'R-b0[1D^em Tv0logFile.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

W)K0\YPy4n0

这样就能在txt中直接明了的看到自己的日志了。(个人感觉比看QTPresults好多了了,当然QTP自身的results还有错误图片等等,下面会介绍我的解决方案)

A7R4Suss0

 

t*D5`@\d)Y)xT.P0

题外话:在实际应用中,我会在sub中增加一个flag,来标志不同的严重等级,在不同的情况下控制输出不一样类型的日志。

9E(O~ Inn!O*_0

2、 使用QTP自身的抓图功能

\a3P%x8@I8L0

Public Function capture_desktop()

hD%{} z0

Dim datestamp51Testing软件测试网:H&aRs9L3Z}z1{

Dim filename51Testing软件测试网c,dK:|J_h/[(d

datestamp = Now()

?9s D,DJ(A.__8X0

filename = Environment("TestName")&"_"&datestamp&".png"

`W-b#I g-Y0

filename = Replace(filename,"/","")

"n`7TOgN*I.N2Y|0

filename = Replace(filename,":","")

Gh^w"r+|0

filename = "C:\QTP_ScreenShots"&""&filename51Testing软件测试网0o2XB1_:{sB]3mk

Desktop.CaptureBitmap filename

Hh!@aQ0

Reporter.ReportEvent micFail,"image","<img src='" & filename & "'>"

]'OOUY(MwM4o0

End Function

&A+v{1}f;]p]0

该函数主要就是用到Desktop.CaptureBitmap的这个方法,把桌面的图片抓下来,这样比较自由的抓下任意时候桌面的图片,方便我们检查结果。

|6^OA,OcE"sC0

 

8ES;o:J4S2T#e[ s1C0

题外话:可以通过Function的返回值和上面提到的方法结合在一起的话,效果会更好的。51Testing软件测试网N*x7Y&B\8v$~

3、 使用outlook发送邮件

I(v!Y|J5kk'\051Testing软件测试网~-c n'D i9Q{,Lp2P

Public 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#Scmi0Set email = out.CreateItem(0)51Testing软件测试网:v@$xH(q
email.Recipients.Add(strrecipient)51Testing软件测试网zR*R5wG&N5[1s7Jdl%R
email.Subject = strsubject51Testing软件测试网-d;i1p7p!X q
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
X&CO/h!Y+S?0End Sub

$x,SR9Z;en0

题外话:假如我们把日志和抓图都结合在一起作为附件发出去的话,在自动化测试中后期会很有用,当然前提是不使用QC的情况下。51Testing软件测试网];L Xfpy1v:?2T

 51Testing软件测试网1Bofvz2S#{_+\

4、 这是一个针对上面第3点如何把抓图放在一个doc中。

fg.A'f}$[YNq0

Set ōWord = CreateObject("Word.Application")
(ka ]'mH+w0oWord.DisplayAlerts = False51Testing软件测试网1c%g-jd W!I(v
oWord.Visible = False
Gz%c2]lF0oWord.documents.open "testWordDoc.doc"51Testing软件测试网v2a gPO oG
Set ōDoc = oWord.ActiveDocument51Testing软件测试网hE8\X)Xm+| u]
Set ōRange = oDoc.content51Testing软件测试网Zx:`{Q^ux8_'n
oRange.ParagraphFormat.Alignment = 051Testing软件测试网e+y9C j+Z E,[U }A N&Vs(n
oRange.insertafter vbcrlf '& " " & vbcrlf
AfA$OuB.Km0oRange.collapse(0)51Testing软件测试网9y6f\ [I7^@\-G
oRange.InlineShapes.AddPicture "ImagePath.bmp", False, True
y*R};M1b0jD^0oWord.ActiveDocument.Save51Testing软件测试网4iBc;bzrV
oWord.Application.Quit True
?-q)GVnq;](y\~0Set ōRange = Nothing
_DA`0i{'x0Set ōDoc = Nothing51Testing软件测试网 k~m/MJ-_@P-y Z8{ h
Set ōWord = Nothing

5re n&~|,[sZP$h/M0

当然QTPexcelword结合的不错的,所以在测试中灵活运动,效果会很好的。

:Oya-vs^-Yn0

 

3A(m'|o^D-s"Ci2_/M s0

总结:毕竟大规模的QTP测试中,需要总结实践的很多,这是笔者是在如何将测试结果的产生中抛砖引玉,学无止境,和大家一起进步。

;W:mt^1B \1Z7y0

TAG: QTP

 

评分:0

我来说两句

Open Toolbar