QTRunner
自动化测试框架中必不可少的一部分是测试脚本的调度执行,一般采用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") ConfigFile = "config.ini" 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()??? ? ?????? ? lineStr=Trim(lineStr) ?????? ? 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),",") ??????????????????????????? paramCount = UBound(ParamArr) ??????????????????????????? For K=0 to paramCount ?????????????????????????????????? oParam = Split(ParamArr(K),"=") ?????????????????????????????????? ParamName = oParam(0) ?????????????????????????????????? ParamValue = oParam(1) ?????? ??????????????????????????? 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 oParams = Nothing ?????? 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 sendusername=allencnj@126.com sendpassword=XXXXXX Email_Address=allencnj@126.com,testing_is_believing@126.com ? ? 生成的日志文件如下: 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: ------------------------- (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |