自动化测试框架中必不可少的一部分是测试脚本的调度执行,一般采用AOM方式调用QTP,需要包括设置测试参数、指定测试结果文件存储的目录、记录执行过程日志、发送邮件等功能,应该可以与持续集成框架结合。
写了个Demo,我把它叫做QTRunner,VBS脚本如下:
LogFile = "Logs/QTRunnerLog" '设置Log文件路径及文件名,可从命令行传入,需改为:Set logFileObj=Getlogobj(argObj(0))
Set logFileObj=Getlogobj(LogFile)
Response "--- Start a new batch task at " & Date & " " & Time
Response "Console logs are saved in " & LogFile & ".log"
' 从配置文件读入各种参数
Set ConfigParameters = CreateObject("Scripting.Dictionary")
ReadTestsFromConfigFile ConfigFile,ConfigParameters
Response "Reading Test Configurations From " & ConfigFile
' 运行测试
RunTest
Closelogobj logFileObj
' 发送Email
SendMail LogFile & ".log"
'----------------------------------------------------------------------------------
' 读取配置文件
Function ReadTestsFromConfigFile( ByVal ConfigFile,ByRef ConfigParameters )
Set GetParameters = ConfigParameters
Dim fso,f,linestr,isValid
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(ConfigFile) Then
Response ConfigFile & " not found"
Exit Function
End If
Dim TestCount
TestCount=0
Set f=fso.OpenTextFile(ConfigFile,1,False) 'Open file for read,if not existed,don't created it.
do while f.AtEndOfStream <> True
linestr = f.ReadLine()
If linestr<>"" Then 'not empty line
If InStr(1,"#") = 1 Then 'begin with "#"
TestCount = TestCount + 1
TestPathStr=Trim(Right(linestr,Len(linestr)-1))
GetParameters.Add TestCount,TestPathStr
ElseIf InStr(1,"ResultPath=") = 1 Then
ResultPathStr = Trim(Right(linestr,Len(linestr)-11))
GetParameters.Add "ResultPath",ResultPathStr
ElseIf InStr(1,"smtpserver=") = 1 Then
smtpserver = Trim(Right(linestr,Len(linestr)-11))
GetParameters.Add "smtpserver",smtpserver
ElseIf InStr(1,"sendusername=") = 1 Then
sendusername = Trim(Right(linestr,Len(linestr)-13))
GetParameters.Add "sendusername",sendusername
ElseIf InStr(1,"sendpassword=") = 1 Then
sendpassword = Trim(Right(linestr,Len(linestr)-13))
GetParameters.Add "sendpassword",sendpassword
ElseIf InStr(1,"Email_Address=") = 1 Then
Email_Address = Trim(Right(linestr,Len(linestr)-14))
GetParameters.Add "Email_Address",Email_Address
End If
End If
Loop
f.Close
GetParameters.Add "TestCount",TestCount
Set ReadTestsFromConfigFile = GetParameters
End Function
' 通过AOM调用QTP执行测试
Function RunTest
Dim qtApp
Dim qtTest
Dim qtResultsOpt
stime = Now
sdate = Year(stime) & "." & Month(stime) & "." & Day(stime) & "_" & Hour(stime) & "." & Minute(stime) & "." & Second(stime)
Set qtApp = CreateObject("QuickTest.Application")
qtApp.Launch
qtApp.Visible = True
Response "Launching QTP..."
TestCount = ConfigParameters.Item("TestCount")
For I=1 To TestCount
TestPath = ConfigParameters.Item(I)
arr = Split(TestPath,";")
testfile = arr(0)
qtApp.Open testfile,True
Set qtTest = qtApp.Test
Response "-------------------------"
Response "opening Test " & arr(0)
Set oParams = qtApp.Test.ParameterDeFinitions.GetParameters()
If UBound(arr) = 1 Then
ParamArr = Split(arr(1),",")
For K=0 to paramCount
oParam = Split(ParamArr(K),"=")
oParams.Item(ParamName).Value = ParamValue
Next
End If
ResultPath = ConfigParameters.Item("ResultPath") & "/" & sdate & "/" & qtTest.Name
Set qtResultsOpt = CreateObject("QuickTest.RunResultsOptions")
qtResultsOpt.ResultsLocation = ResultPath
Response "The Result of Test " & testfile & " will be save in " & ResultPath
qtTest.Run qtResultsOpt,True,oParams ' Run the test
Response "Runing Test " & testfile
Response testfile & "End Running With " & qtTest.LastRunResults.Status & "Status"
Response "Parameter List:"
For P = 1 to oParams.Count
Response oParams.Item(P).Name & "=" & oParams.Item(P).Value
Next
qtTest.Close
Response "Closing Test " & testfile
Response "-------------------------"
Next
qtApp.Quit
Set qtResultsOpt = nothing
Set qtTest = nothing
Set qtApp = nothing
End Function
' 写日志
Function Response(ByVal msg)
logFileObj.WriteLine Date & " " & Time & ": " & msg
If isRunInCmd Then
WScript.Echo Date & " " & Time & ": " & msg
End If
End Function
' 创建日志文件
Function Getlogobj(ByVal cfilePath)
Dim fso,runtimeLog
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(cfilePath) Then
fso.CreateTextFile(cfilePath)
End If
runtimeLog=cfilepath & ".log"
Set Getlogobj = fso.OpenTextFile(runtimeLog,8,True) 'open file for appending.
End Function
Function Closelogobj(ByVal cfile)
cfile.Close
End Function
' 发送邮件
Function SendMail(LogFile)
Set oMessage=WScript.CreateObject("cdo.message")
Set oConf=WScript.CreateObject("CDO.Configuration")
Set fso = CreateObject("Scripting.FileSystemObject")
'创建CDO.Configuration对象后,需要设置邮件服务器的端口、用户帐号等相关信息
oConf.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
oConf.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver")=ConfigParameters.Item("smtpserver")
oConf.Fields("http://schemas.microsoft.com/cdo/configuration/serverport")=25
oConf.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")=1
oConf.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername")=ConfigParameters.Item("sendusername")
oConf.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword")=ConfigParameters.Item("sendpassword")
oConf.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl")=0
oConf.Fields.Update()
'通过CDO的 Message对象设置邮件主题、附件、发送人等信息
oMessage.Configuration = oConf
oMessage.To = ConfigParameters.Item("Email_Address")
oMessage.From = ConfigParameters.Item("sendusername")
oMessage.Subject = "QTRunner Notification"
file = fso.GetAbsolutePathName(LogFile)
Set fso = nothing
oMessage.AddAttachment( file )
TextBody = "QTRunner Finish! See attachment for logs."
oMessage.TextBody = TextBody
oMessage.Send()
End Function
配置文件格式如下:
#D:/Automation Framworks/RunTest/QTRunner_VBS/Tests/Test1;DBType=sqlServer2000,dbname=Pubs,UserID=sasa,Password=123456
#D:/Automation Framworks/RunTest/QTRunner_VBS/Tests/Test2
ResultPath=D:/Automation Framworks/RunTest/QTRunner_VBS/TestResults
smtpserver=smtp.126.com
sendpassword=XXXXXX
[email protected],[email protected]
2009-12-19 12:29:55: --- Start a new batch task at 2009-12-19 12:29:55
2009-12-19 12:29:55: Console logs are saved in Logs/QTRunnerLog.log
2009-12-19 12:29:55: Reading Test Configurations From config.ini
2009-12-19 12:30:07: Launching QTP...
2009-12-19 12:30:10: -------------------------
2009-12-19 12:30:10: opening Test D:/Automation Framworks/RunTest/QTRunner_VBS/Tests/Test1
2009-12-19 12:30:10: The Result of Test D:/Automation Framworks/RunTest/QTRunner_VBS/Tests/Test1 will be save in D:/Automation Framworks/RunTest/QTRunner_VBS/TestResults/2009.12.19_12.29.55/Test1
2009-12-19 12:30:13: Runing Test D:/Automation Framworks/RunTest/QTRunner_VBS/Tests/Test1
2009-12-19 12:30:13: D:/Automation Framworks/RunTest/QTRunner_VBS/Tests/Test1End Running With PassedStatus
2009-12-19 12:30:13: Parameter List:
2009-12-19 12:30:13: dbname=Pubs
2009-12-19 12:30:13: DBType=sqlServer2000
2009-12-19 12:30:13: Password=123456
2009-12-19 12:30:13: Result=True
2009-12-19 12:30:13: UserID=sasa
2009-12-19 12:30:15: Closing Test D:/Automation Framworks/RunTest/QTRunner_VBS/Tests/Test1
2009-12-19 12:30:15: -------------------------
2009-12-19 12:30:17: -------------------------
2009-12-19 12:30:17: opening Test D:/Automation Framworks/RunTest/QTRunner_VBS/Tests/Test2
2009-12-19 12:30:17: The Result of Test D:/Automation Framworks/RunTest/QTRunner_VBS/Tests/Test2 will be save in D:/Automation Framworks/RunTest/QTRunner_VBS/TestResults/2009.12.19_12.29.55/Test2
2009-12-19 12:30:20: Runing Test D:/Automation Framworks/RunTest/QTRunner_VBS/Tests/Test2
2009-12-19 12:30:20: D:/Automation Framworks/RunTest/QTRunner_VBS/Tests/Test2End Running With PassedStatus
2009-12-19 12:30:20: Parameter List:
2009-12-19 12:30:22: Closing Test D:/Automation Framworks/RunTest/QTRunner_VBS/Tests/Test2
2009-12-19 12:30:22: -------------------------
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。