标题: QTP AUTOMATION 介绍!让qtp也能自动化执行! [打印本页] 作者: happypeach 时间: 2005-12-11 17:21 标题: QTP AUTOMATION 介绍!让qtp也能自动化执行! 就像你使用QTP进行自动化测试一样~你也可以调用QTP自动化组件模块使你的QTP操作自动化。
使用QTP automation object model暴露的对象、方法、属性,你可以写程序来配置选项、执行测试
代替你手工的这些操作。QuickTest Automation object Model Reference文档中做了详细的介绍。
What is Automation?
Automation是微软的技术,简单的说,就是一个应用提供了一个接口,使另外一个应用可以
编程实现对这个应用的调用。微软的word、execl都提供了这个功能,详细可以查看msdn.
QTP automation object model 提供了 <QuickTest installation folder>\bin\QTObjectModel.dll这个类型库.
在Reference 中object model diagram部分你可以查到对象库。你可以使用任何支持 automation的开发语言、开发环境,例如 VBScript, JavaScript, Visual Basic, Visual C++, or Visual Studio.NET。
Creating the Application Object
这里我们使用vbscript实现(^_^ Reference中提供了vbscript的很多sample可以借用,而且qtp就是
使用vbscript,熟门熟路吗)。let's go!
.............................................................................
Dim qtApp 'As QuickTest.Application ' Declare the Application object variable
Dim qtTest 'As QuickTest.Test ' Declare a Test object variable
Dim qtResultsOpt 'As QuickTest.RunResultsOptions ' Declare a Run Results Options object variable
Dim ScriptPath 'As your qtp script path
Dim ResultPath 'As your qtp result path
Dim qtpLogPath 'As qtp log
ScriptPath = "*:\***"
ResultPath = "*:\***"
qtpLogPath = "*:\autotest.txt"
Set qtApp = CreateObject("QuickTest.Application") ' Create the Application object
Function WriteToFile (str )
Const ForReading = 1, ForWriting = 2,ForAppending = 8
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(qtpLogPath, ForAppending, True)
f.WriteLine str
End Function
Function doQtpTest()
qtApp.Launch ' Start QuickTest
qtApp.Visible = True ' Make the QuickTest application visible
' Set QuickTest run options
qtApp.Options.Run.CaptureForTestResults = "OnError"
qtApp.Options.Run.RunMode = "Fast"
qtApp.Options.Run.ViewResults = False
qtApp.Open ezScriptPath, True ' Open the test in read-only mode
' set run settings for the test
Set qtTest = qtApp.Test
qtTest.Settings.Run.IterationMode = "rngIterations" ' Run only iterations 2 to 4
qtTest.Settings.Run.StartIteration = 2
qtTest.Settings.Run.EndIteration = 4
qtTest.Settings.Run.OnError = "NextStep" ' Instruct QuickTest to perform next step when error occurs
Set qtResultsOpt = CreateObject("QuickTest.RunResultsOptions") ' Create the Run Results Options object
qtResultsOpt.ResultsLocation = ezResultPath ' Set the results location
qtTest.Run qtResultsOpt ' Run the test
'response.write(qtTest.LastRunResults.Status)
'MsgBox qtTest.LastRunResults.Status ' Check the results of the test run
'qtTest.Close ' Close the test
Set qtResultsOpt = Nothing ' Release the Run Results Options object
Set qtTest = Nothing ' Release the Test object
Set qtApp = Nothing ' Release the Application object
End Function
If (qtApp.Launched ) Then
qtApp.Test.stop 'stop current test
WriteToFile(Time&"----QTP is running test! Ant's request stop the current test. The newset vesion test start!----")
qtApp.Quit 'close qtp
doQtpTest()
Else
doQtpTest()
WriteToFile(Time&"----QTP is running test! The newset vesion test start!----")
}
End If
...........................................................
上面的脚本就是如何自动启动你的qtp test,你只需要改写你自己的脚本目录ScriptPath和结果目录ResultPath,保存为vbs脚本文件就可以运行。当然如果单纯只是启动qtp有很多办法实现,命令行都可以,关键是你对qtp的设置等。有了automation的功能,你就可以更快更高效的执行你的测试。
比如说你可以在daily build时加上这个 步骤自动调用执行qtp测试脚本,也许你只听说过build时自动执行Xunit test case.其实你只需要远程执行automation程序(^-^上面的脚本放在asp页面里,其实也可实现远程调用,只需触发http请求了),然后再写个自动发邮件的小程序将测试日志发出来。
那我们的测试就方便了~~~