`
yidongkaifa
  • 浏览: 4071750 次
文章分类
社区版块
存档分类
最新评论

自定义QTP执行日志

 
阅读更多

在平时使用QTP进行自动化项目时,最常见的应该是QTP+QC的组合了,QC对QTP脚本的执行与管理带来了极大的便利,但是随着脚本的不断增加,对于QC执行结果的维护与查看就变得并不是得心应手。

这种情况下,是否可以换个角度,从测试脚本的编写(源头)加以控制,增强测试脚本,对脚本中设计检查点或关键步骤的地方,手工加入执行日志,以便对一次完整的执行,从执行日志中得到必要的信息。

当然,这些日志引擎,可以用单独的函数来实现,一般情况下,需要做如下几项工作(功能):

选择记录日志的文件格式(txt或xml都可)---》实现写日志文件函数---》实现截图功能---》发送邮件


1、生成txt文件。
首先定义一个function:
Public functionWriteLineToFile(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 function

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

另外,在实际应用中,可以在function中增加一个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 function
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 function
另外:假如我们把日志和抓图都结合在一起作为附件发出去的话,在自动化测试中后期会很有用。对于发送邮件,也可以采用Jmail、CDO等组件,在我的C#一篇日志中有讲到,这里,我们只要能实现我们要的功能就达到目的了。

补充:这是一个针对上面第3点如何把抓图放在一个doc中。
Set oWord = CreateObject("Word.Application")
oWord.DisplayAlerts = False
oWord.Visible = False
oWord.documents.open "testWordDoc.doc"
Set oDoc = oWord.ActiveDocument
Set oRange = oDoc.content
oRange.ParagraphFormat.Alignment = 0
oRange.insertafter vbcrlf '& " " & vbcrlf
oRange.collapse(0)
oRange.InlineShapes.AddPicture "ImagePath.bmp", False, True
oWord.ActiveDocument.Save
oWord.Application.Quit True
Set oRange = Nothing
Set oDoc = Nothing
Set oWord = Nothing

当然QTP和excel,word结合也不错的,所以在测试中灵活应用,这里推荐多学习QTP与EXCEL的结合,在框架开发中应用可能非常广泛,能达到不错的执行和管理效果。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics