这两天一直在研究脚本中的场景恢复,以前一直想把场景恢复放到外面做统一的处理。所以就想通过函数去解决,结果用err.number和err.description获取不到异常run error提示.请教了前辈,前辈说我这样是处理不好的,没有办法统一处理,还是放到脚本中。自己又折腾了一天,最后还是决定放弃了,在脚本中做场景恢复,觉得也蛮有效果的。函数如下:
' 编写日期: 2013-7-10
'
测试功能:场景恢复
'============================================================================
'用于恢复场景控制。
Function RecoveryFunctions(Object, Method, Arguments, retVal)
'有错误发生时,对Desktop进行截图并保存
' SaveTestError ""
Set bjWS = CreateObject("WScript.Shell")
'定义保存截图的路径
strDesktopFolder = "D:/TestData/测试截图/"
'创建文件对象
Set bjFSO = CreateObject("Scripting.FileSystemObject")
'判断文件是否存在,不存在创建一个文件
If objFSO.FolderExists(strDesktopFolder) Then
Set bjFolder = objFSO.GetFolder(strDesktopFolder)
Else
Set bjFolder = objFSO.CreateFolder(strDesktopFolder)
End If
'确定保存文件的名称
tmpTime=Time()
t=Split(tmpTime,":")
If Len(t(0))<2 Then
t(0) = "0" & t(0)
End If
tmpTime=t(0) & t(1) & t(2)
tmpDate=CStr(Date())
tmpYear = CStr(Year(tmpDate))
tmpMonth = CStr(Month(tmpDate))
tmpDay = CStr(Day(tmpDate))
'如果月或日不足两位,前面补0
If Len(tmpMonth) < 2 Then
tmpMonth = "0" & tmpMonth
End If
If Len(tmpDay) < 2 Then
tmpDay = "0" & tmpDay
End If
tpmDate=tmpYear & tmpMonth & tmpDay
' strTimeStamp = CStr(Year(Now)) & CStr(Month(Now)) & CStr(Day(Now)) _
' & "_" & CStr(Hour(Now)) & CStr(Minute(Now)) & CStr(Second(Now))
strTimeStamp = tpmDate & "_" & tmpTime
strFile = strDesktopFolder & "/[测试案例]" & Environment("TestName") & "_[测试机器]" & Environment("LocalHostName") & "_[测试时间]" & strTimeStamp & "_Error.png" '组合出截图文件的名称
'对Desktop截图并保存
Desktop.CaptureBitmap strFile
reporter.Filter = 0
Reporter.ReportEvent micFail,"案例失败","本处提交失败报告是为了避免下面因为数据问题导致错误报告没有正常提交。"
reporter.Filter = 3
set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "{ENTER}"
strStepInfo = "Test(" & Environment("TestName") & ")-Action(" & Environment("ActionName") & ") 发生非预期错误,测试退出"
strDetails = "错误编号:" & CStr(Err.Number) & ",错误描述:" & Err.Description
On Error Resume Next
oClass=Object.GetTOProperty("micclass")
oName=Object.GetTOProperty("name")
reporter.Filter = 0
Reporter.ReportEvent micFail, strStepInfo, oClass & "(" & Chr(34) & oName & Chr(34) & ")." & Method & " " & Arguments(0) & " 出现错误,错误编号:" & retVal
' Arguments应该是1个或多个参数,此处只考虑了1个的情况,以后需要维护。
reporter.Filter = 3
'清除Error并退出Test
If err.number<>0 Then
Err.Clear
End If
' Reporter.ReportEvent micFail, strStepInfo, Method & " " & Arguments(0) & " 出现错误,错误编号:" & retVal
On Error Goto 0
systemutil.CloseProcessByName("iexplore.exe")
ExitTest
End Function