VB calls QTP
Today, I helped a Canadian friend to solve the problem of VB calling QTP. I wrote a Demo myself.
Here is the source code
1. New project, introducing QuickTest Professional 8.0 object library
two。 The code is as follows:
Private strScriptName As String
Private strRunScript As String
Private Sub cmdAddScript_Click ()
StrScriptName = InputBox ("Please enter the path and name of the QuickTestPro script", "Select Script")
LstQtpScript.AddItem strScriptName
StrScriptName = ""
End Sub
Private Sub cmdRunScript_Click ()
If strRunScript = "" Then
MsgBox "please select you want to a running script in listbox"
Else
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 RunResultsOptions object variable
Set qtApp = CreateObject ("QuickTest.Application") 'Create the Application object
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 strRunScript, 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. ' Instruct QuickTest to perform next step when error occurs
Set qtResultsOpt = CreateObject ("QuickTest.RunResultsOptions") 'Create the RunResultsOptions object
QtResultsOpt.ResultsLocation = strRunScript + "\ Res1" 'Set the results location
QtTest.Run qtResultsOpt 'Run the test
'MsgBox qtTest.LastRunResults.Status' Check the results of the test run
QtTest.Close 'Close the test
QtApp.Quit 'Close QuickTestPro
Set qtResultsOpt = Nothing 'Release the Run Results Options object
Set qtTest = Nothing 'Release the Test object
Set qtApp = Nothing 'Release the Application object
StrRunScript = ""
End If
End Sub
Private Sub Form_Load ()
StrScriptName = ""
StrRunScript = ""
End Sub
Private Sub lstQtpScript_Click ()
StrRunScript = lstQtpScript.Text
End Sub
Because it is not specific about the use of vb, so how to build a new project has been omitted, if you are interested, you can find relevant books to learn.
The above code passed in vb6.0 winxp sp2 debugging.