Techno Logica


Wednesday, September 24, 2008

QuickTest Automation Object Model

What is the QuickTest Automation Object Model?

An object model is a structural representation of software objects (classes) that comprise the implementation of a system or application. An object model defines a set of classes and interfaces, together with their properties, methods and events, and their relationships.

Essentially all configuration and run functionality provided via the QuickTest interface is in some way represented in the QuickTest automation object model via objects, methods, and properties. Although a one-on-one comparison cannot always be made, most dialog boxes in QuickTest have a corresponding automation object, most options in dialog boxes can be set and/or retrieved using the corresponding object property, and most menu commands and other operations have corresponding automation methods.

You can use the objects, methods, and properties exposed by the QuickTest automation object model, along with standard programming elements such as loops and conditional statements to design your script.

Automation scripts are especially useful for performing the same tasks multiple times or on multiple tests or components, or quickly configuring QuickTest according to your needs for a particular environment or application.

The QuickTest automation object model exposes the objects shown in the diagram below.
You can use these objects, and their associated methods and properties, to write
programs that automatically configure QuickTest options and run tests.



The QuickTest Professional application object.

When designing and running QuickTest automation scripts in a tool that supports the loading of type libraries for editing and running scripts, you can use the new operator to load the QuickTest type library before creating the QuickTest Application object.

Syntax:

Dim app as Application
Set app=new Application

or, if other type libraries are loaded in your tool, specify the QuickTest type library as follows:

Dim app as QuickTest.Application
Set app=new QuickTest.Application

When designing or running QuickTest automation scripts in a tool that does not support the loading of type libraries, use the CreateObject() function to create the QuickTest Application object.

Syntax:

Set app = CreateObject("QuickTest.Application")



You can create only one instance of this object. Use this object to return other QuickTest objects and to perform application level operations such as loading add-ins, creating or opening tests, and launching or closing the QuickTest application.

Open QuickTest and Connect to Quality Center

'**********************************************************************************
'Description:
'
'This example connects to a Quality Center project, opens a test (checks it out, if applicable),
'updates the Active Screen values and test object descriptions, and, if applicable,
'checks the modified test back into the Quality Center project.
'
'Assumptions:
'The test1 test is not already checked out.
'There is no unsaved test currently open in QuickTest.
'For more information, see the example for the Test.SaveAs method.
'When QuickTest opens, it loads the add-ins required for the test.
'For more information, see the example for the Test.GetAssociatedAddins method.
'**********************************************************************************


Dim qtApp 'As QuickTest.Application ' Declare the Application object variable
Dim qtUpdateRunOptions 'As QuickTest.UpdateRunOptions
' Declare an Update Run Options object variable
Dim qtRunResultsOptions 'As QuickTest.RunResultsOptions
' Declare a Run Results Options object variable
Dim blsSupportsVerCtrl ' Declare a flag for indicating version control support


Set qtApp = CreateObject("QuickTest.Application") ' Create the Application object
qtApp.Launch ' Start QuickTest
qtApp.Visible = True ' Make the QuickTest application visible

' Make changes in a test on Quality Center with version control
qtApp.TDConnection.Connect "http://tdserver/tdbin","MY_DOMAIN", "My_Project", "James", "not4you", False ' Connect to Quality Center

If qtApp.TDConnection.IsConnected Then ' If connection is successful
blsSupportsVerCtrl = qtApp.TDConnection.SupportVersionControl ' Check whether the project supports vervion control
qtApp.Open "[QualityCenter] Subject\tests\test1", False ' Open the test
If blsSupportsVerCtrl Then ' If the project supports version control
qtApp.Test.CheckOut ' Check out the test
End If

' Prepare the UpdateRunOptions object
Set qtUpdateRunOptions = CreateObject("QuickTest.UpdateRunOptions") ' Create the update Run Options object
' Set the Update Run options: update the Active Screen and test object descriptions. Do not update checkpoint values
qtUpdateRunOptions.UpdateActiveScreen = True
qtUpdateRunOptions.UpdateCheckpoints = False
qtUpdateRunOptions.UpdateTestObjectDescriptions = True

' Prepare the RunResultsOptions object
Set qtRunResultsOptions = CreateObject("QuickTest.RunResultsOptions") ' Create the Run Results Options object
qtRunResultsOptions.ResultsLocation = "" ' Set a temporary results location

'Update the test
qtApp.Test.UpdateRun qtUpdateRunOptions, qtRunResultsOptions ' Run the test in Update Run mode
qtApp.Test.Description = qtApp.Test.Description & vbNewLine & "Updated: " & Now ' Document the update in the test's description (Test Settings > Properties tab)

qtApp.Test.Save ' Save the test

If blsSupportsVerCtrl And qtApp.Test.VerCtrlStatus = "CheckedOut" Then ' If the test is checked out
qtApp.Test.CheckIn ' Check it in
End If

qtApp.TDConnection.Disconnect ' Disconnect from Quality Center
Else
MsgBox "Cannot connect to Quality Center" ' If connection is not successful, display an error message.
End If

qtApp.Quit ' Exit QuickTest
Set qtUpdateRunOptions = Nothing ' Release the Update Run Options object
Set qtRunResultsOptions = Nothing ' Release the Run Results Options object
Set qtApp = Nothing ' Release the Application object


QuickTest Professional Automation - Test Object

Open a Test

'**********************************************************************************
'Description:
'
'This example opens QuickTest without any add-ins loaded
'(standard Windows support only) and specifies the applications
'to be opened for the test.
'
'Assumptions:
'There is no unsaved test currently open in QuickTest.
'For more information, see the example for the Test.SaveAs method.
'**********************************************************************************

Dim qtApp 'As QuickTest.Application ' Declare the Application object variable
Dim qtStdLauncher 'As QuickTest.StdLauncher ' Declare an Windows Applications launcher variable
Dim qtStdApp 'As QuickTest.StdApplication ' Declare as StdApplication object variable
Dim strAdded ' Declare a string variable for the added applications

Set qtApp = CreateObject("QuickTest.Application") ' Create the Application object

' Preparare application and test
qtApp.SetActiveAddins Array() ' Remove all add-ins from the collection so that QuickTest opens with no add-ins loaded
qtApp.Launch ' Start QuickTest
qtApp.Visible = True ' Make the QuickTest application visible
qtApp.Test.SetAssociatedAddins Array() ' Remove all add-ins from the test's associated add-ins list.
Set qtStdLauncher = qtApp.Test.Settings.Launchers.Item("Windows Applications") ' Return the Windows Applications launcher

qtStdLauncher.Active = True ' Instruct QuickTest to open applications when the record session begins

' Set the applications under test
qtStdLauncher.Applications.AddApplication "C:\Viewer.exe", "C:\" ' Add an application
qtStdLauncher.Applications.AddApplication "D:\Apps\Editor.exe", "D:\Apps" ' Add another application

' Save changes and clean up
qtApp.Test.SaveAs "C:\Tests\NewTest" ' Save the test
qtApp.Quit ' Exit QuickTest
Set qtStdLauncher = Nothing ' Release the Windows Applications launcher object
Set qtApp = Nothing ' Release the Application object


Run a Test

'**********************************************************************************
'Description:
'
'This example opens a test, configures run options and settings,
'runs the test, and then checks the results of the test run.
'
'Assumptions:
'There is no unsaved test currently open in QuickTest.
'For more information, see the example for the Test.SaveAs method.
'When QuickTest opens, it loads the add-ins required for the test.
'For more information, see the example for the Test.GetAssociatedAddins method.
'**********************************************************************************

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

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.ImageCaptureForTestResults = "OnError"

qtApp.Options.Run.RunMode = "Fast"
qtApp.Options.Run.ViewResults = False

qtApp.Open "C:\Tests\Test1", 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 = "C:\Tests\Test1\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

Set qtResultsOpt = Nothing ' Release the Run Results Options object
Set qtTest = Nothing ' Release the Test object
Set qtApp = Nothing ' Release the Application object