QTP_Logo

[ws_table id=”2″]

An Object Model is a structural representation of objects that comprise the implementation of a system or application. Automation enables software packages to expose their unique features to scripting tools and other applications. Usually Automation uses the Component Object Model (COM). A critical aspect of COM is, how client and servers interact. A COM server is any object that provides services to clients; these services are in the form of COM interface implementations like Properties, Methods, Events and their relationships. Which can be called by any client that is able to get a pointer to one of the interfaces on the server object.

QuickTest Professional is also a COM Server and its different methods and properties are exposed by its COM interface which can be accessed by other applications and scripting tools to control it from outside. In this post, we will see how to interact with QTP from outside via Automation Object Model.

To create the automation object, we use “CreateObject” function. Once the object has been created, we can access all other objects, methods and properties of the QuickTest . CreateObject creates and returns the reference to an automation object.
Syntax:
CreateObject (Servername.typename [, location])

[ws_table id=”1″]

Launching QTP, opening & executing an existing test case and closure of QTP
In this scenario, QTP is launched automatically with default addins i.e. (Web, ActiveX and VB) ,execution of the test case and then exit from QTP

In the code snippet below, COM interface for QTP has been retrieved using the “CreateObject” function. After creating the object, it has been  checked whether the QTP is launched or not, if not then launch the QTP. Once the QTP is launched, to make it visible we have set the visible property of the QTP as True.

Dim qtApp
Dim qtTest
Set qtApp = CreateObject("QuickTest.Application")
If qtApp.launched <> True then
qtApp.Launch
End If
qtApp.Visible = True

Now, we need to set the various parameters for execution of the test case i.e. when the error occurs, capture the error. Set the Run mode and  the result settings, in this code we set the flag as false to not to open the result after execution.

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

In the next section of code, we are opening the test, the test can be opened in two mode i.e. read only or edit mode. Here, we have mentioned the flag as true which opens the test in the read only mode. After opening the code, we create the object for the execution and instructs QTP what needs to be done in case of error. And finally run the test using .Run method and at last check the status of the execution i.e. testcase passed or not.

See also  Automation Object Model : TestComplete

qtApp.Open "C:\Program Files\HP\QuickTest Professional\Tests\trial", True
Set qtTest = qtApp.Test
qtTest.Settings.Run.OnError = "NextStep"
qtTest.Run
MsgBox qtTest.LastRunResults.Status

After the execution, we have to close the test first and then close the QTP using .quit method. In the end we need to release the object using Set Objectname=nothing.

qtTest.Close
qtApp.quit
Set qtTest = Nothing
Set qtApp = Nothing

Here is the complete code for reference.

Dim qtApp
Dim qtTest
'Create the QTP Application object
Set qtApp = CreateObject("QuickTest.Application")
'If QTP is not open then open it
If qtApp.launched <> True then
qtApp.Launch
End If
'Make the QuickTest application visible
qtApp.Visible = True
'Set QuickTest run options
'Instruct QuickTest to perform next step when error occurs
qtApp.Options.Run.ImageCaptureForTestResults = "OnError"
qtApp.Options.Run.RunMode = "Fast"
qtApp.Options.Run.ViewResults = False
'Open the test in read-only mode
qtApp.Open "C:\Program Files\HP\QuickTest Professional\Tests\trial", True
'set run settings for the test
Set qtTest = qtApp.Test
'Instruct QuickTest to perform next step when error occurs
qtTest.Settings.Run.OnError = "NextStep"
'Run the test
qtTest.Run
'Check the results of the test run
MsgBox qtTest.LastRunResults.Status
' Close the test
qtTest.Close
'Close QTP
qtApp.quit

‘Release Object
Set qtTest = Nothing
Set qtApp = Nothing

Copy the above code in a notepad and save the notepad with .vbs extension and execute.

Some of the scripts which can be generated inside QTP at settings tab :

• Go to File > Settings>Properties and click on the “Generate Scripts” buttons
• Go to Tools > Options > General and click on the “Generate Scripts” buttons
• Go to Tools > Object Identification and click on the “Generate Scripts” buttons

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.