QTP Reusable Functions Codes

Automation testing using QTP has always been talked upon the most in the testing industry. There are numerous features available within this very toolset, but most of us might not be aware of this very feature which has been discussed in this very articles on automation testing using QTP which is functions created in Vbscript , and is very useful in code customization to meet our automation testing requirements , as it reduces the script maintenance effort.


1. Action Template 

If you need any default Template to be loaded whenever you create a new Action, just follow these steps.

  1. Design the template with all the statements and comments in a text file.
  2. Save the text file as "ActionTemplate.mst" to the "QuickTest Installation Folder" under \dat folder.
  3. Start QTP and whenever you create new Actions, you can find the template by default.

2. ADO Connection Function - Open the ADO connection through QTP

Public Function BIP_sqlDBConnect(sConnectionStr)
   ' Create the Connection Object.
   Set objADOConnection = CreateObject("ADODB.Connection")
   ' Set the Connection String.
   objADOConnection.Open (sConnectionStr)
   'ReturnADO object reference
   Set BIP_sqlDBConnect = objADOConnection
End Function

3. Close QTP - Closing the QTP after Execution

Private Function CloseQTP
   Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
   Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'QTPro.exe'")
   For Each objProcess in colProcess
   objProcess.Terminate()
   Next
   Set objWMIService = Nothing
   Set colProcess = Nothing
End Function

4. Open Application

' Open a specified application
' Parameter: application - the application full name (including location)
'@Description Opens an application
'@Documentation Open the application.

Function OpenApp (application)
   systemUtil.Run application
End Function

5. Add Test Results

' Add a Report.Event step to the Test Results
'Parameters:
' status - Step status (micPass, micFail, micDone or micWarning)
' StepName - Name of the intended step in the report (object name)
' details - Description of the report event
'@Description Reports an event to the Test Results
'@Documentation Report an event to the Test Results.

Public Function AddToTestResults (status, StepName, details)
   Reporter.ReportEvent status, StepName, details
End Function

6. Verify Object Is Enabled

' Add a Report.Event step to the Test Results
'Parameters:
' status - Step status (micPass, micFail, micDone or micWarning)
' StepName - Name of the intended step in the report (object name)
' details - Description of the report event
'@Description Reports an event to the Test Results
'@Documentation Report an event to the Test Results.

Public Function VerifyEnabled (obj)
   Dim enable_property
   'Get the enabled property from the test object
   enable_property = obj.GetROProperty("enabled")
   If enable_property <> 0 Then ' The value is True (anything but 0)
     Reporter.ReportEvent micPass, "VerifyEnabled Succeeded", "The test object is enabled"
     VerifyEnabled = True
   Else
     Reporter.ReportEvent micFail, "VerifyEnabled Failed", "The test object is NOT enabled"
     VerifyEnabled = False
   End If
End Function

7. Verify Object Is Disabled

' Add a Report.Event step to the Test Results
'Parameters:
' status - Step status (micPass, micFail, micDone or micWarning)
' StepName - Name of the intended step in the report (object name)
' details - Description of the report event
'@Description Reports an event to the Test Results
'@Documentation Report an event to the Test Results.

Public Function VerifyDisabled (obj)
   Dim enable_property
   'Get the enabled property from the test object
   enable_property = obj.GetROProperty("disabled")
   If enable_property = 0 Then ' The value is False (0) - Enabled
     Reporter.ReportEvent micPass, "VerifyDisabled Succeeded", "The test object is enabled"
     VerifyDisabled = True
   Else
     Reporter.ReportEvent micFail, "VerifyDisabled Failed", "The test object is NOT enabled"
     VerifyDisabled = False
   End If
End Function

8. Get Value Property

' Return the object 'Value' property
'@Description Returns the Object value
'@Documentation Return the 'Test object name' 'test object type' value.

Public Function GetValueProperty (obj)
   GetValueProperty = obj.GetROProperty("value")
End Function

9. Get Text Property

' Return the object 'Text' property
'@Description Returns the Object value
'@Documentation Return the 'Test object name' 'test object type' value.

Public Function GetTextProperty (obj)
   GetTextProperty = obj.GetROProperty("text")
End Function

10. Get Position Property

' Return the object 'position' property
'@Description Returns the Object value
'@Documentation Return the 'Test object name' 'test object type' value.

Public Function GetPositionProperty (obj)
   GetPositionProperty = obj.GetROProperty("position")
End Function

11. Get Selection Property

' Return the object 'selection' property
'@Description Returns the Object value
'@Documentation Return the 'Test object name' 'test object type' value.

Public Function GetSelectionProperty (obj)
   GetSelectionProperty = obj.GetROProperty("selection")
End Function

12. Get Checked Property

' Return the object 'checked' property
'@Description Returns the Object value
'@Documentation Return the 'Test object name' 'test object type' value.

Public Function GetCheckedProperty (obj)
   GetCheckedProperty = obj.GetROProperty("checked")
End Function



Learn More On QTP Reusable Function creation codes :

QTP-reusable-functions-codes-5
QTP-reusable-functions-codes-4
QTP-reusable-functions-codes-3
QTP-reusable-functions-codes-2
QTP-reusable-functions-codes-1


For gaining more insights in the automation using QTP log on to below url :
Automation Testing Using QTP


Post a Comment

أحدث أقدم