QTP中用于场景恢复的函数

上一篇 / 下一篇  2012-07-03 17:35:48 / 个人分类:自动化测试

在制作场景恢复的时候可以选择调用函数来解决,下面的函数就是为此写的。因为之前用的场景恢复只报错了,看报告后大概了解了Object、 Method、Arguments、retVal等参数的意义,于是写了这个方法,一方面可以截图,另一方面可以在报告中看到哪个对象出问题了。因为我经 常是通过公司的自动化平台调用测试案例,一般晚上或凌晨执行,第二天看结果,看结果的时候一般直接从QC打开报告查看,这样方便自己定位问题。51Testing软件测试网4C%RwFe2^8fPh

   这个方法的作用我简单描述一下:首先是指定一个目录,然后检查是否从QC调用的(因为有时候自己单机执行也用到这个),如果是就记录测试集的名称,如果 不是就放到SingleTest目录,然后判断对应的目录是否存在,不存在就创建目录。之后是创建日期时间标签,然后把文件完整路径组合起来,并加上了案 例名称,测试机器等等(因为有很多台机器,方便查看是否某台机器出问题了)。之后模拟2次回车按键是防止有异常对话框存在,然后把原来的对象语句自己还原 回来,这样如果是某个对象执行什么操作的时候语句出错,就可以在报告里看到了。51Testing软件测试网#TDu/A:sz`/n5W

L z\ }a ??Mvn0  当然并不是很完善,比如时间标签的分秒没有加0的判断,Arguments其实是个集合,只不过我只考虑了1个的情况,没考虑多个的情况。51Testing软件测试网v9Ugjr!pT

|z#W9l v/G0  需要用这个的朋友可以自己稍微维护改进一下。使用方法就是自己创建一个场景恢复,然后选择指定文件里的函数,下面的函数最好单独保存为一个文件,因为我之前把他和我的函数库放在一起,发现里面的初始化函数被执行了2次,可能和场景恢复有关,分开后就没问题了。

Ib4l x2P!W P:T!s0'用于恢复场景控制。51Testing软件测试网$k9j1a1Z;o)Y
Function RecoveryFunctions(Object, Method, Arguments, retVal)
5f:kU[*h|0JU~R0   '有错误发生时,对Desktop进行截图并保存51Testing软件测试网0jd0[C1BJ
' SaveTestError ""
v.Jkh Y&KqF0 Set bjWS = CreateObject("WScript.Shell")51Testing软件测试网;Bd[\cb'd`d
 strDesktopFolder = "T:/TestData/测试组/测试截图/"51Testing软件测试网 MQ/V2I/P3Yo'DG@ze
 If QCUtil.IsConnected then51Testing软件测试网-j~)m B!}V
  If TypeName(QCUtil.CurrentTestSet) <> "Nothing" Then
Q9X f+zMx p u0   set CurrentTSTest = QCUtil.CurrentTestSet
1|kp-b4x/DDy0   strDesktopFolder = strDesktopFolder & CurrentTSTest.Name    '如果从QC的测试集运行的,则保存到测试集名字的目录下51Testing软件测试网/X"x/R K;{'L Jf'\a
  Else51Testing软件测试网-Gvgiwz P5@
   strDesktopFolder = strDesktopFolder & "SingleTest"    '如果不是从QC运行,保存到SingleTest目录下51Testing软件测试网'Ca)j&^HM ij.Zup
  End If
6OD f#}'l [0T.W0 Else
;A:w0oh3b0r2WC'QL0  strDesktopFolder = strDesktopFolder & "SingleTest"51Testing软件测试网A!w$SyZKO~?+^
 End If51Testing软件测试网#[M:q!O B^h p v n
        Set bjFSO = CreateObject("Scripting.FileSystemObject")
+qLcCq3g0 If objFSO.FolderExists(strDesktopFolder) Then51Testing软件测试网h%U@ MA6Q6Iz
  Set bjFolder = objFSO.GetFolder(strDesktopFolder)
jS6zR`s0 Else
o#th"N Z!B4D0  Set bjFolder = objFSO.CreateFolder(strDesktopFolder) 51Testing软件测试网4N+x$E3r,l6W.m;}
 End If51Testing软件测试网SHO ~"M%Sr7^ W;m
 '确定保存文件的名称51Testing软件测试网dag#\/uX]jDLE
 tmpTime=Time()51Testing软件测试网 {-cPr.u:|%_
 t=Split(tmpTime,":")51Testing软件测试网K@8s$qg R2E%Q \P)W
 If Len(t(0))<2 Then51Testing软件测试网A;h^)f WE
  t(0) = "0" & t(0)51Testing软件测试网(BodP&FK3wxo
 End If
l5J"VM,C)W0 tmpTime=t(0) & t(1) & t(2)51Testing软件测试网#HH q4Q q:~/Ov&_6q1G
 
P Xm-Y[ s0 tmpDate=CStr(Date())
7@['as_Cs)j0 tmpYear = CStr(Year(tmpDate))51Testing软件测试网LxYb&i0n8Z
 tmpMonth = CStr(Month(tmpDate))
~x)_!fS h\Ch-`0 tmpDay = CStr(Day(tmpDate))
#l7b#~m4q x&p5H)i,RV0 '如果月或日不足两位,前面补0
HI*A+p+d1r0 If Len(tmpMonth) < 2 Then
P)Y GDcYo~0    tmpMonth = "0" & tmpMonth51Testing软件测试网\;K+`%Y7E Ll(No
 End If
'Pr ]:hcT0 If Len(tmpDay) < 2 Then
6bJFR]$yD^W0    tmpDay = "0" & tmpDay51Testing软件测试网n9_jdY%[
 End If51Testing软件测试网 e.V:iW5`4r
 tpmDate=tmpYear & tmpMonth & tmpDay51Testing软件测试网#_5R \1p_
   51Testing软件测试网(H]5GT{.H
' strTimeStamp = CStr(Year(Now)) & CStr(Month(Now)) & CStr(Day(Now)) _
^,g Kp7d+b/vC0'  & "_" & CStr(Hour(Now)) & CStr(Minute(Now)) & CStr(Second(Now))51Testing软件测试网R8c ZJU
 strTimeStamp = tpmDate & "_" & tmpTime51Testing软件测试网_+r0O#Rm Ne0t)^
 strFile = strDesktopFolder & "/[测试案例]" & Environment("TestName") & "_[测试机器]" & Environment("LocalHostName") & "_[测试时间]" & strTimeStamp & "_Error.png"   '组合出截图文件的名称51Testing软件测试网l,[be4}R|
 '对Desktop截图并保存51Testing软件测试网Ys8\3Fc:}
 Desktop.CaptureBitmap strFile
D|8Y`%Q,NmfZ']Q0 
h-nK"mk?x%m.n0 reporter.Filter = 0
n+@Hctx4K0 Reporter.ReportEvent micFail,"案例失败","本处提交失败报告是为了避免下面因为数据问题导致错误报告没有正常提交。"51Testing软件测试网3pu e)wn
 reporter.Filter = 351Testing软件测试网DL/Hd7DH
 set WshShell = CreateObject("WScript.Shell")51Testing软件测试网#Ff9Mv)bt%qWd
 WshShell.SendKeys "{ENTER}"
3xv;Mcw#D0 WshShell.SendKeys "{ENTER}"51Testing软件测试网 eu,?Z0?P
 strStepInfo = "Test(" & Environment("TestName") & ")-Action(" & Environment("ActionName") & ") 发生非预期错误,测试退出"51Testing软件测试网+tz6}CLb,F9_D5m
' strDetails = "错误编号:" & CStr(Err.Number) & ",错误描述:" & Err.Description
+F h(l [5a'x0 On Error Resume Next51Testing软件测试网A4Ko qN(? ||Yr
 oClass=Object.GetTOProperty("micclass")
q%Z] q$_D0 oName=Object.GetTOProperty("name")
m8WR!A1Z0 reporter.Filter = 051Testing软件测试网M\l-P7kkS)U
 Reporter.ReportEvent micFail, strStepInfo, oClass & "(" & Chr(34) & oName & Chr(34) & ")." & Method & " " & Arguments(0) & " 出现错误,错误编号:" & retVal
3u4qWV:C4rB%t4f3u0        ' Arguments应该是1个或多个参数,此处只考虑了1个的情况,以后需要维护。
0r7J2`3S8i|Tp0 reporter.Filter = 351Testing软件测试网A `-df"Wr0[2YE
 '清除Error并退出Test
T_R?m0 If err.number<>0 Then51Testing软件测试网5XtVZar6RcF
            Err.Clear
6^@"~6m#r0 End If51Testing软件测试网 P Nn7? N)R2I
' Reporter.ReportEvent micFail, strStepInfo, Method & " " & Arguments(0) & " 出现错误,错误编号:" & retVal51Testing软件测试网8N7S,CZm9D/B
 On Error Goto 051Testing软件测试网n rv&l z,~D$M
 systemutil.CloseProcessByName("iexplore.exe")51Testing软件测试网b"QR ^-n @
 ExitTest
`6AjV%h#D @S:^9l(@0[0End Function

TAG:

 

评分:0

我来说两句

Open Toolbar